1(function(global_object) { 2 "use strict"; 3 4 // @note 5 // A few conventions for the documentation of this file: 6 // 1. Always use "//" (in contrast with "/**/") 7 // 2. The syntax used is Yardoc (yardoc.org), which is intended for Ruby (se below) 8 // 3. `@param` and `@return` types should be preceded by `JS.` when referring to 9 // JavaScript constructors (e.g. `JS.Function`) otherwise Ruby is assumed. 10 // 4. `nil` and `null` being unambiguous refer to the respective 11 // objects/values in Ruby and JavaScript 12 // 5. This is still WIP :) so please give feedback and suggestions on how 13 // to improve or for alternative solutions 14 // 15 // The way the code is digested before going through Yardoc is a secret kept 16 // in the docs repo (https://github.com/opal/docs/tree/master). 17 18 var console; 19 20 // Detect the global object 21 if (typeof(globalThis) !== 'undefined') { global_object = globalThis; } 22 else if (typeof(global) !== 'undefined') { global_object = global; } 23 else if (typeof(window) !== 'undefined') { global_object = window; } 24 25 // Setup a dummy console object if missing 26 if (global_object.console == null) { 27 global_object.console = {}; 28 } 29 30 if (typeof(global_object.console) === 'object') { 31 console = global_object.console; 32 } else { 33 console = {}; 34 } 35 36 if (!('log' in console)) { console.log = function () {}; } 37 if (!('warn' in console)) { console.warn = console.log; } 38 39 if (typeof(global_object.Opal) !== 'undefined') { 40 console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.'); 41 return global_object.Opal; 42 } 43 44 var nil; 45 46 // The actual class for BasicObject 47 var BasicObject; 48 49 // The actual Object class. 50 // The leading underscore is to avoid confusion with window.Object() 51 var _Object; 52 53 // The actual Module class 54 var Module; 55 56 // The actual Class class 57 var Class; 58 59 // The Opal.Opal class (helpers etc.) 60 var _Opal; 61 62 // The Kernel module 63 var Kernel; 64 65 // The Opal object that is exposed globally 66 var Opal = global_object.Opal = {}; 67 68 // This is a useful reference to global object inside ruby files 69 Opal.global = global_object; 70 71 // Configure runtime behavior with regards to require and unsupported features 72 Opal.config = { 73 missing_require_severity: 'error', // error, warning, ignore 74 unsupported_features_severity: 'warning', // error, warning, ignore 75 experimental_features_severity: 'warning',// warning, ignore 76 enable_stack_trace: true // true, false 77 }; 78 79 // Minify common function calls 80 var $call = Function.prototype.call; 81 var $bind = Function.prototype.bind; 82 var $has_own = Object.hasOwn || $call.bind(Object.prototype.hasOwnProperty); 83 var $set_proto = Object.setPrototypeOf; 84 var $slice = $call.bind(Array.prototype.slice); 85 var $splice = $call.bind(Array.prototype.splice); 86 87 // Nil object id is always 4 88 var nil_id = 4; 89 90 // Generates even sequential numbers greater than 4 91 // (nil_id) to serve as unique ids for ruby objects 92 var unique_id = nil_id; 93 94 // Return next unique id 95 function $uid() { 96 unique_id += 2; 97 return unique_id; 98 }; 99 Opal.uid = $uid; 100 101 // Retrieve or assign the id of an object 102 Opal.id = function(obj) { 103 if (obj.$$is_number) return (obj * 2)+1; 104 if (obj.$$id == null) { 105 $prop(obj, '$$id', $uid()); 106 } 107 return obj.$$id; 108 }; 109 110 // Globals table 111 var $gvars = Opal.gvars = {}; 112 113 // Exit function, this should be replaced by platform specific implementation 114 // (See nodejs and chrome for examples) 115 Opal.exit = function(status) { if ($gvars.DEBUG) console.log('Exited with status '+status); }; 116 117 // keeps track of exceptions for $! 118 Opal.exceptions = []; 119 120 // @private 121 // Pops an exception from the stack and updates `$!`. 122 Opal.pop_exception = function() { 123 var exception = Opal.exceptions.pop(); 124 if (exception) { 125 $gvars["!"] = exception; 126 $gvars["@"] = exception.$backtrace(); 127 } 128 else { 129 $gvars["!"] = $gvars["@"] = nil; 130 } 131 }; 132 133 // A helper function for raising things, that gracefully degrades if necessary 134 // functionality is not yet loaded. 135 function $raise(klass, message) { 136 // Raise Exception, so we can know that something wrong is going on. 137 if (!klass) klass = Opal.Exception || Error; 138 139 if (Kernel && Kernel.$raise) { 140 if (arguments.length > 2) { 141 Kernel.$raise(klass.$new.apply(klass, $slice(arguments, 1))); 142 } 143 else { 144 Kernel.$raise(klass, message); 145 } 146 } 147 else if (!klass.$new) { 148 throw new klass(message); 149 } 150 else { 151 throw klass.$new(message); 152 } 153 } 154 155 function $prop(object, name, initialValue) { 156 if (typeof(object) === "string") { 157 // Special case for: 158 // s = "string" 159 // def s.m; end 160 // String class is the only class that: 161 // + compiles to JS primitive 162 // + allows method definition directly on instances 163 // numbers, true, false and null do not support it. 164 object[name] = initialValue; 165 } else { 166 Object.defineProperty(object, name, { 167 value: initialValue, 168 enumerable: false, 169 configurable: true, 170 writable: true 171 }); 172 } 173 } 174 175 Opal.prop = $prop; 176 177 // @deprecated 178 Opal.defineProperty = Opal.prop; 179 180 Opal.slice = $slice; 181 182 // Helpers 183 // ----- 184 185 var $truthy = Opal.truthy = function(val) { 186 return false !== val && nil !== val && undefined !== val && null !== val && (!(val instanceof Boolean) || true === val.valueOf()); 187 }; 188 189 Opal.falsy = function(val) { 190 return !$truthy(val); 191 }; 192 193 Opal.type_error = function(object, type, method, coerced) { 194 object = object.$$class; 195 196 if (coerced && method) { 197 coerced = coerced.$$class; 198 $raise(Opal.TypeError, 199 "can't convert " + object + " into " + type + 200 " (" + object + "#" + method + " gives " + coerced + ")" 201 ) 202 } else { 203 $raise(Opal.TypeError, 204 "no implicit conversion of " + object + " into " + type 205 ) 206 } 207 }; 208 209 Opal.coerce_to = function(object, type, method, args) { 210 var body; 211 212 if (method === 'to_int' && type === Opal.Integer && object.$$is_number) 213 return object < 0 ? Math.ceil(object) : Math.floor(object); 214 215 if (method === 'to_str' && type === Opal.String && object.$$is_string) 216 return object; 217 218 if (Opal.is_a(object, type)) return object; 219 220 // Fast path for the most common situation 221 if (object['$respond_to?'].$$pristine && object.$method_missing.$$pristine) { 222 body = object[$jsid(method)]; 223 if (body == null || body.$$stub) Opal.type_error(object, type); 224 return body.apply(object, args); 225 } 226 227 if (!object['$respond_to?'](method)) { 228 Opal.type_error(object, type); 229 } 230 231 if (args == null) args = []; 232 return Opal.send(object, method, args); 233 } 234 235 Opal.respond_to = function(obj, jsid, include_all) { 236 if (obj == null || !obj.$$class) return false; 237 include_all = !!include_all; 238 var body = obj[jsid]; 239 240 if (obj['$respond_to?'].$$pristine) { 241 if (typeof(body) === "function" && !body.$$stub) { 242 return true; 243 } 244 if (!obj['$respond_to_missing?'].$$pristine) { 245 return Opal.send(obj, obj['$respond_to_missing?'], [jsid.substr(1), include_all]); 246 } 247 } else { 248 return Opal.send(obj, obj['$respond_to?'], [jsid.substr(1), include_all]); 249 } 250 } 251 252 // TracePoint support 253 // ------------------ 254 // 255 // Support for `TracePoint.trace(:class) do ... end` 256 Opal.trace_class = false; 257 Opal.tracers_for_class = []; 258 259 function invoke_tracers_for_class(klass_or_module) { 260 var i, ii, tracer; 261 262 for(i = 0, ii = Opal.tracers_for_class.length; i < ii; i++) { 263 tracer = Opal.tracers_for_class[i]; 264 tracer.trace_object = klass_or_module; 265 tracer.block.$call(tracer); 266 } 267 } 268 269 function handle_autoload(cref, name) { 270 if (!cref.$$autoload[name].loaded) { 271 cref.$$autoload[name].loaded = true; 272 try { 273 Opal.Kernel.$require(cref.$$autoload[name].path); 274 } catch (e) { 275 cref.$$autoload[name].exception = e; 276 throw e; 277 } 278 cref.$$autoload[name].required = true; 279 if (cref.$$const[name] != null) { 280 cref.$$autoload[name].success = true; 281 return cref.$$const[name]; 282 } 283 } else if (cref.$$autoload[name].loaded && !cref.$$autoload[name].required) { 284 if (cref.$$autoload[name].exception) { throw cref.$$autoload[name].exception; } 285 } 286 } 287 288 // Constants 289 // --------- 290 // 291 // For future reference: 292 // - The Rails autoloading guide (http://guides.rubyonrails.org/v5.0/autoloading_and_reloading_constants.html) 293 // - @ConradIrwin's 2012 post on “Everything you ever wanted to know about constant lookup in Ruby” (http://cirw.in/blog/constant-lookup.html) 294 // 295 // Legend of MRI concepts/names: 296 // - constant reference (cref): the module/class that acts as a namespace 297 // - nesting: the namespaces wrapping the current scope, e.g. nesting inside 298 // `module A; module B::C; end; end` is `[B::C, A]` 299 300 // Get the constant in the scope of the current cref 301 function const_get_name(cref, name) { 302 if (cref) { 303 if (cref.$$const[name] != null) { return cref.$$const[name]; } 304 if (cref.$$autoload && cref.$$autoload[name]) { 305 return handle_autoload(cref, name); 306 } 307 } 308 } 309 310 // Walk up the nesting array looking for the constant 311 function const_lookup_nesting(nesting, name) { 312 var i, ii, constant; 313 314 if (nesting.length === 0) return; 315 316 // If the nesting is not empty the constant is looked up in its elements 317 // and in order. The ancestors of those elements are ignored. 318 for (i = 0, ii = nesting.length; i < ii; i++) { 319 constant = nesting[i].$$const[name]; 320 if (constant != null) { 321 return constant; 322 } else if (nesting[i].$$autoload && nesting[i].$$autoload[name]) { 323 return handle_autoload(nesting[i], name); 324 } 325 } 326 } 327 328 // Walk up the ancestors chain looking for the constant 329 function const_lookup_ancestors(cref, name) { 330 var i, ii, ancestors; 331 332 if (cref == null) return; 333 334 ancestors = $ancestors(cref); 335 336 for (i = 0, ii = ancestors.length; i < ii; i++) { 337 if (ancestors[i].$$const && $has_own(ancestors[i].$$const, name)) { 338 return ancestors[i].$$const[name]; 339 } else if (ancestors[i].$$autoload && ancestors[i].$$autoload[name]) { 340 return handle_autoload(ancestors[i], name); 341 } 342 } 343 } 344 345 // Walk up Object's ancestors chain looking for the constant, 346 // but only if cref is missing or a module. 347 function const_lookup_Object(cref, name) { 348 if (cref == null || cref.$$is_module) { 349 return const_lookup_ancestors(_Object, name); 350 } 351 } 352 353 // Call const_missing if nothing else worked 354 function const_missing(cref, name) { 355 return (cref || _Object).$const_missing(name); 356 } 357 358 // Look for the constant just in the current cref or call `#const_missing` 359 Opal.const_get_local = function(cref, name, skip_missing) { 360 var result; 361 362 if (cref == null) return; 363 364 if (cref === '::') cref = _Object; 365 366 if (!cref.$$is_module && !cref.$$is_class) { 367 $raise(Opal.TypeError, cref.toString() + " is not a class/module"); 368 } 369 370 result = const_get_name(cref, name); 371 return result != null || skip_missing ? result : const_missing(cref, name); 372 }; 373 374 // Look for the constant relative to a cref or call `#const_missing` (when the 375 // constant is prefixed by `::`). 376 Opal.const_get_qualified = function(cref, name, skip_missing) { 377 var result, cache, cached, current_version = Opal.const_cache_version; 378 379 if (name == null) { 380 // A shortpath for calls like ::String => $$$("String") 381 result = const_get_name(_Object, cref); 382 383 if (result != null) return result; 384 return Opal.const_get_qualified(_Object, cref, skip_missing); 385 } 386 387 if (cref == null) return; 388 389 if (cref === '::') cref = _Object; 390 391 if (!cref.$$is_module && !cref.$$is_class) { 392 $raise(Opal.TypeError, cref.toString() + " is not a class/module"); 393 } 394 395 if ((cache = cref.$$const_cache) == null) { 396 $prop(cref, '$$const_cache', Object.create(null)); 397 cache = cref.$$const_cache; 398 } 399 cached = cache[name]; 400 401 if (cached == null || cached[0] !== current_version) { 402 ((result = const_get_name(cref, name)) != null) || 403 ((result = const_lookup_ancestors(cref, name)) != null); 404 cache[name] = [current_version, result]; 405 } else { 406 result = cached[1]; 407 } 408 409 return result != null || skip_missing ? result : const_missing(cref, name); 410 }; 411 412 // Initialize the top level constant cache generation counter 413 Opal.const_cache_version = 1; 414 415 // Look for the constant in the open using the current nesting and the nearest 416 // cref ancestors or call `#const_missing` (when the constant has no :: prefix). 417 Opal.const_get_relative = function(nesting, name, skip_missing) { 418 var cref = nesting[0], result, current_version = Opal.const_cache_version, cache, cached; 419 420 if ((cache = nesting.$$const_cache) == null) { 421 $prop(nesting, '$$const_cache', Object.create(null)); 422 cache = nesting.$$const_cache; 423 } 424 cached = cache[name]; 425 426 if (cached == null || cached[0] !== current_version) { 427 ((result = const_get_name(cref, name)) != null) || 428 ((result = const_lookup_nesting(nesting, name)) != null) || 429 ((result = const_lookup_ancestors(cref, name)) != null) || 430 ((result = const_lookup_Object(cref, name)) != null); 431 432 cache[name] = [current_version, result]; 433 } else { 434 result = cached[1]; 435 } 436 437 return result != null || skip_missing ? result : const_missing(cref, name); 438 }; 439 440 // Register the constant on a cref and opportunistically set the name of 441 // unnamed classes/modules. 442 function $const_set(cref, name, value) { 443 var new_const = true; 444 445 if (cref == null || cref === '::') cref = _Object; 446 447 if (value.$$is_a_module) { 448 if (value.$$name == null || value.$$name === nil) value.$$name = name; 449 if (value.$$base_module == null) value.$$base_module = cref; 450 } 451 452 cref.$$const = (cref.$$const || Object.create(null)); 453 454 if (name in cref.$$const || ("$$autoload" in cref && name in cref.$$autoload)) { 455 new_const = false; 456 } 457 458 cref.$$const[name] = value; 459 460 // Add a short helper to navigate constants manually. 461 // @example 462 // Opal.$$.Regexp.$$.IGNORECASE 463 cref.$$ = cref.$$const; 464 465 Opal.const_cache_version++; 466 467 // Expose top level constants onto the Opal object 468 if (cref === _Object) Opal[name] = value; 469 470 // Name new class directly onto current scope (Opal.Foo.Baz = klass) 471 $prop(cref, name, value); 472 473 if (new_const && cref.$const_added && !cref.$const_added.$$pristine) { 474 cref.$const_added(name); 475 } 476 477 return value; 478 }; 479 480 Opal.const_set = $const_set; 481 482 // Get all the constants reachable from a given cref, by default will include 483 // inherited constants. 484 Opal.constants = function(cref, inherit) { 485 if (inherit == null) inherit = true; 486 487 var module, modules = [cref], i, ii, constants = {}, constant; 488 489 if (inherit) modules = modules.concat($ancestors(cref)); 490 if (inherit && cref.$$is_module) modules = modules.concat([Opal.Object]).concat($ancestors(Opal.Object)); 491 492 for (i = 0, ii = modules.length; i < ii; i++) { 493 module = modules[i]; 494 495 // Do not show Objects constants unless we're querying Object itself 496 if (cref !== _Object && module == _Object) break; 497 498 for (constant in module.$$const) { 499 constants[constant] = true; 500 } 501 if (module.$$autoload) { 502 for (constant in module.$$autoload) { 503 constants[constant] = true; 504 } 505 } 506 } 507 508 return Object.keys(constants); 509 }; 510 511 // Remove a constant from a cref. 512 Opal.const_remove = function(cref, name) { 513 Opal.const_cache_version++; 514 515 if (cref.$$const[name] != null) { 516 var old = cref.$$const[name]; 517 delete cref.$$const[name]; 518 return old; 519 } 520 521 if (cref.$$autoload && cref.$$autoload[name]) { 522 delete cref.$$autoload[name]; 523 return nil; 524 } 525 526 $raise(Opal.NameError, "constant "+cref+"::"+cref.$name()+" not defined"); 527 }; 528 529 // Generates a function that is a curried const_get_relative. 530 Opal.const_get_relative_factory = function(nesting) { 531 return function(name, skip_missing) { 532 return Opal.$$(nesting, name, skip_missing); 533 } 534 } 535 536 // Setup some shortcuts to reduce compiled size 537 Opal.$$ = Opal.const_get_relative; 538 Opal.$$$ = Opal.const_get_qualified; 539 Opal.$r = Opal.const_get_relative_factory; 540 541 // Modules & Classes 542 // ----------------- 543 544 // A `class Foo; end` expression in ruby is compiled to call this runtime 545 // method which either returns an existing class of the given name, or creates 546 // a new class in the given `base` scope. 547 // 548 // If a constant with the given name exists, then we check to make sure that 549 // it is a class and also that the superclasses match. If either of these 550 // fail, then we raise a `TypeError`. Note, `superclass` may be null if one 551 // was not specified in the ruby code. 552 // 553 // We pass a constructor to this method of the form `function ClassName() {}` 554 // simply so that classes show up with nicely formatted names inside debuggers 555 // in the web browser (or node/sprockets). 556 // 557 // The `scope` is the current `self` value where the class is being created 558 // from. We use this to get the scope for where the class should be created. 559 // If `scope` is an object (not a class/module), we simple get its class and 560 // use that as the scope instead. 561 // 562 // @param scope [Object] where the class is being created 563 // @param superclass [Class,null] superclass of the new class (may be null) 564 // @param singleton [Boolean,null] a true value denotes we want to allocate 565 // a singleton 566 // 567 // @return new [Class] or existing ruby class 568 // 569 function $allocate_class(name, superclass, singleton) { 570 var klass; 571 572 if (superclass != null && superclass.$$bridge) { 573 // Inheritance from bridged classes requires 574 // calling original JS constructors 575 klass = function() { 576 var args = $slice(arguments), 577 self = new ($bind.apply(superclass.$$constructor, [null].concat(args)))(); 578 579 // and replacing a __proto__ manually 580 $set_proto(self, klass.$$prototype); 581 return self; 582 } 583 } else { 584 klass = function(){}; 585 } 586 587 if (name && name !== nil) { 588 $prop(klass, 'displayName', '::'+name); 589 } 590 591 $prop(klass, '$$name', name); 592 $prop(klass, '$$constructor', klass); 593 $prop(klass, '$$prototype', klass.prototype); 594 $prop(klass, '$$const', {}); 595 $prop(klass, '$$is_class', true); 596 $prop(klass, '$$is_a_module', true); 597 $prop(klass, '$$super', superclass); 598 $prop(klass, '$$cvars', {}); 599 $prop(klass, '$$own_included_modules', []); 600 $prop(klass, '$$own_prepended_modules', []); 601 $prop(klass, '$$ancestors', []); 602 $prop(klass, '$$ancestors_cache_version', null); 603 $prop(klass, '$$subclasses', []); 604 605 $prop(klass.$$prototype, '$$class', klass); 606 607 // By default if there are no singleton class methods 608 // __proto__ is Class.prototype 609 // Later singleton methods generate a singleton_class 610 // and inject it into ancestors chain 611 if (Opal.Class) { 612 $set_proto(klass, Opal.Class.prototype); 613 } 614 615 if (superclass != null) { 616 $set_proto(klass.$$prototype, superclass.$$prototype); 617 618 if (singleton !== true) { 619 // Let's not forbid GC from cleaning up our 620 // subclasses. 621 if (typeof WeakRef !== 'undefined') { 622 // First, let's clean up our array from empty objects. 623 var i, subclass, rebuilt_subclasses = []; 624 for (i = 0; i < superclass.$$subclasses.length; i++) { 625 subclass = superclass.$$subclasses[i]; 626 if (subclass.deref() !== undefined) { 627 rebuilt_subclasses.push(subclass); 628 } 629 } 630 // Now, let's add our class. 631 rebuilt_subclasses.push(new WeakRef(klass)); 632 superclass.$$subclasses = rebuilt_subclasses; 633 } 634 else { 635 superclass.$$subclasses.push(klass); 636 } 637 } 638 639 if (superclass.$$meta) { 640 // If superclass has metaclass then we have explicitely inherit it. 641 Opal.build_class_singleton_class(klass); 642 } 643 } 644 645 return klass; 646 }; 647 Opal.allocate_class = $allocate_class; 648 649 650 function find_existing_class(scope, name) { 651 // Try to find the class in the current scope 652 var klass = const_get_name(scope, name); 653 654 // If the class exists in the scope, then we must use that 655 if (klass) { 656 // Make sure the existing constant is a class, or raise error 657 if (!klass.$$is_class) { 658 $raise(Opal.TypeError, name + " is not a class"); 659 } 660 661 return klass; 662 } 663 } 664 665 function ensureSuperclassMatch(klass, superclass) { 666 if (klass.$$super !== superclass) { 667 $raise(Opal.TypeError, "superclass mismatch for class " + klass.$$name); 668 } 669 } 670 671 Opal.klass = function(scope, superclass, name) { 672 var bridged; 673 674 if (scope == null || scope == '::') { 675 // Global scope 676 scope = _Object; 677 } else if (!scope.$$is_class && !scope.$$is_module) { 678 // Scope is an object, use its class 679 scope = scope.$$class; 680 } 681 682 // If the superclass is not an Opal-generated class then we're bridging a native JS class 683 if ( 684 superclass != null && (!superclass.hasOwnProperty || ( 685 superclass.hasOwnProperty && !superclass.hasOwnProperty('$$is_class') 686 )) 687 ) { 688 if (superclass.constructor && superclass.constructor.name == "Function") { 689 bridged = superclass; 690 superclass = _Object; 691 } else { 692 $raise(Opal.TypeError, "superclass must be a Class (" + ( 693 (superclass.constructor && (superclass.constructor.name || superclass.constructor.$$name)) || 694 typeof(superclass) 695 ) + " given)"); 696 } 697 } 698 699 var klass = find_existing_class(scope, name); 700 701 if (klass != null) { 702 if (superclass) { 703 // Make sure existing class has same superclass 704 ensureSuperclassMatch(klass, superclass); 705 } 706 } 707 else { 708 // Class doesn't exist, create a new one with given superclass... 709 710 // Not specifying a superclass means we can assume it to be Object 711 if (superclass == null) { 712 superclass = _Object; 713 } 714 715 // Create the class object (instance of Class) 716 klass = $allocate_class(name, superclass); 717 $const_set(scope, name, klass); 718 719 // Call .inherited() hook with new class on the superclass 720 if (superclass.$inherited) { 721 superclass.$inherited(klass); 722 } 723 724 if (bridged) { 725 Opal.bridge(bridged, klass); 726 } 727 } 728 729 if (Opal.trace_class) { invoke_tracers_for_class(klass); } 730 731 return klass; 732 }; 733 734 // Define new module (or return existing module). The given `scope` is basically 735 // the current `self` value the `module` statement was defined in. If this is 736 // a ruby module or class, then it is used, otherwise if the scope is a ruby 737 // object then that objects real ruby class is used (e.g. if the scope is the 738 // main object, then the top level `Object` class is used as the scope). 739 // 740 // If a module of the given name is already defined in the scope, then that 741 // instance is just returned. 742 // 743 // If there is a class of the given name in the scope, then an error is 744 // generated instead (cannot have a class and module of same name in same scope). 745 // 746 // Otherwise, a new module is created in the scope with the given name, and that 747 // new instance is returned back (to be referenced at runtime). 748 // 749 // @param scope [Module, Class] class or module this definition is inside 750 // @param id [String] the name of the new (or existing) module 751 // 752 // @return [Module] 753 function $allocate_module(name) { 754 var constructor = function(){}; 755 var module = constructor; 756 757 if (name) 758 $prop(constructor, 'displayName', name+'.constructor'); 759 760 $prop(module, '$$name', name); 761 $prop(module, '$$prototype', constructor.prototype); 762 $prop(module, '$$const', {}); 763 $prop(module, '$$is_module', true); 764 $prop(module, '$$is_a_module', true); 765 $prop(module, '$$cvars', {}); 766 $prop(module, '$$iclasses', []); 767 $prop(module, '$$own_included_modules', []); 768 $prop(module, '$$own_prepended_modules', []); 769 $prop(module, '$$ancestors', [module]); 770 $prop(module, '$$ancestors_cache_version', null); 771 772 $set_proto(module, Opal.Module.prototype); 773 774 return module; 775 }; 776 Opal.allocate_module = $allocate_module; 777 778 function find_existing_module(scope, name) { 779 var module = const_get_name(scope, name); 780 if (module == null && scope === _Object) module = const_lookup_ancestors(_Object, name); 781 782 if (module) { 783 if (!module.$$is_module && module !== _Object) { 784 $raise(Opal.TypeError, name + " is not a module"); 785 } 786 } 787 788 return module; 789 } 790 791 Opal.module = function(scope, name) { 792 var module; 793 794 if (scope == null || scope == '::') { 795 // Global scope 796 scope = _Object; 797 } else if (!scope.$$is_class && !scope.$$is_module) { 798 // Scope is an object, use its class 799 scope = scope.$$class; 800 } 801 802 module = find_existing_module(scope, name); 803 804 if (module == null) { 805 // Module doesnt exist, create a new one... 806 module = $allocate_module(name); 807 $const_set(scope, name, module); 808 } 809 810 if (Opal.trace_class) { invoke_tracers_for_class(module); } 811 812 return module; 813 }; 814 815 // Return the singleton class for the passed object. 816 // 817 // If the given object alredy has a singleton class, then it will be stored on 818 // the object as the `$$meta` property. If this exists, then it is simply 819 // returned back. 820 // 821 // Otherwise, a new singleton object for the class or object is created, set on 822 // the object at `$$meta` for future use, and then returned. 823 // 824 // @param object [Object] the ruby object 825 // @return [Class] the singleton class for object 826 Opal.get_singleton_class = function(object) { 827 if (object.$$is_number) { 828 $raise(Opal.TypeError, "can't define singleton"); 829 } 830 if (object.$$meta) { 831 return object.$$meta; 832 } 833 834 if (object.hasOwnProperty('$$is_class')) { 835 return Opal.build_class_singleton_class(object); 836 } else if (object.hasOwnProperty('$$is_module')) { 837 return Opal.build_module_singleton_class(object); 838 } else { 839 return Opal.build_object_singleton_class(object); 840 } 841 }; 842 843 // helper to set $$meta on klass, module or instance 844 function set_meta(obj, meta) { 845 if (obj.hasOwnProperty('$$meta')) { 846 obj.$$meta = meta; 847 } else { 848 $prop(obj, '$$meta', meta); 849 } 850 if (obj.$$frozen) { 851 // If a object is frozen (sealed), freeze $$meta too. 852 // No need to inject $$meta.$$prototype in the prototype chain, 853 // as $$meta cannot be modified anyway. 854 obj.$$meta.$freeze(); 855 } else { 856 $set_proto(obj, meta.$$prototype); 857 } 858 }; 859 860 // Build the singleton class for an existing class. Class object are built 861 // with their singleton class already in the prototype chain and inheriting 862 // from their superclass object (up to `Class` itself). 863 // 864 // NOTE: Actually in MRI a class' singleton class inherits from its 865 // superclass' singleton class which in turn inherits from Class. 866 // 867 // @param klass [Class] 868 // @return [Class] 869 Opal.build_class_singleton_class = function(klass) { 870 if (klass.$$meta) { 871 return klass.$$meta; 872 } 873 874 // The singleton_class superclass is the singleton_class of its superclass; 875 // but BasicObject has no superclass (its `$$super` is null), thus we 876 // fallback on `Class`. 877 var superclass = klass === BasicObject ? Class : Opal.get_singleton_class(klass.$$super); 878 879 var meta = $allocate_class(null, superclass, true); 880 881 $prop(meta, '$$is_singleton', true); 882 $prop(meta, '$$singleton_of', klass); 883 set_meta(klass, meta); 884 // Restoring ClassName.class 885 $prop(klass, '$$class', Opal.Class); 886 887 return meta; 888 }; 889 890 Opal.build_module_singleton_class = function(mod) { 891 if (mod.$$meta) { 892 return mod.$$meta; 893 } 894 895 var meta = $allocate_class(null, Opal.Module, true); 896 897 $prop(meta, '$$is_singleton', true); 898 $prop(meta, '$$singleton_of', mod); 899 set_meta(mod, meta); 900 // Restoring ModuleName.class 901 $prop(mod, '$$class', Opal.Module); 902 903 return meta; 904 }; 905 906 // Build the singleton class for a Ruby (non class) Object. 907 // 908 // @param object [Object] 909 // @return [Class] 910 Opal.build_object_singleton_class = function(object) { 911 var superclass = object.$$class, 912 klass = $allocate_class(nil, superclass, true); 913 914 $prop(klass, '$$is_singleton', true); 915 $prop(klass, '$$singleton_of', object); 916 917 delete klass.$$prototype.$$class; 918 919 set_meta(object, klass); 920 921 return klass; 922 }; 923 924 Opal.is_method = function(prop) { 925 return (prop[0] === '$' && prop[1] !== '$'); 926 }; 927 928 Opal.instance_methods = function(mod) { 929 var exclude = [], results = [], ancestors = $ancestors(mod); 930 931 for (var i = 0, l = ancestors.length; i < l; i++) { 932 var ancestor = ancestors[i], 933 proto = ancestor.$$prototype; 934 935 if (proto.hasOwnProperty('$$dummy')) { 936 proto = proto.$$define_methods_on; 937 } 938 939 var props = Object.getOwnPropertyNames(proto); 940 941 for (var j = 0, ll = props.length; j < ll; j++) { 942 var prop = props[j]; 943 944 if (Opal.is_method(prop)) { 945 var method_name = prop.slice(1), 946 method = proto[prop]; 947 948 if (method.$$stub && exclude.indexOf(method_name) === -1) { 949 exclude.push(method_name); 950 } 951 952 if (!method.$$stub && results.indexOf(method_name) === -1 && exclude.indexOf(method_name) === -1) { 953 results.push(method_name); 954 } 955 } 956 } 957 } 958 959 return results; 960 }; 961 962 Opal.own_instance_methods = function(mod) { 963 var results = [], 964 proto = mod.$$prototype; 965 966 if (proto.hasOwnProperty('$$dummy')) { 967 proto = proto.$$define_methods_on; 968 } 969 970 var props = Object.getOwnPropertyNames(proto); 971 972 for (var i = 0, length = props.length; i < length; i++) { 973 var prop = props[i]; 974 975 if (Opal.is_method(prop)) { 976 var method = proto[prop]; 977 978 if (!method.$$stub) { 979 var method_name = prop.slice(1); 980 results.push(method_name); 981 } 982 } 983 } 984 985 return results; 986 }; 987 988 Opal.methods = function(obj) { 989 return Opal.instance_methods(obj.$$meta || obj.$$class); 990 }; 991 992 Opal.own_methods = function(obj) { 993 return obj.$$meta ? Opal.own_instance_methods(obj.$$meta) : []; 994 }; 995 996 Opal.receiver_methods = function(obj) { 997 var mod = Opal.get_singleton_class(obj); 998 var singleton_methods = Opal.own_instance_methods(mod); 999 var instance_methods = Opal.own_instance_methods(mod.$$super); 1000 return singleton_methods.concat(instance_methods); 1001 }; 1002 1003 // Returns an object containing all pairs of names/values 1004 // for all class variables defined in provided +module+ 1005 // and its ancestors. 1006 // 1007 // @param module [Module] 1008 // @return [Object] 1009 Opal.class_variables = function(module) { 1010 var ancestors = $ancestors(module), 1011 i, length = ancestors.length, 1012 result = {}; 1013 1014 for (i = length - 1; i >= 0; i--) { 1015 var ancestor = ancestors[i]; 1016 1017 for (var cvar in ancestor.$$cvars) { 1018 result[cvar] = ancestor.$$cvars[cvar]; 1019 } 1020 } 1021 1022 return result; 1023 }; 1024 1025 // Sets class variable with specified +name+ to +value+ 1026 // in provided +module+ 1027 // 1028 // @param module [Module] 1029 // @param name [String] 1030 // @param value [Object] 1031 Opal.class_variable_set = function(module, name, value) { 1032 var ancestors = $ancestors(module), 1033 i, length = ancestors.length; 1034 1035 for (i = length - 2; i >= 0; i--) { 1036 var ancestor = ancestors[i]; 1037 1038 if ($has_own(ancestor.$$cvars, name)) { 1039 ancestor.$$cvars[name] = value; 1040 return value; 1041 } 1042 } 1043 1044 module.$$cvars[name] = value; 1045 1046 return value; 1047 }; 1048 1049 // Gets class variable with specified +name+ from provided +module+ 1050 // 1051 // @param module [Module] 1052 // @param name [String] 1053 Opal.class_variable_get = function(module, name, tolerant) { 1054 if ($has_own(module.$$cvars, name)) 1055 return module.$$cvars[name]; 1056 1057 var ancestors = $ancestors(module), 1058 i, length = ancestors.length; 1059 1060 for (i = 0; i < length; i++) { 1061 var ancestor = ancestors[i]; 1062 1063 if ($has_own(ancestor.$$cvars, name)) { 1064 return ancestor.$$cvars[name]; 1065 } 1066 } 1067 1068 if (!tolerant) 1069 $raise(Opal.NameError, 'uninitialized class variable '+name+' in '+module.$name()); 1070 1071 return nil; 1072 } 1073 1074 function isRoot(proto) { 1075 return proto.hasOwnProperty('$$iclass') && proto.hasOwnProperty('$$root'); 1076 } 1077 1078 function own_included_modules(module) { 1079 var result = [], mod, proto = Object.getPrototypeOf(module.$$prototype); 1080 1081 while (proto) { 1082 if (proto.hasOwnProperty('$$class')) { 1083 // superclass 1084 break; 1085 } 1086 mod = protoToModule(proto); 1087 if (mod) { 1088 result.push(mod); 1089 } 1090 proto = Object.getPrototypeOf(proto); 1091 } 1092 1093 return result; 1094 } 1095 1096 function own_prepended_modules(module) { 1097 var result = [], mod, proto = Object.getPrototypeOf(module.$$prototype); 1098 1099 if (module.$$prototype.hasOwnProperty('$$dummy')) { 1100 while (proto) { 1101 if (proto === module.$$prototype.$$define_methods_on) { 1102 break; 1103 } 1104 1105 mod = protoToModule(proto); 1106 if (mod) { 1107 result.push(mod); 1108 } 1109 1110 proto = Object.getPrototypeOf(proto); 1111 } 1112 } 1113 1114 return result; 1115 } 1116 1117 1118 // The actual inclusion of a module into a class. 1119 // 1120 // ## Class `$$parent` and `iclass` 1121 // 1122 // To handle `super` calls, every class has a `$$parent`. This parent is 1123 // used to resolve the next class for a super call. A normal class would 1124 // have this point to its superclass. However, if a class includes a module 1125 // then this would need to take into account the module. The module would 1126 // also have to then point its `$$parent` to the actual superclass. We 1127 // cannot modify modules like this, because it might be included in more 1128 // then one class. To fix this, we actually insert an `iclass` as the class' 1129 // `$$parent` which can then point to the superclass. The `iclass` acts as 1130 // a proxy to the actual module, so the `super` chain can then search it for 1131 // the required method. 1132 // 1133 // @param module [Module] the module to include 1134 // @param includer [Module] the target class to include module into 1135 // @return [null] 1136 Opal.append_features = function(module, includer) { 1137 var module_ancestors = $ancestors(module); 1138 var iclasses = []; 1139 1140 if (module_ancestors.indexOf(includer) !== -1) { 1141 $raise(Opal.ArgumentError, 'cyclic include detected'); 1142 } 1143 1144 for (var i = 0, length = module_ancestors.length; i < length; i++) { 1145 var ancestor = module_ancestors[i], iclass = create_iclass(ancestor); 1146 $prop(iclass, '$$included', true); 1147 iclasses.push(iclass); 1148 } 1149 var includer_ancestors = $ancestors(includer), 1150 chain = chain_iclasses(iclasses), 1151 start_chain_after, 1152 end_chain_on; 1153 1154 if (includer_ancestors.indexOf(module) === -1) { 1155 // first time include 1156 1157 // includer -> chain.first -> ...chain... -> chain.last -> includer.parent 1158 start_chain_after = includer.$$prototype; 1159 end_chain_on = Object.getPrototypeOf(includer.$$prototype); 1160 } else { 1161 // The module has been already included, 1162 // we don't need to put it into the ancestors chain again, 1163 // but this module may have new included modules. 1164 // If it's true we need to copy them. 1165 // 1166 // The simplest way is to replace ancestors chain from 1167 // parent 1168 // | 1169 // `module` iclass (has a $$root flag) 1170 // | 1171 // ...previos chain of module.included_modules ... 1172 // | 1173 // "next ancestor" (has a $$root flag or is a real class) 1174 // 1175 // to 1176 // parent 1177 // | 1178 // `module` iclass (has a $$root flag) 1179 // | 1180 // ...regenerated chain of module.included_modules 1181 // | 1182 // "next ancestor" (has a $$root flag or is a real class) 1183 // 1184 // because there are no intermediate classes between `parent` and `next ancestor`. 1185 // It doesn't break any prototypes of other objects as we don't change class references. 1186 1187 var parent = includer.$$prototype, module_iclass = Object.getPrototypeOf(parent); 1188 1189 while (module_iclass != null) { 1190 if (module_iclass.$$module === module && isRoot(module_iclass)) { 1191 break; 1192 } 1193 1194 parent = module_iclass; 1195 module_iclass = Object.getPrototypeOf(module_iclass); 1196 } 1197 1198 if (module_iclass) { 1199 // module has been directly included 1200 var next_ancestor = Object.getPrototypeOf(module_iclass); 1201 1202 // skip non-root iclasses (that were recursively included) 1203 while (next_ancestor.hasOwnProperty('$$iclass') && !isRoot(next_ancestor)) { 1204 next_ancestor = Object.getPrototypeOf(next_ancestor); 1205 } 1206 1207 start_chain_after = parent; 1208 end_chain_on = next_ancestor; 1209 } else { 1210 // module has not been directly included but was in ancestor chain because it was included by another module 1211 // include it directly 1212 start_chain_after = includer.$$prototype; 1213 end_chain_on = Object.getPrototypeOf(includer.$$prototype); 1214 } 1215 } 1216 1217 $set_proto(start_chain_after, chain.first); 1218 $set_proto(chain.last, end_chain_on); 1219 1220 // recalculate own_included_modules cache 1221 includer.$$own_included_modules = own_included_modules(includer); 1222 1223 Opal.const_cache_version++; 1224 }; 1225 1226 Opal.prepend_features = function(module, prepender) { 1227 // Here we change the ancestors chain from 1228 // 1229 // prepender 1230 // | 1231 // parent 1232 // 1233 // to: 1234 // 1235 // dummy(prepender) 1236 // | 1237 // iclass(module) 1238 // | 1239 // iclass(prepender) 1240 // | 1241 // parent 1242 var module_ancestors = $ancestors(module); 1243 var iclasses = []; 1244 1245 if (module_ancestors.indexOf(prepender) !== -1) { 1246 $raise(Opal.ArgumentError, 'cyclic prepend detected'); 1247 } 1248 1249 for (var i = 0, length = module_ancestors.length; i < length; i++) { 1250 var ancestor = module_ancestors[i], iclass = create_iclass(ancestor); 1251 $prop(iclass, '$$prepended', true); 1252 iclasses.push(iclass); 1253 } 1254 1255 var chain = chain_iclasses(iclasses), 1256 dummy_prepender = prepender.$$prototype, 1257 previous_parent = Object.getPrototypeOf(dummy_prepender), 1258 prepender_iclass, 1259 start_chain_after, 1260 end_chain_on; 1261 1262 if (dummy_prepender.hasOwnProperty('$$dummy')) { 1263 // The module already has some prepended modules 1264 // which means that we don't need to make it "dummy" 1265 prepender_iclass = dummy_prepender.$$define_methods_on; 1266 } else { 1267 // Making the module "dummy" 1268 prepender_iclass = create_dummy_iclass(prepender); 1269 flush_methods_in(prepender); 1270 $prop(dummy_prepender, '$$dummy', true); 1271 $prop(dummy_prepender, '$$define_methods_on', prepender_iclass); 1272 1273 // Converting 1274 // dummy(prepender) -> previous_parent 1275 // to 1276 // dummy(prepender) -> iclass(prepender) -> previous_parent 1277 $set_proto(dummy_prepender, prepender_iclass); 1278 $set_proto(prepender_iclass, previous_parent); 1279 } 1280 1281 var prepender_ancestors = $ancestors(prepender); 1282 1283 if (prepender_ancestors.indexOf(module) === -1) { 1284 // first time prepend 1285 1286 start_chain_after = dummy_prepender; 1287 1288 // next $$root or prepender_iclass or non-$$iclass 1289 end_chain_on = Object.getPrototypeOf(dummy_prepender); 1290 while (end_chain_on != null) { 1291 if ( 1292 end_chain_on.hasOwnProperty('$$root') || 1293 end_chain_on === prepender_iclass || 1294 !end_chain_on.hasOwnProperty('$$iclass') 1295 ) { 1296 break; 1297 } 1298 1299 end_chain_on = Object.getPrototypeOf(end_chain_on); 1300 } 1301 } else { 1302 $raise(Opal.RuntimeError, "Prepending a module multiple times is not supported"); 1303 } 1304 1305 $set_proto(start_chain_after, chain.first); 1306 $set_proto(chain.last, end_chain_on); 1307 1308 // recalculate own_prepended_modules cache 1309 prepender.$$own_prepended_modules = own_prepended_modules(prepender); 1310 1311 Opal.const_cache_version++; 1312 }; 1313 1314 function flush_methods_in(module) { 1315 var proto = module.$$prototype, 1316 props = Object.getOwnPropertyNames(proto); 1317 1318 for (var i = 0; i < props.length; i++) { 1319 var prop = props[i]; 1320 if (Opal.is_method(prop)) { 1321 delete proto[prop]; 1322 } 1323 } 1324 } 1325 1326 function create_iclass(module) { 1327 var iclass = create_dummy_iclass(module); 1328 1329 if (module.$$is_module) { 1330 module.$$iclasses.push(iclass); 1331 } 1332 1333 return iclass; 1334 } 1335 1336 // Dummy iclass doesn't receive updates when the module gets a new method. 1337 function create_dummy_iclass(module) { 1338 var iclass = {}, 1339 proto = module.$$prototype; 1340 1341 if (proto.hasOwnProperty('$$dummy')) { 1342 proto = proto.$$define_methods_on; 1343 } 1344 1345 var props = Object.getOwnPropertyNames(proto), 1346 length = props.length, i; 1347 1348 for (i = 0; i < length; i++) { 1349 var prop = props[i]; 1350 $prop(iclass, prop, proto[prop]); 1351 } 1352 1353 $prop(iclass, '$$iclass', true); 1354 $prop(iclass, '$$module', module); 1355 1356 return iclass; 1357 } 1358 1359 function chain_iclasses(iclasses) { 1360 var length = iclasses.length, first = iclasses[0]; 1361 1362 $prop(first, '$$root', true); 1363 1364 if (length === 1) { 1365 return { first: first, last: first }; 1366 } 1367 1368 var previous = first; 1369 1370 for (var i = 1; i < length; i++) { 1371 var current = iclasses[i]; 1372 $set_proto(previous, current); 1373 previous = current; 1374 } 1375 1376 1377 return { first: iclasses[0], last: iclasses[length - 1] }; 1378 } 1379 1380 // For performance, some core Ruby classes are toll-free bridged to their 1381 // native JavaScript counterparts (e.g. a Ruby Array is a JavaScript Array). 1382 // 1383 // This method is used to setup a native constructor (e.g. Array), to have 1384 // its prototype act like a normal Ruby class. Firstly, a new Ruby class is 1385 // created using the native constructor so that its prototype is set as the 1386 // target for the new class. Note: all bridged classes are set to inherit 1387 // from Object. 1388 // 1389 // Example: 1390 // 1391 // Opal.bridge(self, Function); 1392 // 1393 // @param klass [Class] the Ruby class to bridge 1394 // @param constructor [JS.Function] native JavaScript constructor to use 1395 // @return [Class] returns the passed Ruby class 1396 // 1397 Opal.bridge = function(native_klass, klass) { 1398 if (native_klass.hasOwnProperty('$$bridge')) { 1399 $raise(Opal.ArgumentError, "already bridged"); 1400 } 1401 1402 // constructor is a JS function with a prototype chain like: 1403 // - constructor 1404 // - super 1405 // 1406 // What we need to do is to inject our class (with its prototype chain) 1407 // between constructor and super. For example, after injecting ::Object 1408 // into JS String we get: 1409 // 1410 // - constructor (window.String) 1411 // - Opal.Object 1412 // - Opal.Kernel 1413 // - Opal.BasicObject 1414 // - super (window.Object) 1415 // - null 1416 // 1417 $prop(native_klass, '$$bridge', klass); 1418 $set_proto(native_klass.prototype, (klass.$$super || Opal.Object).$$prototype); 1419 $prop(klass, '$$prototype', native_klass.prototype); 1420 1421 $prop(klass.$$prototype, '$$class', klass); 1422 $prop(klass, '$$constructor', native_klass); 1423 $prop(klass, '$$bridge', true); 1424 }; 1425 1426 function protoToModule(proto) { 1427 if (proto.hasOwnProperty('$$dummy')) { 1428 return; 1429 } else if (proto.hasOwnProperty('$$iclass')) { 1430 return proto.$$module; 1431 } else if (proto.hasOwnProperty('$$class')) { 1432 return proto.$$class; 1433 } 1434 } 1435 1436 function own_ancestors(module) { 1437 return module.$$own_prepended_modules.concat([module]).concat(module.$$own_included_modules); 1438 } 1439 1440 // The Array of ancestors for a given module/class 1441 function $ancestors(module) { 1442 if (!module) { return []; } 1443 1444 if (module.$$ancestors_cache_version === Opal.const_cache_version) { 1445 return module.$$ancestors; 1446 } 1447 1448 var result = [], i, mods, length; 1449 1450 for (i = 0, mods = own_ancestors(module), length = mods.length; i < length; i++) { 1451 result.push(mods[i]); 1452 } 1453 1454 if (module.$$super) { 1455 for (i = 0, mods = $ancestors(module.$$super), length = mods.length; i < length; i++) { 1456 result.push(mods[i]); 1457 } 1458 } 1459 1460 module.$$ancestors_cache_version = Opal.const_cache_version; 1461 module.$$ancestors = result; 1462 1463 return result; 1464 }; 1465 Opal.ancestors = $ancestors; 1466 1467 Opal.included_modules = function(module) { 1468 var result = [], mod = null, proto = Object.getPrototypeOf(module.$$prototype); 1469 1470 for (; proto && Object.getPrototypeOf(proto); proto = Object.getPrototypeOf(proto)) { 1471 mod = protoToModule(proto); 1472 if (mod && mod.$$is_module && proto.$$iclass && proto.$$included) { 1473 result.push(mod); 1474 } 1475 } 1476 1477 return result; 1478 }; 1479 1480 1481 // Method Missing 1482 // -------------- 1483 1484 // Methods stubs are used to facilitate method_missing in opal. A stub is a 1485 // placeholder function which just calls `method_missing` on the receiver. 1486 // If no method with the given name is actually defined on an object, then it 1487 // is obvious to say that the stub will be called instead, and then in turn 1488 // method_missing will be called. 1489 // 1490 // When a file in ruby gets compiled to javascript, it includes a call to 1491 // this function which adds stubs for every method name in the compiled file. 1492 // It should then be safe to assume that method_missing will work for any 1493 // method call detected. 1494 // 1495 // Method stubs are added to the BasicObject prototype, which every other 1496 // ruby object inherits, so all objects should handle method missing. A stub 1497 // is only added if the given property name (method name) is not already 1498 // defined. 1499 // 1500 // Note: all ruby methods have a `$` prefix in javascript, so all stubs will 1501 // have this prefix as well (to make this method more performant). 1502 // 1503 // Opal.add_stubs("foo,bar,baz="); 1504 // 1505 // All stub functions will have a private `$$stub` property set to true so 1506 // that other internal methods can detect if a method is just a stub or not. 1507 // `Kernel#respond_to?` uses this property to detect a methods presence. 1508 // 1509 // @param stubs [Array] an array of method stubs to add 1510 // @return [undefined] 1511 Opal.add_stubs = function(stubs) { 1512 var proto = Opal.BasicObject.$$prototype; 1513 var stub, existing_method; 1514 stubs = stubs.split(','); 1515 1516 for (var i = 0, length = stubs.length; i < length; i++) { 1517 stub = $jsid(stubs[i]), existing_method = proto[stub]; 1518 1519 if (existing_method == null || existing_method.$$stub) { 1520 Opal.add_stub_for(proto, stub); 1521 } 1522 } 1523 }; 1524 1525 // Add a method_missing stub function to the given prototype for the 1526 // given name. 1527 // 1528 // @param prototype [Prototype] the target prototype 1529 // @param stub [String] stub name to add (e.g. "$foo") 1530 // @return [undefined] 1531 Opal.add_stub_for = function(prototype, stub) { 1532 // Opal.stub_for(stub) is the method_missing_stub 1533 $prop(prototype, stub, Opal.stub_for(stub)); 1534 }; 1535 1536 // Generate the method_missing stub for a given method name. 1537 // 1538 // @param method_name [String] The js-name of the method to stub (e.g. "$foo") 1539 // @return [undefined] 1540 Opal.stub_for = function(method_name) { 1541 1542 function method_missing_stub() { 1543 // Copy any given block onto the method_missing dispatcher 1544 this.$method_missing.$$p = method_missing_stub.$$p; 1545 1546 // Set block property to null ready for the next call (stop false-positives) 1547 method_missing_stub.$$p = null; 1548 1549 // call method missing with correct args (remove '$' prefix on method name) 1550 var args_ary = new Array(arguments.length); 1551 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = arguments[i]; } 1552 1553 return this.$method_missing.apply(this, [method_name.slice(1)].concat(args_ary)); 1554 } 1555 1556 method_missing_stub.$$stub = true; 1557 1558 return method_missing_stub; 1559 }; 1560 1561 1562 // Methods 1563 // ------- 1564 1565 // Arity count error dispatcher for methods 1566 // 1567 // @param actual [Fixnum] number of arguments given to method 1568 // @param expected [Fixnum] expected number of arguments 1569 // @param object [Object] owner of the method +meth+ 1570 // @param meth [String] method name that got wrong number of arguments 1571 // @raise [ArgumentError] 1572 Opal.ac = function(actual, expected, object, meth) { 1573 var inspect = ''; 1574 if (object.$$is_a_module) { 1575 inspect += object.$$name + '.'; 1576 } 1577 else { 1578 inspect += object.$$class.$$name + '#'; 1579 } 1580 inspect += meth; 1581 1582 $raise(Opal.ArgumentError, '[' + inspect + '] wrong number of arguments (given ' + actual + ', expected ' + expected + ')'); 1583 }; 1584 1585 // Arity count error dispatcher for blocks 1586 // 1587 // @param actual [Fixnum] number of arguments given to block 1588 // @param expected [Fixnum] expected number of arguments 1589 // @param context [Object] context of the block definition 1590 // @raise [ArgumentError] 1591 Opal.block_ac = function(actual, expected, context) { 1592 var inspect = "`block in " + context + "'"; 1593 1594 $raise(Opal.ArgumentError, inspect + ': wrong number of arguments (given ' + actual + ', expected ' + expected + ')'); 1595 }; 1596 1597 function get_ancestors(obj) { 1598 if (obj.hasOwnProperty('$$meta') && obj.$$meta !== null) { 1599 return $ancestors(obj.$$meta); 1600 } else { 1601 return $ancestors(obj.$$class); 1602 } 1603 }; 1604 1605 // Super dispatcher 1606 Opal.find_super = function(obj, mid, current_func, defcheck, allow_stubs) { 1607 var jsid = $jsid(mid), ancestors, super_method; 1608 1609 ancestors = get_ancestors(obj); 1610 1611 var current_index = ancestors.indexOf(current_func.$$owner); 1612 1613 for (var i = current_index + 1; i < ancestors.length; i++) { 1614 var ancestor = ancestors[i], 1615 proto = ancestor.$$prototype; 1616 1617 if (proto.hasOwnProperty('$$dummy')) { 1618 proto = proto.$$define_methods_on; 1619 } 1620 1621 if (proto.hasOwnProperty(jsid)) { 1622 super_method = proto[jsid]; 1623 break; 1624 } 1625 } 1626 1627 if (!defcheck && super_method && super_method.$$stub && obj.$method_missing.$$pristine) { 1628 // method_missing hasn't been explicitly defined 1629 $raise(Opal.NoMethodError, 'super: no superclass method `'+mid+"' for "+obj, mid); 1630 } 1631 1632 return (super_method.$$stub && !allow_stubs) ? null : super_method; 1633 }; 1634 1635 // Iter dispatcher for super in a block 1636 Opal.find_block_super = function(obj, jsid, current_func, defcheck, implicit) { 1637 var call_jsid = jsid; 1638 1639 if (!current_func) { 1640 $raise(Opal.RuntimeError, "super called outside of method"); 1641 } 1642 1643 if (implicit && current_func.$$define_meth) { 1644 $raise(Opal.RuntimeError, 1645 "implicit argument passing of super from method defined by define_method() is not supported. " + 1646 "Specify all arguments explicitly" 1647 ); 1648 } 1649 1650 if (current_func.$$def) { 1651 call_jsid = current_func.$$jsid; 1652 } 1653 1654 return Opal.find_super(obj, call_jsid, current_func, defcheck); 1655 }; 1656 1657 // @deprecated 1658 Opal.find_super_dispatcher = Opal.find_super; 1659 1660 // @deprecated 1661 Opal.find_iter_super_dispatcher = Opal.find_block_super; 1662 1663 // handles yield calls for 1 yielded arg 1664 Opal.yield1 = function(block, arg) { 1665 if (typeof(block) !== "function") { 1666 $raise(Opal.LocalJumpError, "no block given"); 1667 } 1668 1669 var has_mlhs = block.$$has_top_level_mlhs_arg, 1670 has_trailing_comma = block.$$has_trailing_comma_in_args; 1671 1672 if (block.length > 1 || ((has_mlhs || has_trailing_comma) && block.length === 1)) { 1673 arg = Opal.to_ary(arg); 1674 } 1675 1676 if ((block.length > 1 || (has_trailing_comma && block.length === 1)) && arg.$$is_array) { 1677 return block.apply(null, arg); 1678 } 1679 else { 1680 return block(arg); 1681 } 1682 }; 1683 1684 // handles yield for > 1 yielded arg 1685 Opal.yieldX = function(block, args) { 1686 if (typeof(block) !== "function") { 1687 $raise(Opal.LocalJumpError, "no block given"); 1688 } 1689 1690 if (block.length > 1 && args.length === 1) { 1691 if (args[0].$$is_array) { 1692 return block.apply(null, args[0]); 1693 } 1694 } 1695 1696 if (!args.$$is_array) { 1697 var args_ary = new Array(args.length); 1698 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } 1699 1700 return block.apply(null, args_ary); 1701 } 1702 1703 return block.apply(null, args); 1704 }; 1705 1706 // Finds the corresponding exception match in candidates. Each candidate can 1707 // be a value, or an array of values. Returns null if not found. 1708 Opal.rescue = function(exception, candidates) { 1709 for (var i = 0; i < candidates.length; i++) { 1710 var candidate = candidates[i]; 1711 1712 if (candidate.$$is_array) { 1713 var result = Opal.rescue(exception, candidate); 1714 1715 if (result) { 1716 return result; 1717 } 1718 } 1719 else if (candidate === Opal.JS.Error || candidate['$==='](exception)) { 1720 return candidate; 1721 } 1722 } 1723 1724 return null; 1725 }; 1726 1727 Opal.is_a = function(object, klass) { 1728 if (klass != null && object.$$meta === klass || object.$$class === klass) { 1729 return true; 1730 } 1731 1732 if (object.$$is_number && klass.$$is_number_class) { 1733 return (klass.$$is_integer_class) ? (object % 1) === 0 : true; 1734 } 1735 1736 var ancestors = $ancestors(object.$$is_class ? Opal.get_singleton_class(object) : (object.$$meta || object.$$class)); 1737 1738 return ancestors.indexOf(klass) !== -1; 1739 }; 1740 1741 // Helpers for extracting kwsplats 1742 // Used for: { **h } 1743 Opal.to_hash = function(value) { 1744 if (value.$$is_hash) { 1745 return value; 1746 } 1747 else if (value['$respond_to?']('to_hash', true)) { 1748 var hash = value.$to_hash(); 1749 if (hash.$$is_hash) { 1750 return hash; 1751 } 1752 else { 1753 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1754 " to Hash (" + value.$$class + "#to_hash gives " + hash.$$class + ")"); 1755 } 1756 } 1757 else { 1758 $raise(Opal.TypeError, "no implicit conversion of " + value.$$class + " into Hash"); 1759 } 1760 }; 1761 1762 // Helpers for implementing multiple assignment 1763 // Our code for extracting the values and assigning them only works if the 1764 // return value is a JS array. 1765 // So if we get an Array subclass, extract the wrapped JS array from it 1766 1767 // Used for: a, b = something (no splat) 1768 Opal.to_ary = function(value) { 1769 if (value.$$is_array) { 1770 return value; 1771 } 1772 else if (value['$respond_to?']('to_ary', true)) { 1773 var ary = value.$to_ary(); 1774 if (ary === nil) { 1775 return [value]; 1776 } 1777 else if (ary.$$is_array) { 1778 return ary; 1779 } 1780 else { 1781 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1782 " to Array (" + value.$$class + "#to_ary gives " + ary.$$class + ")"); 1783 } 1784 } 1785 else { 1786 return [value]; 1787 } 1788 }; 1789 1790 // Used for: a, b = *something (with splat) 1791 Opal.to_a = function(value) { 1792 if (value.$$is_array) { 1793 // A splatted array must be copied 1794 return value.slice(); 1795 } 1796 else if (value['$respond_to?']('to_a', true)) { 1797 var ary = value.$to_a(); 1798 if (ary === nil) { 1799 return [value]; 1800 } 1801 else if (ary.$$is_array) { 1802 return ary; 1803 } 1804 else { 1805 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1806 " to Array (" + value.$$class + "#to_a gives " + ary.$$class + ")"); 1807 } 1808 } 1809 else { 1810 return [value]; 1811 } 1812 }; 1813 1814 // Used for extracting keyword arguments from arguments passed to 1815 // JS function. If provided +arguments+ list doesn't have a Hash 1816 // as a last item, returns a blank Hash. 1817 // 1818 // @param parameters [Array] 1819 // @return [Hash] 1820 // 1821 Opal.extract_kwargs = function(parameters) { 1822 var kwargs = parameters[parameters.length - 1]; 1823 if (kwargs != null && Opal.respond_to(kwargs, '$to_hash', true)) { 1824 $splice(parameters, parameters.length - 1); 1825 return kwargs; 1826 } 1827 }; 1828 1829 // Used to get a list of rest keyword arguments. Method takes the given 1830 // keyword args, i.e. the hash literal passed to the method containing all 1831 // keyword arguemnts passed to method, as well as the used args which are 1832 // the names of required and optional arguments defined. This method then 1833 // just returns all key/value pairs which have not been used, in a new 1834 // hash literal. 1835 // 1836 // @param given_args [Hash] all kwargs given to method 1837 // @param used_args [Object<String: true>] all keys used as named kwargs 1838 // @return [Hash] 1839 // 1840 Opal.kwrestargs = function(given_args, used_args) { 1841 var keys = [], 1842 map = {}, 1843 key , 1844 given_map = given_args.$$smap; 1845 1846 for (key in given_map) { 1847 if (!used_args[key]) { 1848 keys.push(key); 1849 map[key] = given_map[key]; 1850 } 1851 } 1852 1853 return Opal.hash2(keys, map); 1854 }; 1855 1856 function apply_blockopts(block, blockopts) { 1857 if (typeof(blockopts) === 'number') { 1858 block.$$arity = blockopts; 1859 } 1860 else if (typeof(blockopts) === 'object') { 1861 Object.assign(block, blockopts); 1862 } 1863 } 1864 1865 // Optimization for a costly operation of prepending '$' to method names 1866 var jsid_cache = {} 1867 function $jsid(name) { 1868 return jsid_cache[name] || (jsid_cache[name] = '$' + name); 1869 } 1870 Opal.jsid = $jsid; 1871 1872 // Calls passed method on a ruby object with arguments and block: 1873 // 1874 // Can take a method or a method name. 1875 // 1876 // 1. When method name gets passed it invokes it by its name 1877 // and calls 'method_missing' when object doesn't have this method. 1878 // Used internally by Opal to invoke method that takes a block or a splat. 1879 // 2. When method (i.e. method body) gets passed, it doesn't trigger 'method_missing' 1880 // because it doesn't know the name of the actual method. 1881 // Used internally by Opal to invoke 'super'. 1882 // 1883 // @example 1884 // var my_array = [1, 2, 3, 4] 1885 // Opal.send(my_array, 'length') # => 4 1886 // Opal.send(my_array, my_array.$length) # => 4 1887 // 1888 // Opal.send(my_array, 'reverse!') # => [4, 3, 2, 1] 1889 // Opal.send(my_array, my_array['$reverse!']') # => [4, 3, 2, 1] 1890 // 1891 // @param recv [Object] ruby object 1892 // @param method [Function, String] method body or name of the method 1893 // @param args [Array] arguments that will be passed to the method call 1894 // @param block [Function] ruby block 1895 // @param blockopts [Object, Number] optional properties to set on the block 1896 // @return [Object] returning value of the method call 1897 Opal.send = function(recv, method, args, block, blockopts) { 1898 var body; 1899 1900 if (typeof(method) === 'function') { 1901 body = method; 1902 method = null; 1903 } else if (typeof(method) === 'string') { 1904 body = recv[$jsid(method)]; 1905 } else { 1906 $raise(Opal.NameError, "Passed method should be a string or a function"); 1907 } 1908 1909 return Opal.send2(recv, body, method, args, block, blockopts); 1910 }; 1911 1912 Opal.send2 = function(recv, body, method, args, block, blockopts) { 1913 if (body == null && method != null && recv.$method_missing) { 1914 body = recv.$method_missing; 1915 args = [method].concat(args); 1916 } 1917 1918 apply_blockopts(block, blockopts); 1919 1920 if (typeof block === 'function') body.$$p = block; 1921 return body.apply(recv, args); 1922 }; 1923 1924 Opal.refined_send = function(refinement_groups, recv, method, args, block, blockopts) { 1925 var i, j, k, ancestors, ancestor, refinements, refinement, refine_modules, refine_module, body; 1926 1927 ancestors = get_ancestors(recv); 1928 1929 // For all ancestors that there are, starting from the closest to the furthest... 1930 for (i = 0; i < ancestors.length; i++) { 1931 ancestor = Opal.id(ancestors[i]); 1932 1933 // For all refinement groups there are, starting from the closest scope to the furthest... 1934 for (j = 0; j < refinement_groups.length; j++) { 1935 refinements = refinement_groups[j]; 1936 1937 // For all refinements there are, starting from the last `using` call to the furthest... 1938 for (k = refinements.length - 1; k >= 0; k--) { 1939 refinement = refinements[k]; 1940 if (typeof refinement.$$refine_modules === 'undefined') continue; 1941 1942 // A single module being given as an argument of the `using` call contains multiple 1943 // refinement modules 1944 refine_modules = refinement.$$refine_modules; 1945 1946 // Does this module refine a given call for a given ancestor module? 1947 if (typeof refine_modules[ancestor] === 'undefined') continue; 1948 refine_module = refine_modules[ancestor]; 1949 1950 // Does this module define a method we want to call? 1951 if (typeof refine_module.$$prototype[$jsid(method)] !== 'undefined') { 1952 body = refine_module.$$prototype[$jsid(method)]; 1953 return Opal.send2(recv, body, method, args, block, blockopts); 1954 } 1955 } 1956 } 1957 } 1958 1959 return Opal.send(recv, method, args, block, blockopts); 1960 }; 1961 1962 Opal.lambda = function(block, blockopts) { 1963 block.$$is_lambda = true; 1964 1965 apply_blockopts(block, blockopts); 1966 1967 return block; 1968 }; 1969 1970 // Used to define methods on an object. This is a helper method, used by the 1971 // compiled source to define methods on special case objects when the compiler 1972 // can not determine the destination object, or the object is a Module 1973 // instance. This can get called by `Module#define_method` as well. 1974 // 1975 // ## Modules 1976 // 1977 // Any method defined on a module will come through this runtime helper. 1978 // The method is added to the module body, and the owner of the method is 1979 // set to be the module itself. This is used later when choosing which 1980 // method should show on a class if more than 1 included modules define 1981 // the same method. Finally, if the module is in `module_function` mode, 1982 // then the method is also defined onto the module itself. 1983 // 1984 // ## Classes 1985 // 1986 // This helper will only be called for classes when a method is being 1987 // defined indirectly; either through `Module#define_method`, or by a 1988 // literal `def` method inside an `instance_eval` or `class_eval` body. In 1989 // either case, the method is simply added to the class' prototype. A special 1990 // exception exists for `BasicObject` and `Object`. These two classes are 1991 // special because they are used in toll-free bridged classes. In each of 1992 // these two cases, extra work is required to define the methods on toll-free 1993 // bridged class' prototypes as well. 1994 // 1995 // ## Objects 1996 // 1997 // If a simple ruby object is the object, then the method is simply just 1998 // defined on the object as a singleton method. This would be the case when 1999 // a method is defined inside an `instance_eval` block. 2000 // 2001 // @param obj [Object, Class] the actual obj to define method for 2002 // @param jsid [String] the JavaScript friendly method name (e.g. '$foo') 2003 // @param body [JS.Function] the literal JavaScript function used as method 2004 // @param blockopts [Object, Number] optional properties to set on the body 2005 // @return [null] 2006 // 2007 Opal.def = function(obj, jsid, body, blockopts) { 2008 apply_blockopts(body, blockopts); 2009 2010 // Special case for a method definition in the 2011 // top-level namespace 2012 if (obj === Opal.top) { 2013 return Opal.defn(Opal.Object, jsid, body); 2014 } 2015 // if instance_eval is invoked on a module/class, it sets inst_eval_mod 2016 else if (!obj.$$eval && obj.$$is_a_module) { 2017 return Opal.defn(obj, jsid, body); 2018 } 2019 else { 2020 return Opal.defs(obj, jsid, body); 2021 } 2022 }; 2023 2024 // Define method on a module or class (see Opal.def). 2025 Opal.defn = function(module, jsid, body) { 2026 $deny_frozen_access(module); 2027 2028 body.displayName = jsid; 2029 body.$$owner = module; 2030 2031 var name = jsid.substr(1); 2032 2033 var proto = module.$$prototype; 2034 if (proto.hasOwnProperty('$$dummy')) { 2035 proto = proto.$$define_methods_on; 2036 } 2037 $prop(proto, jsid, body); 2038 2039 if (module.$$is_module) { 2040 if (module.$$module_function) { 2041 Opal.defs(module, jsid, body) 2042 } 2043 2044 for (var i = 0, iclasses = module.$$iclasses, length = iclasses.length; i < length; i++) { 2045 var iclass = iclasses[i]; 2046 $prop(iclass, jsid, body); 2047 } 2048 } 2049 2050 var singleton_of = module.$$singleton_of; 2051 if (module.$method_added && !module.$method_added.$$stub && !singleton_of) { 2052 module.$method_added(name); 2053 } 2054 else if (singleton_of && singleton_of.$singleton_method_added && !singleton_of.$singleton_method_added.$$stub) { 2055 singleton_of.$singleton_method_added(name); 2056 } 2057 2058 return name; 2059 }; 2060 2061 // Define a singleton method on the given object (see Opal.def). 2062 Opal.defs = function(obj, jsid, body, blockopts) { 2063 apply_blockopts(body, blockopts); 2064 2065 if (obj.$$is_string || obj.$$is_number) { 2066 $raise(Opal.TypeError, "can't define singleton"); 2067 } 2068 return Opal.defn(Opal.get_singleton_class(obj), jsid, body); 2069 }; 2070 2071 // Called from #remove_method. 2072 Opal.rdef = function(obj, jsid) { 2073 if (!$has_own(obj.$$prototype, jsid)) { 2074 $raise(Opal.NameError, "method '" + jsid.substr(1) + "' not defined in " + obj.$name()); 2075 } 2076 2077 delete obj.$$prototype[jsid]; 2078 2079 if (obj.$$is_singleton) { 2080 if (obj.$$prototype.$singleton_method_removed && !obj.$$prototype.$singleton_method_removed.$$stub) { 2081 obj.$$prototype.$singleton_method_removed(jsid.substr(1)); 2082 } 2083 } 2084 else { 2085 if (obj.$method_removed && !obj.$method_removed.$$stub) { 2086 obj.$method_removed(jsid.substr(1)); 2087 } 2088 } 2089 }; 2090 2091 // Called from #undef_method. 2092 Opal.udef = function(obj, jsid) { 2093 if (!obj.$$prototype[jsid] || obj.$$prototype[jsid].$$stub) { 2094 $raise(Opal.NameError, "method '" + jsid.substr(1) + "' not defined in " + obj.$name()); 2095 } 2096 2097 Opal.add_stub_for(obj.$$prototype, jsid); 2098 2099 if (obj.$$is_singleton) { 2100 if (obj.$$prototype.$singleton_method_undefined && !obj.$$prototype.$singleton_method_undefined.$$stub) { 2101 obj.$$prototype.$singleton_method_undefined(jsid.substr(1)); 2102 } 2103 } 2104 else { 2105 if (obj.$method_undefined && !obj.$method_undefined.$$stub) { 2106 obj.$method_undefined(jsid.substr(1)); 2107 } 2108 } 2109 }; 2110 2111 function is_method_body(body) { 2112 return (typeof(body) === "function" && !body.$$stub); 2113 } 2114 2115 Opal.alias = function(obj, name, old) { 2116 var id = $jsid(name), 2117 old_id = $jsid(old), 2118 body, 2119 alias; 2120 2121 // Aliasing on main means aliasing on Object... 2122 if (typeof obj.$$prototype === 'undefined') { 2123 obj = Opal.Object; 2124 } 2125 2126 body = obj.$$prototype[old_id]; 2127 2128 // When running inside #instance_eval the alias refers to class methods. 2129 if (obj.$$eval) { 2130 return Opal.alias(Opal.get_singleton_class(obj), name, old); 2131 } 2132 2133 if (!is_method_body(body)) { 2134 var ancestor = obj.$$super; 2135 2136 while (typeof(body) !== "function" && ancestor) { 2137 body = ancestor[old_id]; 2138 ancestor = ancestor.$$super; 2139 } 2140 2141 if (!is_method_body(body) && obj.$$is_module) { 2142 // try to look into Object 2143 body = Opal.Object.$$prototype[old_id] 2144 } 2145 2146 if (!is_method_body(body)) { 2147 $raise(Opal.NameError, "undefined method `" + old + "' for class `" + obj.$name() + "'") 2148 } 2149 } 2150 2151 // If the body is itself an alias use the original body 2152 // to keep the max depth at 1. 2153 if (body.$$alias_of) body = body.$$alias_of; 2154 2155 // We need a wrapper because otherwise properties 2156 // would be overwritten on the original body. 2157 alias = function() { 2158 var block = alias.$$p, args, i, ii; 2159 2160 args = new Array(arguments.length); 2161 for(i = 0, ii = arguments.length; i < ii; i++) { 2162 args[i] = arguments[i]; 2163 } 2164 2165 alias.$$p = null; 2166 2167 return Opal.send(this, body, args, block); 2168 }; 2169 2170 // Assign the 'length' value with defineProperty because 2171 // in strict mode the property is not writable. 2172 // It doesn't work in older browsers (like Chrome 38), where 2173 // an exception is thrown breaking Opal altogether. 2174 try { 2175 Object.defineProperty(alias, 'length', { value: body.length }); 2176 } catch (e) {} 2177 2178 // Try to make the browser pick the right name 2179 alias.displayName = name; 2180 2181 alias.$$arity = body.$$arity == null ? body.length : body.$$arity; 2182 alias.$$parameters = body.$$parameters; 2183 alias.$$source_location = body.$$source_location; 2184 alias.$$alias_of = body; 2185 alias.$$alias_name = name; 2186 2187 Opal.defn(obj, id, alias); 2188 2189 return obj; 2190 }; 2191 2192 Opal.alias_gvar = function(new_name, old_name) { 2193 Object.defineProperty($gvars, new_name, { 2194 configurable: true, 2195 enumerable: true, 2196 get: function() { 2197 return $gvars[old_name]; 2198 }, 2199 set: function(new_value) { 2200 $gvars[old_name] = new_value; 2201 } 2202 }); 2203 return nil; 2204 } 2205 2206 Opal.alias_native = function(obj, name, native_name) { 2207 var id = $jsid(name), 2208 body = obj.$$prototype[native_name]; 2209 2210 if (typeof(body) !== "function" || body.$$stub) { 2211 $raise(Opal.NameError, "undefined native method `" + native_name + "' for class `" + obj.$name() + "'") 2212 } 2213 2214 Opal.defn(obj, id, body); 2215 2216 return obj; 2217 }; 2218 2219 2220 // Hashes 2221 // ------ 2222 2223 Opal.hash_init = function(hash) { 2224 hash.$$smap = Object.create(null); 2225 hash.$$map = Object.create(null); 2226 hash.$$keys = []; 2227 }; 2228 2229 Opal.hash_clone = function(from_hash, to_hash) { 2230 to_hash.$$none = from_hash.$$none; 2231 to_hash.$$proc = from_hash.$$proc; 2232 2233 for (var i = 0, keys = from_hash.$$keys, smap = from_hash.$$smap, len = keys.length, key, value; i < len; i++) { 2234 key = keys[i]; 2235 2236 if (key.$$is_string) { 2237 value = smap[key]; 2238 } else { 2239 value = key.value; 2240 key = key.key; 2241 } 2242 2243 Opal.hash_put(to_hash, key, value); 2244 } 2245 }; 2246 2247 Opal.hash_put = function(hash, key, value) { 2248 if (key.$$is_string) { 2249 if (!$has_own(hash.$$smap, key)) { 2250 hash.$$keys.push(key); 2251 } 2252 hash.$$smap[key] = value; 2253 return; 2254 } 2255 2256 var key_hash, bucket, last_bucket; 2257 key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash(); 2258 2259 if (!$has_own(hash.$$map, key_hash)) { 2260 bucket = {key: key, key_hash: key_hash, value: value}; 2261 hash.$$keys.push(bucket); 2262 hash.$$map[key_hash] = bucket; 2263 return; 2264 } 2265 2266 bucket = hash.$$map[key_hash]; 2267 2268 while (bucket) { 2269 if (key === bucket.key || key['$eql?'](bucket.key)) { 2270 last_bucket = undefined; 2271 bucket.value = value; 2272 break; 2273 } 2274 last_bucket = bucket; 2275 bucket = bucket.next; 2276 } 2277 2278 if (last_bucket) { 2279 bucket = {key: key, key_hash: key_hash, value: value}; 2280 hash.$$keys.push(bucket); 2281 last_bucket.next = bucket; 2282 } 2283 }; 2284 2285 Opal.hash_get = function(hash, key) { 2286 if (key.$$is_string) { 2287 if ($has_own(hash.$$smap, key)) { 2288 return hash.$$smap[key]; 2289 } 2290 return; 2291 } 2292 2293 var key_hash, bucket; 2294 key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash(); 2295 2296 if ($has_own(hash.$$map, key_hash)) { 2297 bucket = hash.$$map[key_hash]; 2298 2299 while (bucket) { 2300 if (key === bucket.key || key['$eql?'](bucket.key)) { 2301 return bucket.value; 2302 } 2303 bucket = bucket.next; 2304 } 2305 } 2306 }; 2307 2308 Opal.hash_delete = function(hash, key) { 2309 var i, keys = hash.$$keys, length = keys.length, value, key_tmp; 2310 2311 if (key.$$is_string) { 2312 if (typeof key !== "string") key = key.valueOf(); 2313 2314 if (!$has_own(hash.$$smap, key)) { 2315 return; 2316 } 2317 2318 for (i = 0; i < length; i++) { 2319 key_tmp = keys[i]; 2320 2321 if (key_tmp.$$is_string && typeof key_tmp !== "string") { 2322 key_tmp = key_tmp.valueOf(); 2323 } 2324 2325 if (key_tmp === key) { 2326 keys.splice(i, 1); 2327 break; 2328 } 2329 } 2330 2331 value = hash.$$smap[key]; 2332 delete hash.$$smap[key]; 2333 return value; 2334 } 2335 2336 var key_hash = key.$hash(); 2337 2338 if (!$has_own(hash.$$map, key_hash)) { 2339 return; 2340 } 2341 2342 var bucket = hash.$$map[key_hash], last_bucket; 2343 2344 while (bucket) { 2345 if (key === bucket.key || key['$eql?'](bucket.key)) { 2346 value = bucket.value; 2347 2348 for (i = 0; i < length; i++) { 2349 if (keys[i] === bucket) { 2350 keys.splice(i, 1); 2351 break; 2352 } 2353 } 2354 2355 if (last_bucket && bucket.next) { 2356 last_bucket.next = bucket.next; 2357 } 2358 else if (last_bucket) { 2359 delete last_bucket.next; 2360 } 2361 else if (bucket.next) { 2362 hash.$$map[key_hash] = bucket.next; 2363 } 2364 else { 2365 delete hash.$$map[key_hash]; 2366 } 2367 2368 return value; 2369 } 2370 last_bucket = bucket; 2371 bucket = bucket.next; 2372 } 2373 }; 2374 2375 Opal.hash_rehash = function(hash) { 2376 for (var i = 0, length = hash.$$keys.length, key_hash, bucket, last_bucket; i < length; i++) { 2377 2378 if (hash.$$keys[i].$$is_string) { 2379 continue; 2380 } 2381 2382 key_hash = hash.$$keys[i].key.$hash(); 2383 2384 if (key_hash === hash.$$keys[i].key_hash) { 2385 continue; 2386 } 2387 2388 bucket = hash.$$map[hash.$$keys[i].key_hash]; 2389 last_bucket = undefined; 2390 2391 while (bucket) { 2392 if (bucket === hash.$$keys[i]) { 2393 if (last_bucket && bucket.next) { 2394 last_bucket.next = bucket.next; 2395 } 2396 else if (last_bucket) { 2397 delete last_bucket.next; 2398 } 2399 else if (bucket.next) { 2400 hash.$$map[hash.$$keys[i].key_hash] = bucket.next; 2401 } 2402 else { 2403 delete hash.$$map[hash.$$keys[i].key_hash]; 2404 } 2405 break; 2406 } 2407 last_bucket = bucket; 2408 bucket = bucket.next; 2409 } 2410 2411 hash.$$keys[i].key_hash = key_hash; 2412 2413 if (!$has_own(hash.$$map, key_hash)) { 2414 hash.$$map[key_hash] = hash.$$keys[i]; 2415 continue; 2416 } 2417 2418 bucket = hash.$$map[key_hash]; 2419 last_bucket = undefined; 2420 2421 while (bucket) { 2422 if (bucket === hash.$$keys[i]) { 2423 last_bucket = undefined; 2424 break; 2425 } 2426 last_bucket = bucket; 2427 bucket = bucket.next; 2428 } 2429 2430 if (last_bucket) { 2431 last_bucket.next = hash.$$keys[i]; 2432 } 2433 } 2434 }; 2435 2436 Opal.hash = function() { 2437 var arguments_length = arguments.length, args, hash, i, length, key, value; 2438 2439 if (arguments_length === 1 && arguments[0].$$is_hash) { 2440 return arguments[0]; 2441 } 2442 2443 hash = new Opal.Hash(); 2444 Opal.hash_init(hash); 2445 2446 if (arguments_length === 1) { 2447 args = arguments[0]; 2448 2449 if (arguments[0].$$is_array) { 2450 length = args.length; 2451 2452 for (i = 0; i < length; i++) { 2453 if (args[i].length !== 2) { 2454 $raise(Opal.ArgumentError, "value not of length 2: " + args[i].$inspect()); 2455 } 2456 2457 key = args[i][0]; 2458 value = args[i][1]; 2459 2460 Opal.hash_put(hash, key, value); 2461 } 2462 2463 return hash; 2464 } 2465 else { 2466 args = arguments[0]; 2467 for (key in args) { 2468 if ($has_own(args, key)) { 2469 value = args[key]; 2470 2471 Opal.hash_put(hash, key, value); 2472 } 2473 } 2474 2475 return hash; 2476 } 2477 } 2478 2479 if (arguments_length % 2 !== 0) { 2480 $raise(Opal.ArgumentError, "odd number of arguments for Hash"); 2481 } 2482 2483 for (i = 0; i < arguments_length; i += 2) { 2484 key = arguments[i]; 2485 value = arguments[i + 1]; 2486 2487 Opal.hash_put(hash, key, value); 2488 } 2489 2490 return hash; 2491 }; 2492 2493 // A faster Hash creator for hashes that just use symbols and 2494 // strings as keys. The map and keys array can be constructed at 2495 // compile time, so they are just added here by the constructor 2496 // function. 2497 // 2498 Opal.hash2 = function(keys, smap) { 2499 var hash = new Opal.Hash(); 2500 2501 hash.$$smap = smap; 2502 hash.$$map = Object.create(null); 2503 hash.$$keys = keys; 2504 2505 return hash; 2506 }; 2507 2508 // Create a new range instance with first and last values, and whether the 2509 // range excludes the last value. 2510 // 2511 Opal.range = function(first, last, exc) { 2512 var range = new Opal.Range(); 2513 range.begin = first; 2514 range.end = last; 2515 range.excl = exc; 2516 2517 return range; 2518 }; 2519 2520 var reserved_ivar_names = [ 2521 // properties 2522 "constructor", "displayName", "__count__", "__noSuchMethod__", 2523 "__parent__", "__proto__", 2524 // methods 2525 "hasOwnProperty", "valueOf" 2526 ]; 2527 2528 // Get the ivar name for a given name. 2529 // Mostly adds a trailing $ to reserved names. 2530 // 2531 Opal.ivar = function(name) { 2532 if (reserved_ivar_names.indexOf(name) !== -1) { 2533 name += "$"; 2534 } 2535 2536 return name; 2537 }; 2538 2539 // Support for #freeze 2540 // ------------------- 2541 2542 // helper that can be used from methods 2543 function $deny_frozen_access(obj) { 2544 if (obj.$$frozen) { 2545 $raise(Opal.FrozenError, "can't modify frozen " + (obj.$class()) + ": " + (obj), Opal.hash2(["receiver"], {"receiver": obj})); 2546 } 2547 }; 2548 Opal.deny_frozen_access = $deny_frozen_access; 2549 2550 // common #freeze runtime support 2551 Opal.freeze = function(obj) { 2552 $prop(obj, "$$frozen", true); 2553 2554 // set $$id 2555 if (!obj.hasOwnProperty('$$id')) { $prop(obj, '$$id', $uid()); } 2556 2557 if (obj.hasOwnProperty('$$meta')) { 2558 // freeze $$meta if it has already been set 2559 obj.$$meta.$freeze(); 2560 } else { 2561 // ensure $$meta can be set lazily, $$meta is frozen when set in runtime.js 2562 $prop(obj, '$$meta', null); 2563 } 2564 2565 // $$comparable is used internally and set multiple times 2566 // defining it before sealing ensures it can be modified later on 2567 if (!obj.hasOwnProperty('$$comparable')) { $prop(obj, '$$comparable', null); } 2568 2569 // seal the Object 2570 Object.seal(obj); 2571 2572 return obj; 2573 }; 2574 2575 // freze props, make setters of instance variables throw FrozenError 2576 Opal.freeze_props = function(obj) { 2577 var prop, prop_type, desc; 2578 2579 for(prop in obj) { 2580 prop_type = typeof(prop); 2581 2582 // prop_type "object" here is a String(), skip $ props 2583 if ((prop_type === "string" || prop_type === "object") && prop[0] === '$') { 2584 continue; 2585 } 2586 2587 desc = Object.getOwnPropertyDescriptor(obj, prop); 2588 if (desc && desc.enumerable && desc.writable) { 2589 // create closure to retain current value as cv 2590 // for Opal 2.0 let for cv should do the trick, instead of a function 2591 (function() { 2592 // set v to undefined, as if the property is not set 2593 var cv = obj[prop]; 2594 Object.defineProperty(obj, prop, { 2595 get: function() { return cv; }, 2596 set: function(_val) { $deny_frozen_access(obj); }, 2597 enumerable: true 2598 }); 2599 })(); 2600 } 2601 } 2602 }; 2603 2604 // Regexps 2605 // ------- 2606 2607 // Escape Regexp special chars letting the resulting string be used to build 2608 // a new Regexp. 2609 // 2610 Opal.escape_regexp = function(str) { 2611 return str.replace(/([-[\]\/{}()*+?.^$\\| ])/g, '\\$1') 2612 .replace(/[\n]/g, '\\n') 2613 .replace(/[\r]/g, '\\r') 2614 .replace(/[\f]/g, '\\f') 2615 .replace(/[\t]/g, '\\t'); 2616 }; 2617 2618 // Create a global Regexp from a RegExp object and cache the result 2619 // on the object itself ($$g attribute). 2620 // 2621 Opal.global_regexp = function(pattern) { 2622 if (pattern.global) { 2623 return pattern; // RegExp already has the global flag 2624 } 2625 if (pattern.$$g == null) { 2626 pattern.$$g = new RegExp(pattern.source, (pattern.multiline ? 'gm' : 'g') + (pattern.ignoreCase ? 'i' : '')); 2627 } else { 2628 pattern.$$g.lastIndex = null; // reset lastIndex property 2629 } 2630 return pattern.$$g; 2631 }; 2632 2633 // Create a global multiline Regexp from a RegExp object and cache the result 2634 // on the object itself ($$gm or $$g attribute). 2635 // 2636 Opal.global_multiline_regexp = function(pattern) { 2637 var result, flags; 2638 2639 // RegExp already has the global and multiline flag 2640 if (pattern.global && pattern.multiline) return pattern; 2641 2642 flags = 'gm' + (pattern.ignoreCase ? 'i' : ''); 2643 if (pattern.multiline) { 2644 // we are using the $$g attribute because the Regexp is already multiline 2645 if (pattern.$$g == null) { 2646 pattern.$$g = new RegExp(pattern.source, flags); 2647 } 2648 result = pattern.$$g; 2649 } else { 2650 if (pattern.$$gm == null) { 2651 pattern.$$gm = new RegExp(pattern.source, flags); 2652 } 2653 result = pattern.$$gm; 2654 } 2655 result.lastIndex = null; // reset lastIndex property 2656 return result; 2657 }; 2658 2659 // Combine multiple regexp parts together 2660 Opal.regexp = function(parts, flags) { 2661 var part; 2662 var ignoreCase = typeof flags !== 'undefined' && flags && flags.indexOf('i') >= 0; 2663 2664 for (var i = 0, ii = parts.length; i < ii; i++) { 2665 part = parts[i]; 2666 if (part instanceof RegExp) { 2667 if (part.ignoreCase !== ignoreCase) 2668 Opal.Kernel.$warn( 2669 "ignore case doesn't match for " + part.source.$inspect(), 2670 Opal.hash({uplevel: 1}) 2671 ) 2672 2673 part = part.source; 2674 } 2675 if (part === '') part = '(?:' + part + ')'; 2676 parts[i] = part; 2677 } 2678 2679 if (flags) { 2680 return new RegExp(parts.join(''), flags); 2681 } else { 2682 return new RegExp(parts.join('')); 2683 } 2684 }; 2685 2686 // Require system 2687 // -------------- 2688 2689 Opal.modules = {}; 2690 Opal.loaded_features = ['corelib/runtime']; 2691 Opal.current_dir = '.'; 2692 Opal.require_table = {'corelib/runtime': true}; 2693 2694 Opal.normalize = function(path) { 2695 var parts, part, new_parts = [], SEPARATOR = '/'; 2696 2697 if (Opal.current_dir !== '.') { 2698 path = Opal.current_dir.replace(/\/*$/, '/') + path; 2699 } 2700 2701 path = path.replace(/^\.\//, ''); 2702 path = path.replace(/\.(rb|opal|js)$/, ''); 2703 parts = path.split(SEPARATOR); 2704 2705 for (var i = 0, ii = parts.length; i < ii; i++) { 2706 part = parts[i]; 2707 if (part === '') continue; 2708 (part === '..') ? new_parts.pop() : new_parts.push(part) 2709 } 2710 2711 return new_parts.join(SEPARATOR); 2712 }; 2713 2714 Opal.loaded = function(paths) { 2715 var i, l, path; 2716 2717 for (i = 0, l = paths.length; i < l; i++) { 2718 path = Opal.normalize(paths[i]); 2719 2720 if (Opal.require_table[path]) { 2721 continue; 2722 } 2723 2724 Opal.loaded_features.push(path); 2725 Opal.require_table[path] = true; 2726 } 2727 }; 2728 2729 Opal.load_normalized = function(path) { 2730 Opal.loaded([path]); 2731 2732 var module = Opal.modules[path]; 2733 2734 if (module) { 2735 var retval = module(Opal); 2736 if (typeof Promise !== 'undefined' && retval instanceof Promise) { 2737 // A special case of require having an async top: 2738 // We will need to await it. 2739 return retval.then($return_val(true)); 2740 } 2741 } 2742 else { 2743 var severity = Opal.config.missing_require_severity; 2744 var message = 'cannot load such file -- ' + path; 2745 2746 if (severity === "error") { 2747 $raise(Opal.LoadError, message); 2748 } 2749 else if (severity === "warning") { 2750 console.warn('WARNING: LoadError: ' + message); 2751 } 2752 } 2753 2754 return true; 2755 }; 2756 2757 Opal.load = function(path) { 2758 path = Opal.normalize(path); 2759 2760 return Opal.load_normalized(path); 2761 }; 2762 2763 Opal.require = function(path) { 2764 path = Opal.normalize(path); 2765 2766 if (Opal.require_table[path]) { 2767 return false; 2768 } 2769 2770 return Opal.load_normalized(path); 2771 }; 2772 2773 2774 // Strings 2775 // ------- 2776 2777 Opal.encodings = Object.create(null); 2778 2779 // Sets the encoding on a string, will treat string literals as frozen strings 2780 // raising a FrozenError. 2781 // 2782 // @param str [String] the string on which the encoding should be set 2783 // @param name [String] the canonical name of the encoding 2784 // @param type [String] possible values are either `"encoding"`, `"internal_encoding"`, or `undefined 2785 Opal.set_encoding = function(str, name, type) { 2786 if (typeof type === "undefined") type = "encoding"; 2787 if (typeof str === 'string' || str.$$frozen === true) 2788 $raise(Opal.FrozenError, "can't modify frozen String"); 2789 2790 var encoding = Opal.find_encoding(name); 2791 2792 if (encoding === str[type]) { return str; } 2793 2794 str[type] = encoding; 2795 2796 return str; 2797 }; 2798 2799 // Fetches the encoding for the given name or raises ArgumentError. 2800 Opal.find_encoding = function(name) { 2801 var register = Opal.encodings; 2802 var encoding = register[name] || register[name.toUpperCase()]; 2803 if (!encoding) $raise(Opal.ArgumentError, "unknown encoding name - " + name); 2804 return encoding; 2805 } 2806 2807 // @returns a String object with the encoding set from a string literal 2808 Opal.enc = function(str, name) { 2809 var dup = new String(str); 2810 dup = Opal.set_encoding(dup, name); 2811 dup.internal_encoding = dup.encoding; 2812 return dup 2813 } 2814 2815 // @returns a String object with the internal encoding set to Binary 2816 Opal.binary = function(str) { 2817 var dup = new String(str); 2818 return Opal.set_encoding(dup, "binary", "internal_encoding"); 2819 } 2820 2821 Opal.last_promise = null; 2822 Opal.promise_unhandled_exception = false; 2823 2824 // Run a block of code, but if it returns a Promise, don't run the next 2825 // one, but queue it. 2826 Opal.queue = function(proc) { 2827 if (Opal.last_promise) { 2828 // The async path is taken only if anything before returned a 2829 // Promise(V2). 2830 Opal.last_promise = Opal.last_promise.then(function() { 2831 if (!Opal.promise_unhandled_exception) return proc(Opal); 2832 })['catch'](function(error) { 2833 if (Opal.respond_to(error, '$full_message')) { 2834 error = error.$full_message(); 2835 } 2836 console.error(error); 2837 // Abort further execution 2838 Opal.promise_unhandled_exception = true; 2839 Opal.exit(1); 2840 }); 2841 return Opal.last_promise; 2842 } 2843 else { 2844 var ret = proc(Opal); 2845 if (typeof Promise === 'function' && typeof ret === 'object' && ret instanceof Promise) { 2846 Opal.last_promise = ret; 2847 } 2848 return ret; 2849 } 2850 } 2851 2852 // Operator helpers 2853 // ---------------- 2854 2855 function are_both_numbers(l,r) { return typeof(l) === 'number' && typeof(r) === 'number' } 2856 2857 Opal.rb_plus = function(l,r) { return are_both_numbers(l,r) ? l + r : l['$+'](r); } 2858 Opal.rb_minus = function(l,r) { return are_both_numbers(l,r) ? l - r : l['$-'](r); } 2859 Opal.rb_times = function(l,r) { return are_both_numbers(l,r) ? l * r : l['$*'](r); } 2860 Opal.rb_divide = function(l,r) { return are_both_numbers(l,r) ? l / r : l['$/'](r); } 2861 Opal.rb_lt = function(l,r) { return are_both_numbers(l,r) ? l < r : l['$<'](r); } 2862 Opal.rb_gt = function(l,r) { return are_both_numbers(l,r) ? l > r : l['$>'](r); } 2863 Opal.rb_le = function(l,r) { return are_both_numbers(l,r) ? l <= r : l['$<='](r); } 2864 Opal.rb_ge = function(l,r) { return are_both_numbers(l,r) ? l >= r : l['$>='](r); } 2865 2866 // Optimized helpers for calls like $truthy((a)['$==='](b)) -> $eqeqeq(a, b) 2867 function are_both_numbers_or_strings(lhs, rhs) { 2868 return (typeof lhs === 'number' && typeof rhs === 'number') || 2869 (typeof lhs === 'string' && typeof rhs === 'string'); 2870 } 2871 2872 function $eqeq(lhs, rhs) { 2873 return are_both_numbers_or_strings(lhs,rhs) ? lhs === rhs : $truthy((lhs)['$=='](rhs)); 2874 }; 2875 Opal.eqeq = $eqeq; 2876 Opal.eqeqeq = function(lhs, rhs) { 2877 return are_both_numbers_or_strings(lhs,rhs) ? lhs === rhs : $truthy((lhs)['$==='](rhs)); 2878 }; 2879 Opal.neqeq = function(lhs, rhs) { 2880 return are_both_numbers_or_strings(lhs,rhs) ? lhs !== rhs : $truthy((lhs)['$!='](rhs)); 2881 }; 2882 Opal.not = function(arg) { 2883 if (undefined === arg || null === arg || false === arg || nil === arg) return true; 2884 if (true === arg || arg['$!'].$$pristine) return false; 2885 return $truthy(arg['$!']()); 2886 } 2887 2888 // Shortcuts - optimized function generators for simple kinds of functions 2889 function $return_val(arg) { 2890 return function() { 2891 return arg; 2892 } 2893 } 2894 Opal.return_val = $return_val; 2895 2896 Opal.return_self = function() { 2897 return this; 2898 } 2899 Opal.return_ivar = function(ivar) { 2900 return function() { 2901 if (this[ivar] == null) { return nil; } 2902 return this[ivar]; 2903 } 2904 } 2905 Opal.assign_ivar = function(ivar) { 2906 return function(val) { 2907 $deny_frozen_access(this); 2908 return this[ivar] = val; 2909 } 2910 } 2911 Opal.assign_ivar_val = function(ivar, static_val) { 2912 return function() { 2913 $deny_frozen_access(this); 2914 return this[ivar] = static_val; 2915 } 2916 } 2917 2918 // Primitives for handling parameters 2919 Opal.ensure_kwargs = function(kwargs) { 2920 if (kwargs == null) { 2921 return Opal.hash2([], {}); 2922 } else if (kwargs.$$is_hash) { 2923 return kwargs; 2924 } else { 2925 $raise(Opal.ArgumentError, 'expected kwargs'); 2926 } 2927 } 2928 2929 Opal.get_kwarg = function(kwargs, key) { 2930 if (!$has_own(kwargs.$$smap, key)) { 2931 $raise(Opal.ArgumentError, 'missing keyword: '+key); 2932 } 2933 return kwargs.$$smap[key]; 2934 } 2935 2936 // Arrays of size > 32 elements that contain only strings, 2937 // symbols, integers and nils are compiled as a self-extracting 2938 // string. 2939 Opal.large_array_unpack = function(str) { 2940 var array = str.split(","), length = array.length, i; 2941 for (i = 0; i < length; i++) { 2942 switch(array[i][0]) { 2943 case undefined: 2944 array[i] = nil 2945 break; 2946 case '-': 2947 case '0': 2948 case '1': 2949 case '2': 2950 case '3': 2951 case '4': 2952 case '5': 2953 case '6': 2954 case '7': 2955 case '8': 2956 case '9': 2957 array[i] = +array[i]; 2958 } 2959 } 2960 return array; 2961 } 2962 2963 // Initialization 2964 // -------------- 2965 Opal.BasicObject = BasicObject = $allocate_class('BasicObject', null); 2966 Opal.Object = _Object = $allocate_class('Object', Opal.BasicObject); 2967 Opal.Module = Module = $allocate_class('Module', Opal.Object); 2968 Opal.Class = Class = $allocate_class('Class', Opal.Module); 2969 Opal.Opal = _Opal = $allocate_module('Opal'); 2970 Opal.Kernel = Kernel = $allocate_module('Kernel'); 2971 2972 $set_proto(Opal.BasicObject, Opal.Class.$$prototype); 2973 $set_proto(Opal.Object, Opal.Class.$$prototype); 2974 $set_proto(Opal.Module, Opal.Class.$$prototype); 2975 $set_proto(Opal.Class, Opal.Class.$$prototype); 2976 2977 // BasicObject can reach itself, avoid const_set to skip the $$base_module logic 2978 BasicObject.$$const.BasicObject = BasicObject; 2979 2980 // Assign basic constants 2981 $const_set(_Object, "BasicObject", BasicObject); 2982 $const_set(_Object, "Object", _Object); 2983 $const_set(_Object, "Module", Module); 2984 $const_set(_Object, "Class", Class); 2985 $const_set(_Object, "Opal", _Opal); 2986 $const_set(_Object, "Kernel", Kernel); 2987 2988 // Fix booted classes to have correct .class value 2989 BasicObject.$$class = Class; 2990 _Object.$$class = Class; 2991 Module.$$class = Class; 2992 Class.$$class = Class; 2993 _Opal.$$class = Module; 2994 Kernel.$$class = Module; 2995 2996 // Forward .toString() to #to_s 2997 $prop(_Object.$$prototype, 'toString', function() { 2998 var to_s = this.$to_s(); 2999 if (to_s.$$is_string && typeof(to_s) === 'object') { 3000 // a string created using new String('string') 3001 return to_s.valueOf(); 3002 } else { 3003 return to_s; 3004 } 3005 }); 3006 3007 // Make Kernel#require immediately available as it's needed to require all the 3008 // other corelib files. 3009 $prop(_Object.$$prototype, '$require', Opal.require); 3010 3011 // Instantiate the main object 3012 Opal.top = new _Object(); 3013 Opal.top.$to_s = Opal.top.$inspect = $return_val('main'); 3014 Opal.top.$define_method = top_define_method; 3015 3016 // Foward calls to define_method on the top object to Object 3017 function top_define_method() { 3018 var args = $slice(arguments); 3019 var block = top_define_method.$$p; 3020 top_define_method.$$p = null; 3021 return Opal.send(_Object, 'define_method', args, block) 3022 }; 3023 3024 // Nil 3025 Opal.NilClass = $allocate_class('NilClass', Opal.Object); 3026 $const_set(_Object, 'NilClass', Opal.NilClass); 3027 nil = Opal.nil = new Opal.NilClass(); 3028 nil.$$id = nil_id; 3029 nil.call = nil.apply = function() { $raise(Opal.LocalJumpError, 'no block given'); }; 3030 nil.$$frozen = true; 3031 nil.$$comparable = false; 3032 Object.seal(nil); 3033 3034 Opal.thrower = function(type) { 3035 var thrower = new Error('unexpected '+type); 3036 thrower.$thrower_type = type; 3037 thrower.$throw = function(value) { 3038 if (value == null) value = nil; 3039 thrower.$v = value; 3040 throw thrower; 3041 }; 3042 return thrower; 3043 }; 3044 3045 Opal.t_eval_return = Opal.thrower("return"); 3046 3047 TypeError.$$super = Error; 3048 3049 // If enable-file-source-embed compiler option is enabled, each module loaded will add its 3050 // sources to this object 3051 Opal.file_sources = {}; 3052}).call(this); 3053Opal.loaded(["corelib/runtime.js"]); 3054Opal.modules["corelib/helpers"] = function(Opal) {/* Generated by Opal 1.7.3 */ 3055 var $type_error = Opal.type_error, $coerce_to = Opal.coerce_to, $module = Opal.module, $defs = Opal.defs, $slice = Opal.slice, $eqeqeq = Opal.eqeqeq, $Kernel = Opal.Kernel, $truthy = Opal.truthy, $Opal = Opal.Opal, nil = Opal.nil, $$$ = Opal.$$$; 3056 3057 Opal.add_stubs('===,raise,respond_to?,nil?,__send__,<=>,class,coerce_to!,new,to_s,__id__'); 3058 return (function($base) { 3059 var self = $module($base, 'Opal'); 3060 3061 3062 3063 $defs(self, '$bridge', function $$bridge(constructor, klass) { 3064 3065 return Opal.bridge(constructor, klass); 3066 }); 3067 $defs(self, '$coerce_to!', function $Opal_coerce_to$excl$1(object, type, method, $a) { 3068 var $post_args, args, coerced = nil; 3069 3070 3071 $post_args = $slice(arguments, 3); 3072 args = $post_args; 3073 coerced = $coerce_to(object, type, method, args); 3074 if (!$eqeqeq(type, coerced)) { 3075 $Kernel.$raise($type_error(object, type, method, coerced)) 3076 }; 3077 return coerced; 3078 }, -4); 3079 $defs(self, '$coerce_to?', function $Opal_coerce_to$ques$2(object, type, method, $a) { 3080 var $post_args, args, coerced = nil; 3081 3082 3083 $post_args = $slice(arguments, 3); 3084 args = $post_args; 3085 if (!$truthy(object['$respond_to?'](method))) { 3086 return nil 3087 }; 3088 coerced = $coerce_to(object, type, method, args); 3089 if ($truthy(coerced['$nil?']())) { 3090 return nil 3091 }; 3092 if (!$eqeqeq(type, coerced)) { 3093 $Kernel.$raise($type_error(object, type, method, coerced)) 3094 }; 3095 return coerced; 3096 }, -4); 3097 $defs(self, '$try_convert', function $$try_convert(object, type, method) { 3098 3099 3100 if ($eqeqeq(type, object)) { 3101 return object 3102 }; 3103 if ($truthy(object['$respond_to?'](method))) { 3104 return object.$__send__(method) 3105 } else { 3106 return nil 3107 }; 3108 }); 3109 $defs(self, '$compare', function $$compare(a, b) { 3110 var compare = nil; 3111 3112 3113 compare = a['$<=>'](b); 3114 if ($truthy(compare === nil)) { 3115 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (a.$class()) + " with " + (b.$class()) + " failed") 3116 }; 3117 return compare; 3118 }); 3119 $defs(self, '$destructure', function $$destructure(args) { 3120 3121 3122 if (args.length == 1) { 3123 return args[0]; 3124 } 3125 else if (args.$$is_array) { 3126 return args; 3127 } 3128 else { 3129 var args_ary = new Array(args.length); 3130 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } 3131 3132 return args_ary; 3133 } 3134 3135 }); 3136 $defs(self, '$respond_to?', function $Opal_respond_to$ques$3(obj, method, include_all) { 3137 3138 3139 if (include_all == null) include_all = false; 3140 3141 if (obj == null || !obj.$$class) { 3142 return false; 3143 } 3144 ; 3145 return obj['$respond_to?'](method, include_all); 3146 }, -3); 3147 $defs(self, '$instance_variable_name!', function $Opal_instance_variable_name$excl$4(name) { 3148 3149 3150 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 3151 if (!$truthy(/^@[a-zA-Z_][a-zA-Z0-9_]*?$/.test(name))) { 3152 $Kernel.$raise($$$('NameError').$new("'" + (name) + "' is not allowed as an instance variable name", name)) 3153 }; 3154 return name; 3155 }); 3156 $defs(self, '$class_variable_name!', function $Opal_class_variable_name$excl$5(name) { 3157 3158 3159 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 3160 if ($truthy(name.length < 3 || name.slice(0,2) !== '@@')) { 3161 $Kernel.$raise($$$('NameError').$new("`" + (name) + "' is not allowed as a class variable name", name)) 3162 }; 3163 return name; 3164 }); 3165 $defs(self, '$const_name?', function $Opal_const_name$ques$6(const_name) { 3166 3167 3168 if (typeof const_name !== 'string') { 3169 (const_name = $Opal['$coerce_to!'](const_name, $$$('String'), "to_str")) 3170 } 3171 3172 return const_name[0] === const_name[0].toUpperCase() 3173 3174 }); 3175 $defs(self, '$const_name!', function $Opal_const_name$excl$7(const_name) { 3176 var $a, self = this; 3177 3178 3179 if ($truthy((($a = $$$('::', 'String', 'skip_raise')) ? 'constant' : nil))) { 3180 const_name = $Opal['$coerce_to!'](const_name, $$$('String'), "to_str") 3181 }; 3182 3183 if (!const_name || const_name[0] != const_name[0].toUpperCase()) { 3184 self.$raise($$$('NameError'), "wrong constant name " + (const_name)) 3185 } 3186 ; 3187 return const_name; 3188 }); 3189 $defs(self, '$pristine', function $$pristine(owner_class, $a) { 3190 var $post_args, method_names; 3191 3192 3193 $post_args = $slice(arguments, 1); 3194 method_names = $post_args; 3195 3196 var method_name, method; 3197 for (var i = method_names.length - 1; i >= 0; i--) { 3198 method_name = method_names[i]; 3199 method = owner_class.$$prototype[Opal.jsid(method_name)]; 3200 3201 if (method && !method.$$stub) { 3202 method.$$pristine = true; 3203 } 3204 } 3205 ; 3206 return nil; 3207 }, -2); 3208 var inspect_stack = []; 3209 return $defs(self, '$inspect', function $$inspect(value) { 3210 var e = nil; 3211 3212 3213 ; 3214 var pushed = false; 3215 3216 return (function() { try { 3217 try { 3218 3219 3220 if (value === null) { 3221 // JS null value 3222 return 'null'; 3223 } 3224 else if (value === undefined) { 3225 // JS undefined value 3226 return 'undefined'; 3227 } 3228 else if (typeof value.$$class === 'undefined') { 3229 // JS object / other value that is not bridged 3230 return Object.prototype.toString.apply(value); 3231 } 3232 else if (typeof value.$inspect !== 'function' || value.$inspect.$$stub) { 3233 // BasicObject and friends 3234 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3235 } 3236 else if (inspect_stack.indexOf(value.$__id__()) !== -1) { 3237 // inspect recursing inside inspect to find out about the 3238 // same object 3239 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3240 } 3241 else { 3242 // anything supporting Opal 3243 inspect_stack.push(value.$__id__()); 3244 pushed = true; 3245 return value.$inspect(); 3246 } 3247 ; 3248 return nil; 3249 } catch ($err) { 3250 if (Opal.rescue($err, [$$$('Exception')])) {(e = $err) 3251 try { 3252 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3253 } finally { Opal.pop_exception(); } 3254 } else { throw $err; } 3255 } 3256 } finally { 3257 if (pushed) inspect_stack.pop() 3258 }; })();; 3259 }, -1); 3260 })('::') 3261}; 3262 3263Opal.modules["corelib/module"] = function(Opal) {/* Generated by Opal 1.7.3 */ 3264 var $truthy = Opal.truthy, $coerce_to = Opal.coerce_to, $const_set = Opal.const_set, $Object = Opal.Object, $return_ivar = Opal.return_ivar, $assign_ivar = Opal.assign_ivar, $ivar = Opal.ivar, $deny_frozen_access = Opal.deny_frozen_access, $freeze = Opal.freeze, $prop = Opal.prop, $jsid = Opal.jsid, $klass = Opal.klass, $defs = Opal.defs, $send = Opal.send, $def = Opal.def, $eqeqeq = Opal.eqeqeq, $Module = Opal.Module, $Kernel = Opal.Kernel, $rb_lt = Opal.rb_lt, $rb_gt = Opal.rb_gt, $slice = Opal.slice, $to_a = Opal.to_a, $hash2 = Opal.hash2, $Opal = Opal.Opal, $return_val = Opal.return_val, $eqeq = Opal.eqeq, $lambda = Opal.lambda, $range = Opal.range, $send2 = Opal.send2, $find_super = Opal.find_super, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 3265 3266 Opal.add_stubs('module_eval,to_proc,===,raise,equal?,<,>,nil?,attr_reader,attr_writer,warn,attr_accessor,const_name?,class_variable_name!,pristine,const_name!,=~,new,inject,split,const_get,==,start_with?,!~,bind,call,class,frozen?,name,append_features,included,cover?,size,merge,compile,proc,any?,prepend_features,prepended,to_s,__id__,constants,include?,copy_class_variables,copy_constants,class_exec,module_exec,inspect'); 3267 3268 (function($base, $super, $parent_nesting) { 3269 var self = $klass($base, $super, 'Module'); 3270 3271 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 3272 3273 3274 $defs(self, '$allocate', function $$allocate() { 3275 var self = this; 3276 3277 3278 var module = Opal.allocate_module(nil, function(){}); 3279 // Link the prototype of Module subclasses 3280 if (self !== Opal.Module) Object.setPrototypeOf(module, self.$$prototype); 3281 return module; 3282 3283 }); 3284 3285 $def(self, '$initialize', function $$initialize() { 3286 var block = $$initialize.$$p || nil, self = this; 3287 3288 $$initialize.$$p = null; 3289 3290 ; 3291 if ((block !== nil)) { 3292 return $send(self, 'module_eval', [], block.$to_proc()) 3293 } else { 3294 return nil 3295 }; 3296 }); 3297 3298 $def(self, '$===', function $Module_$eq_eq_eq$1(object) { 3299 var self = this; 3300 3301 3302 if ($truthy(object == null)) { 3303 return false 3304 }; 3305 return Opal.is_a(object, self);; 3306 }); 3307 3308 $def(self, '$<', function $Module_$lt$2(other) { 3309 var self = this; 3310 3311 3312 if (!$eqeqeq($Module, other)) { 3313 $Kernel.$raise($$$('TypeError'), "compared with non class/module") 3314 }; 3315 3316 var working = self, 3317 ancestors, 3318 i, length; 3319 3320 if (working === other) { 3321 return false; 3322 } 3323 3324 for (i = 0, ancestors = Opal.ancestors(self), length = ancestors.length; i < length; i++) { 3325 if (ancestors[i] === other) { 3326 return true; 3327 } 3328 } 3329 3330 for (i = 0, ancestors = Opal.ancestors(other), length = ancestors.length; i < length; i++) { 3331 if (ancestors[i] === self) { 3332 return false; 3333 } 3334 } 3335 3336 return nil; 3337 ; 3338 }); 3339 3340 $def(self, '$<=', function $Module_$lt_eq$3(other) { 3341 var self = this, $ret_or_1 = nil; 3342 3343 if ($truthy(($ret_or_1 = self['$equal?'](other)))) { 3344 return $ret_or_1 3345 } else { 3346 return $rb_lt(self, other) 3347 } 3348 }); 3349 3350 $def(self, '$>', function $Module_$gt$4(other) { 3351 var self = this; 3352 3353 3354 if (!$eqeqeq($Module, other)) { 3355 $Kernel.$raise($$$('TypeError'), "compared with non class/module") 3356 }; 3357 return $rb_lt(other, self); 3358 }); 3359 3360 $def(self, '$>=', function $Module_$gt_eq$5(other) { 3361 var self = this, $ret_or_1 = nil; 3362 3363 if ($truthy(($ret_or_1 = self['$equal?'](other)))) { 3364 return $ret_or_1 3365 } else { 3366 return $rb_gt(self, other) 3367 } 3368 }); 3369 3370 $def(self, '$<=>', function $Module_$lt_eq_gt$6(other) { 3371 var self = this, lt = nil; 3372 3373 3374 3375 if (self === other) { 3376 return 0; 3377 } 3378 ; 3379 if (!$eqeqeq($Module, other)) { 3380 return nil 3381 }; 3382 lt = $rb_lt(self, other); 3383 if ($truthy(lt['$nil?']())) { 3384 return nil 3385 }; 3386 if ($truthy(lt)) { 3387 return -1 3388 } else { 3389 return 1 3390 }; 3391 }); 3392 3393 $def(self, '$alias_method', function $$alias_method(newname, oldname) { 3394 var self = this; 3395 3396 3397 $deny_frozen_access(self); 3398 newname = $coerce_to(newname, $$$('String'), 'to_str'); 3399 oldname = $coerce_to(oldname, $$$('String'), 'to_str'); 3400 Opal.alias(self, newname, oldname); 3401 return self; 3402 }); 3403 3404 $def(self, '$alias_native', function $$alias_native(mid, jsid) { 3405 var self = this; 3406 3407 3408 if (jsid == null) jsid = mid; 3409 $deny_frozen_access(self); 3410 Opal.alias_native(self, mid, jsid); 3411 return self; 3412 }, -2); 3413 3414 $def(self, '$ancestors', function $$ancestors() { 3415 var self = this; 3416 3417 return Opal.ancestors(self); 3418 }); 3419 3420 $def(self, '$append_features', function $$append_features(includer) { 3421 var self = this; 3422 3423 3424 $deny_frozen_access(includer); 3425 Opal.append_features(self, includer); 3426 return self; 3427 }); 3428 3429 $def(self, '$attr_accessor', function $$attr_accessor($a) { 3430 var $post_args, names, self = this; 3431 3432 3433 $post_args = $slice(arguments); 3434 names = $post_args; 3435 $send(self, 'attr_reader', $to_a(names)); 3436 return $send(self, 'attr_writer', $to_a(names)); 3437 }, -1); 3438 3439 $def(self, '$attr', function $$attr($a) { 3440 var $post_args, args, self = this; 3441 3442 3443 $post_args = $slice(arguments); 3444 args = $post_args; 3445 3446 if (args.length == 2 && (args[1] === true || args[1] === false)) { 3447 self.$warn("optional boolean argument is obsoleted", $hash2(["uplevel"], {"uplevel": 1})) 3448 3449 args[1] ? self.$attr_accessor(args[0]) : self.$attr_reader(args[0]); 3450 return nil; 3451 } 3452 ; 3453 return $send(self, 'attr_reader', $to_a(args)); 3454 }, -1); 3455 3456 $def(self, '$attr_reader', function $$attr_reader($a) { 3457 var $post_args, names, self = this; 3458 3459 3460 $post_args = $slice(arguments); 3461 names = $post_args; 3462 3463 $deny_frozen_access(self); 3464 3465 var proto = self.$$prototype; 3466 3467 for (var i = names.length - 1; i >= 0; i--) { 3468 var name = names[i], 3469 id = $jsid(name), 3470 ivar = $ivar(name); 3471 3472 var body = $return_ivar(ivar); 3473 3474 // initialize the instance variable as nil 3475 Opal.prop(proto, ivar, nil); 3476 3477 body.$$parameters = []; 3478 body.$$arity = 0; 3479 3480 Opal.defn(self, id, body); 3481 } 3482 ; 3483 return nil; 3484 }, -1); 3485 3486 $def(self, '$attr_writer', function $$attr_writer($a) { 3487 var $post_args, names, self = this; 3488 3489 3490 $post_args = $slice(arguments); 3491 names = $post_args; 3492 3493 $deny_frozen_access(self); 3494 3495 var proto = self.$$prototype; 3496 3497 for (var i = names.length - 1; i >= 0; i--) { 3498 var name = names[i], 3499 id = $jsid(name + '='), 3500 ivar = $ivar(name); 3501 3502 var body = $assign_ivar(ivar) 3503 3504 body.$$parameters = [['req']]; 3505 body.$$arity = 1; 3506 3507 // initialize the instance variable as nil 3508 Opal.prop(proto, ivar, nil); 3509 3510 Opal.defn(self, id, body); 3511 } 3512 ; 3513 return nil; 3514 }, -1); 3515 3516 $def(self, '$autoload', function $$autoload(const$, path) { 3517 var self = this; 3518 3519 3520 $deny_frozen_access(self); 3521 3522 if (!$$('Opal')['$const_name?'](const$)) { 3523 $Kernel.$raise($$$('NameError'), "autoload must be constant name: " + (const$)) 3524 } 3525 3526 if (path == "") { 3527 $Kernel.$raise($$$('ArgumentError'), "empty file name") 3528 } 3529 3530 if (!self.$$const.hasOwnProperty(const$)) { 3531 if (!self.$$autoload) { 3532 self.$$autoload = {}; 3533 } 3534 Opal.const_cache_version++; 3535 self.$$autoload[const$] = { path: path, loaded: false, required: false, success: false, exception: false }; 3536 3537 if (self.$const_added && !self.$const_added.$$pristine) { 3538 self.$const_added(const$); 3539 } 3540 } 3541 return nil; 3542 3543 }); 3544 3545 $def(self, '$autoload?', function $Module_autoload$ques$7(const$) { 3546 var self = this; 3547 3548 3549 if (self.$$autoload && self.$$autoload[const$] && !self.$$autoload[const$].required && !self.$$autoload[const$].success) { 3550 return self.$$autoload[const$].path; 3551 } 3552 3553 var ancestors = self.$ancestors(); 3554 3555 for (var i = 0, length = ancestors.length; i < length; i++) { 3556 if (ancestors[i].$$autoload && ancestors[i].$$autoload[const$] && !ancestors[i].$$autoload[const$].required && !ancestors[i].$$autoload[const$].success) { 3557 return ancestors[i].$$autoload[const$].path; 3558 } 3559 } 3560 return nil; 3561 3562 }); 3563 3564 $def(self, '$class_variables', function $$class_variables() { 3565 var self = this; 3566 3567 return Object.keys(Opal.class_variables(self)); 3568 }); 3569 3570 $def(self, '$class_variable_get', function $$class_variable_get(name) { 3571 var self = this; 3572 3573 3574 name = $Opal['$class_variable_name!'](name); 3575 return Opal.class_variable_get(self, name, false);; 3576 }); 3577 3578 $def(self, '$class_variable_set', function $$class_variable_set(name, value) { 3579 var self = this; 3580 3581 3582 $deny_frozen_access(self); 3583 name = $Opal['$class_variable_name!'](name); 3584 return Opal.class_variable_set(self, name, value);; 3585 }); 3586 3587 $def(self, '$class_variable_defined?', function $Module_class_variable_defined$ques$8(name) { 3588 var self = this; 3589 3590 3591 name = $Opal['$class_variable_name!'](name); 3592 return Opal.class_variables(self).hasOwnProperty(name);; 3593 }); 3594 3595 $def(self, '$const_added', $return_val(nil)); 3596 $Opal.$pristine(self, "const_added"); 3597 3598 $def(self, '$remove_class_variable', function $$remove_class_variable(name) { 3599 var self = this; 3600 3601 3602 $deny_frozen_access(self); 3603 name = $Opal['$class_variable_name!'](name); 3604 3605 if (Opal.hasOwnProperty.call(self.$$cvars, name)) { 3606 var value = self.$$cvars[name]; 3607 delete self.$$cvars[name]; 3608 return value; 3609 } else { 3610 $Kernel.$raise($$$('NameError'), "cannot remove " + (name) + " for " + (self)) 3611 } 3612 ; 3613 }); 3614 3615 $def(self, '$constants', function $$constants(inherit) { 3616 var self = this; 3617 3618 3619 if (inherit == null) inherit = true; 3620 return Opal.constants(self, inherit);; 3621 }, -1); 3622 $defs(self, '$constants', function $$constants(inherit) { 3623 var self = this; 3624 3625 3626 ; 3627 3628 if (inherit == null) { 3629 var nesting = (self.$$nesting || []).concat($Object), 3630 constant, constants = {}, 3631 i, ii; 3632 3633 for(i = 0, ii = nesting.length; i < ii; i++) { 3634 for (constant in nesting[i].$$const) { 3635 constants[constant] = true; 3636 } 3637 } 3638 return Object.keys(constants); 3639 } else { 3640 return Opal.constants(self, inherit) 3641 } 3642 ; 3643 }, -1); 3644 $defs(self, '$nesting', function $$nesting() { 3645 var self = this; 3646 3647 return self.$$nesting || []; 3648 }); 3649 3650 $def(self, '$const_defined?', function $Module_const_defined$ques$9(name, inherit) { 3651 var self = this; 3652 3653 3654 if (inherit == null) inherit = true; 3655 name = $$('Opal')['$const_name!'](name); 3656 if (!$truthy(name['$=~']($$$($Opal, 'CONST_NAME_REGEXP')))) { 3657 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)) 3658 }; 3659 3660 var module, modules = [self], module_constants, i, ii; 3661 3662 // Add up ancestors if inherit is true 3663 if (inherit) { 3664 modules = modules.concat(Opal.ancestors(self)); 3665 3666 // Add Object's ancestors if it's a module – modules have no ancestors otherwise 3667 if (self.$$is_module) { 3668 modules = modules.concat([$Object]).concat(Opal.ancestors($Object)); 3669 } 3670 } 3671 3672 for (i = 0, ii = modules.length; i < ii; i++) { 3673 module = modules[i]; 3674 if (module.$$const[name] != null) { return true; } 3675 if ( 3676 module.$$autoload && 3677 module.$$autoload[name] && 3678 !module.$$autoload[name].required && 3679 !module.$$autoload[name].success 3680 ) { 3681 return true; 3682 } 3683 } 3684 3685 return false; 3686 ; 3687 }, -2); 3688 3689 $def(self, '$const_get', function $$const_get(name, inherit) { 3690 var self = this; 3691 3692 3693 if (inherit == null) inherit = true; 3694 name = $$('Opal')['$const_name!'](name); 3695 3696 if (name.indexOf('::') === 0 && name !== '::'){ 3697 name = name.slice(2); 3698 } 3699 ; 3700 if ($truthy(name.indexOf('::') != -1 && name != '::')) { 3701 return $send(name.$split("::"), 'inject', [self], function $$10(o, c){ 3702 3703 if (o == null) o = nil; 3704 if (c == null) c = nil; 3705 return o.$const_get(c);}) 3706 }; 3707 if (!$truthy(name['$=~']($$$($Opal, 'CONST_NAME_REGEXP')))) { 3708 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)) 3709 }; 3710 3711 if (inherit) { 3712 return Opal.$$([self], name); 3713 } else { 3714 return Opal.const_get_local(self, name); 3715 } 3716 ; 3717 }, -2); 3718 3719 $def(self, '$const_missing', function $$const_missing(name) { 3720 var self = this, full_const_name = nil; 3721 3722 3723 full_const_name = ($eqeq(self, $Object) ? (name) : ("" + (self) + "::" + (name))); 3724 return $Kernel.$raise($$$('NameError').$new("uninitialized constant " + (full_const_name), name)); 3725 }); 3726 3727 $def(self, '$const_set', function $$const_set(name, value) { 3728 var self = this; 3729 3730 3731 $deny_frozen_access(self); 3732 name = $Opal['$const_name!'](name); 3733 if (($truthy(name['$!~']($$$($Opal, 'CONST_NAME_REGEXP'))) || ($truthy(name['$start_with?']("::"))))) { 3734 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)) 3735 }; 3736 $const_set(self, name, value); 3737 return value; 3738 }); 3739 3740 $def(self, '$public_constant', $return_val(nil)); 3741 3742 $def(self, '$define_method', function $$define_method(name, method) { 3743 var block = $$define_method.$$p || nil, self = this, $ret_or_1 = nil, $ret_or_2 = nil; 3744 3745 $$define_method.$$p = null; 3746 3747 ; 3748 ; 3749 3750 $deny_frozen_access(self); 3751 3752 if (method === undefined && block === nil) 3753 $Kernel.$raise($$$('ArgumentError'), "tried to create a Proc object without a block") 3754 ; 3755 block = ($truthy(($ret_or_1 = block)) ? ($ret_or_1) : ($eqeqeq($$$('Proc'), ($ret_or_2 = method)) ? (method) : ($eqeqeq($$$('Method'), $ret_or_2) ? (method.$to_proc().$$unbound) : ($eqeqeq($$$('UnboundMethod'), $ret_or_2) ? ($lambda(function $$11($a){var $post_args, args, self = $$11.$$s == null ? this : $$11.$$s, bound = nil; 3756 3757 3758 $post_args = $slice(arguments); 3759 args = $post_args; 3760 bound = method.$bind(self); 3761 return $send(bound, 'call', $to_a(args));}, {$$arity: -1, $$s: self})) : ($Kernel.$raise($$$('TypeError'), "wrong argument type " + (block.$class()) + " (expected Proc/Method)")))))); 3762 3763 if (typeof(Proxy) !== 'undefined') { 3764 var meta = Object.create(null) 3765 3766 block.$$proxy_target = block 3767 block = new Proxy(block, { 3768 apply: function(target, self, args) { 3769 var old_name = target.$$jsid 3770 target.$$jsid = name; 3771 try { 3772 return target.apply(self, args); 3773 } catch(e) { 3774 if (e === target.$$brk || e === target.$$ret) return e.$v; 3775 throw e; 3776 } finally { 3777 target.$$jsid = old_name 3778 } 3779 } 3780 }) 3781 } 3782 3783 block.$$jsid = name; 3784 block.$$s = null; 3785 block.$$def = block; 3786 block.$$define_meth = true; 3787 3788 return Opal.defn(self, $jsid(name), block); 3789 ; 3790 }, -2); 3791 3792 $def(self, '$freeze', function $$freeze() { 3793 var self = this; 3794 3795 3796 if ($truthy(self['$frozen?']())) { 3797 return self 3798 }; 3799 3800 if (!self.hasOwnProperty('$$base_module')) { $prop(self, '$$base_module', null); } 3801 3802 return $freeze(self); 3803 ; 3804 }); 3805 3806 $def(self, '$remove_method', function $$remove_method($a) { 3807 var $post_args, names, self = this; 3808 3809 3810 $post_args = $slice(arguments); 3811 names = $post_args; 3812 3813 for (var i = 0; i < names.length; i++) { 3814 var name = names[i]; 3815 if (!(typeof name === "string" || name.$$is_string)) { 3816 self.$raise($$$('TypeError'), "" + (self.$name()) + " is not a symbol nor a string") 3817 } 3818 $deny_frozen_access(self); 3819 3820 Opal.rdef(self, "$" + name); 3821 } 3822 ; 3823 return self; 3824 }, -1); 3825 3826 $def(self, '$singleton_class?', function $Module_singleton_class$ques$12() { 3827 var self = this; 3828 3829 return !!self.$$is_singleton; 3830 }); 3831 3832 $def(self, '$include', function $$include($a) { 3833 var $post_args, mods, self = this; 3834 3835 3836 $post_args = $slice(arguments); 3837 mods = $post_args; 3838 3839 for (var i = mods.length - 1; i >= 0; i--) { 3840 var mod = mods[i]; 3841 3842 if (!mod.$$is_module) { 3843 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 3844 } 3845 3846 (mod).$append_features(self); 3847 (mod).$included(self); 3848 } 3849 ; 3850 return self; 3851 }, -1); 3852 3853 $def(self, '$included_modules', function $$included_modules() { 3854 var self = this; 3855 3856 return Opal.included_modules(self); 3857 }); 3858 3859 $def(self, '$include?', function $Module_include$ques$13(mod) { 3860 var self = this; 3861 3862 3863 if (!mod.$$is_module) { 3864 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 3865 } 3866 3867 var i, ii, mod2, ancestors = Opal.ancestors(self); 3868 3869 for (i = 0, ii = ancestors.length; i < ii; i++) { 3870 mod2 = ancestors[i]; 3871 if (mod2 === mod && mod2 !== self) { 3872 return true; 3873 } 3874 } 3875 3876 return false; 3877 3878 }); 3879 3880 $def(self, '$instance_method', function $$instance_method(name) { 3881 var self = this; 3882 3883 3884 var meth = self.$$prototype[$jsid(name)]; 3885 3886 if (!meth || meth.$$stub) { 3887 $Kernel.$raise($$$('NameError').$new("undefined method `" + (name) + "' for class `" + (self.$name()) + "'", name)); 3888 } 3889 3890 return $$$('UnboundMethod').$new(self, meth.$$owner || self, meth, name); 3891 3892 }); 3893 3894 $def(self, '$instance_methods', function $$instance_methods(include_super) { 3895 var self = this; 3896 3897 3898 if (include_super == null) include_super = true; 3899 3900 if ($truthy(include_super)) { 3901 return Opal.instance_methods(self); 3902 } else { 3903 return Opal.own_instance_methods(self); 3904 } 3905 ; 3906 }, -1); 3907 3908 $def(self, '$included', $return_val(nil)); 3909 3910 $def(self, '$extended', $return_val(nil)); 3911 3912 $def(self, '$extend_object', function $$extend_object(object) { 3913 3914 3915 $deny_frozen_access(object); 3916 return nil; 3917 }); 3918 3919 $def(self, '$method_added', function $$method_added($a) { 3920 var $post_args, $fwd_rest; 3921 3922 3923 $post_args = $slice(arguments); 3924 $fwd_rest = $post_args; 3925 return nil; 3926 }, -1); 3927 3928 $def(self, '$method_removed', function $$method_removed($a) { 3929 var $post_args, $fwd_rest; 3930 3931 3932 $post_args = $slice(arguments); 3933 $fwd_rest = $post_args; 3934 return nil; 3935 }, -1); 3936 3937 $def(self, '$method_undefined', function $$method_undefined($a) { 3938 var $post_args, $fwd_rest; 3939 3940 3941 $post_args = $slice(arguments); 3942 $fwd_rest = $post_args; 3943 return nil; 3944 }, -1); 3945 3946 $def(self, '$module_eval', function $$module_eval($a) { 3947 var block = $$module_eval.$$p || nil, $post_args, args, $b, self = this, string = nil, file = nil, _lineno = nil, default_eval_options = nil, $ret_or_1 = nil, compiling_options = nil, compiled = nil; 3948 3949 $$module_eval.$$p = null; 3950 3951 ; 3952 $post_args = $slice(arguments); 3953 args = $post_args; 3954 if (($truthy(block['$nil?']()) && ($truthy(!!Opal.compile)))) { 3955 3956 if (!$truthy($range(1, 3, false)['$cover?'](args.$size()))) { 3957 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)") 3958 }; 3959 $b = [].concat($to_a(args)), (string = ($b[0] == null ? nil : $b[0])), (file = ($b[1] == null ? nil : $b[1])), (_lineno = ($b[2] == null ? nil : $b[2])), $b; 3960 default_eval_options = $hash2(["file", "eval"], {"file": ($truthy(($ret_or_1 = file)) ? ($ret_or_1) : ("(eval)")), "eval": true}); 3961 compiling_options = Opal.hash({ arity_check: false }).$merge(default_eval_options); 3962 compiled = $Opal.$compile(string, compiling_options); 3963 block = $send($Kernel, 'proc', [], function $$14(){var self = $$14.$$s == null ? this : $$14.$$s; 3964 3965 return new Function("Opal,self", "return " + compiled)(Opal, self);}, {$$s: self}); 3966 } else if ($truthy(args['$any?']())) { 3967 $Kernel.$raise($$$('ArgumentError'), "" + ("wrong number of arguments (" + (args.$size()) + " for 0)") + "\n\n NOTE:If you want to enable passing a String argument please add \"require 'opal-parser'\" to your script\n") 3968 }; 3969 3970 var old = block.$$s, 3971 result; 3972 3973 block.$$s = null; 3974 result = block.apply(self, [self]); 3975 block.$$s = old; 3976 3977 return result; 3978 ; 3979 }, -1); 3980 3981 $def(self, '$module_exec', function $$module_exec($a) { 3982 var block = $$module_exec.$$p || nil, $post_args, args, self = this; 3983 3984 $$module_exec.$$p = null; 3985 3986 ; 3987 $post_args = $slice(arguments); 3988 args = $post_args; 3989 3990 if (block === nil) { 3991 $Kernel.$raise($$$('LocalJumpError'), "no block given") 3992 } 3993 3994 var block_self = block.$$s, result; 3995 3996 block.$$s = null; 3997 result = block.apply(self, args); 3998 block.$$s = block_self; 3999 4000 return result; 4001 ; 4002 }, -1); 4003 4004 $def(self, '$method_defined?', function $Module_method_defined$ques$15(method) { 4005 var self = this; 4006 4007 4008 var body = self.$$prototype[$jsid(method)]; 4009 return (!!body) && !body.$$stub; 4010 4011 }); 4012 4013 $def(self, '$module_function', function $$module_function($a) { 4014 var $post_args, methods, self = this; 4015 4016 4017 $post_args = $slice(arguments); 4018 methods = $post_args; 4019 4020 $deny_frozen_access(self); 4021 4022 if (methods.length === 0) { 4023 self.$$module_function = true; 4024 return nil; 4025 } 4026 else { 4027 for (var i = 0, length = methods.length; i < length; i++) { 4028 var meth = methods[i], 4029 id = $jsid(meth), 4030 func = self.$$prototype[id]; 4031 4032 Opal.defs(self, id, func); 4033 } 4034 return methods.length === 1 ? methods[0] : methods; 4035 } 4036 4037 return self; 4038 ; 4039 }, -1); 4040 4041 $def(self, '$name', function $$name() { 4042 var self = this; 4043 4044 4045 if (self.$$full_name) { 4046 return self.$$full_name; 4047 } 4048 4049 var result = [], base = self; 4050 4051 while (base) { 4052 // Give up if any of the ancestors is unnamed 4053 if (base.$$name === nil || base.$$name == null) return nil; 4054 4055 result.unshift(base.$$name); 4056 4057 base = base.$$base_module; 4058 4059 if (base === $Object) { 4060 break; 4061 } 4062 } 4063 4064 if (result.length === 0) { 4065 return nil; 4066 } 4067 4068 return self.$$full_name = result.join('::'); 4069 4070 }); 4071 4072 $def(self, '$prepend', function $$prepend($a) { 4073 var $post_args, mods, self = this; 4074 4075 4076 $post_args = $slice(arguments); 4077 mods = $post_args; 4078 4079 if (mods.length === 0) { 4080 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (given 0, expected 1+)") 4081 } 4082 4083 for (var i = mods.length - 1; i >= 0; i--) { 4084 var mod = mods[i]; 4085 4086 if (!mod.$$is_module) { 4087 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 4088 } 4089 4090 (mod).$prepend_features(self); 4091 (mod).$prepended(self); 4092 } 4093 ; 4094 return self; 4095 }, -1); 4096 4097 $def(self, '$prepend_features', function $$prepend_features(prepender) { 4098 var self = this; 4099 4100 4101 4102 $deny_frozen_access(prepender); 4103 4104 if (!self.$$is_module) { 4105 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (self.$class()) + " (expected Module)"); 4106 } 4107 4108 Opal.prepend_features(self, prepender) 4109 ; 4110 return self; 4111 }); 4112 4113 $def(self, '$prepended', $return_val(nil)); 4114 4115 $def(self, '$remove_const', function $$remove_const(name) { 4116 var self = this; 4117 4118 4119 $deny_frozen_access(self); 4120 return Opal.const_remove(self, name);; 4121 }); 4122 4123 $def(self, '$to_s', function $$to_s() { 4124 var self = this, $ret_or_1 = nil; 4125 4126 if ($truthy(($ret_or_1 = Opal.Module.$name.call(self)))) { 4127 return $ret_or_1 4128 } else { 4129 return "#<" + (self.$$is_module ? 'Module' : 'Class') + ":0x" + (self.$__id__().$to_s(16)) + ">" 4130 } 4131 }); 4132 4133 $def(self, '$undef_method', function $$undef_method($a) { 4134 var $post_args, names, self = this; 4135 4136 4137 $post_args = $slice(arguments); 4138 names = $post_args; 4139 4140 for (var i = 0; i < names.length; i++) { 4141 var name = names[i]; 4142 if (!(typeof name === "string" || name.$$is_string)) { 4143 self.$raise($$$('TypeError'), "" + (self.$name()) + " is not a symbol nor a string") 4144 } 4145 $deny_frozen_access(self); 4146 4147 Opal.udef(self, "$" + names[i]); 4148 } 4149 ; 4150 return self; 4151 }, -1); 4152 4153 $def(self, '$instance_variables', function $$instance_variables() { 4154 var self = this, consts = nil; 4155 4156 4157 consts = (Opal.Module.$$nesting = $nesting, self.$constants()); 4158 4159 var result = []; 4160 4161 for (var name in self) { 4162 if (self.hasOwnProperty(name) && name.charAt(0) !== '$' && name !== 'constructor' && !consts['$include?'](name)) { 4163 result.push('@' + name); 4164 } 4165 } 4166 4167 return result; 4168 ; 4169 }); 4170 4171 $def(self, '$dup', function $$dup() { 4172 var $yield = $$dup.$$p || nil, self = this, copy = nil; 4173 4174 $$dup.$$p = null; 4175 4176 copy = $send2(self, $find_super(self, 'dup', $$dup, false, true), 'dup', [], $yield); 4177 copy.$copy_class_variables(self); 4178 copy.$copy_constants(self); 4179 return copy; 4180 }); 4181 4182 $def(self, '$copy_class_variables', function $$copy_class_variables(other) { 4183 var self = this; 4184 4185 4186 for (var name in other.$$cvars) { 4187 self.$$cvars[name] = other.$$cvars[name]; 4188 } 4189 4190 }); 4191 4192 $def(self, '$copy_constants', function $$copy_constants(other) { 4193 var self = this; 4194 4195 4196 var name, other_constants = other.$$const; 4197 4198 for (name in other_constants) { 4199 $const_set(self, name, other_constants[name]); 4200 } 4201 4202 }); 4203 4204 $def(self, '$refine', function $$refine(klass) { 4205 var block = $$refine.$$p || nil, $a, self = this, refinement_module = nil, m = nil, klass_id = nil; 4206 4207 $$refine.$$p = null; 4208 4209 ; 4210 $a = [self, nil, nil], (refinement_module = $a[0]), (m = $a[1]), (klass_id = $a[2]), $a; 4211 4212 klass_id = Opal.id(klass); 4213 if (typeof self.$$refine_modules === "undefined") { 4214 self.$$refine_modules = Object.create(null); 4215 } 4216 if (typeof self.$$refine_modules[klass_id] === "undefined") { 4217 m = self.$$refine_modules[klass_id] = $$$('Refinement').$new(); 4218 } 4219 else { 4220 m = self.$$refine_modules[klass_id]; 4221 } 4222 m.refinement_module = refinement_module 4223 m.refined_class = klass 4224 ; 4225 $send(m, 'class_exec', [], block.$to_proc()); 4226 return m; 4227 }); 4228 4229 $def(self, '$refinements', function $$refinements() { 4230 var self = this; 4231 4232 4233 var refine_modules = self.$$refine_modules, hash = $hash2([], {});; 4234 if (typeof refine_modules === "undefined") return hash; 4235 for (var id in refine_modules) { 4236 hash['$[]='](refine_modules[id].refined_class, refine_modules[id]); 4237 } 4238 return hash; 4239 4240 }); 4241 4242 $def(self, '$using', function $$using(mod) { 4243 4244 return $Kernel.$raise("Module#using is not permitted in methods") 4245 }); 4246 $alias(self, "class_eval", "module_eval"); 4247 $alias(self, "class_exec", "module_exec"); 4248 return $alias(self, "inspect", "to_s"); 4249 })('::', null, $nesting); 4250 return (function($base, $super) { 4251 var self = $klass($base, $super, 'Refinement'); 4252 4253 var $proto = self.$$prototype; 4254 4255 $proto.refinement_module = $proto.refined_class = nil; 4256 4257 self.$attr_reader("refined_class"); 4258 return $def(self, '$inspect', function $$inspect() { 4259 var $yield = $$inspect.$$p || nil, self = this; 4260 4261 $$inspect.$$p = null; 4262 if ($truthy(self.refinement_module)) { 4263 return "#<refinement:" + (self.refined_class.$inspect()) + "@" + (self.refinement_module.$inspect()) + ">" 4264 } else { 4265 return $send2(self, $find_super(self, 'inspect', $$inspect, false, true), 'inspect', [], $yield) 4266 } 4267 }); 4268 })('::', $Module); 4269}; 4270 4271Opal.modules["corelib/class"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4272 var $klass = Opal.klass, $send = Opal.send, $defs = Opal.defs, $def = Opal.def, $rb_plus = Opal.rb_plus, $return_val = Opal.return_val, $slice = Opal.slice, $send2 = Opal.send2, $find_super = Opal.find_super, $Kernel = Opal.Kernel, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 4273 4274 Opal.add_stubs('require,class_eval,to_proc,+,subclasses,flatten,map,initialize_copy,allocate,name,to_s,raise'); 4275 4276 self.$require("corelib/module"); 4277 return (function($base, $super, $parent_nesting) { 4278 var self = $klass($base, $super, 'Class'); 4279 4280 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 4281 4282 4283 $defs(self, '$new', function $Class_new$1(superclass) { 4284 var block = $Class_new$1.$$p || nil; 4285 4286 $Class_new$1.$$p = null; 4287 4288 ; 4289 if (superclass == null) superclass = $$('Object'); 4290 4291 if (!superclass.$$is_class) { 4292 throw Opal.TypeError.$new("superclass must be a Class"); 4293 } 4294 4295 var klass = Opal.allocate_class(nil, superclass); 4296 superclass.$inherited(klass); 4297 ((block !== nil) ? ($send((klass), 'class_eval', [], block.$to_proc())) : nil) 4298 return klass; 4299 ; 4300 }, -1); 4301 4302 $def(self, '$allocate', function $$allocate() { 4303 var self = this; 4304 4305 4306 var obj = new self.$$constructor(); 4307 obj.$$id = Opal.uid(); 4308 return obj; 4309 4310 }); 4311 4312 $def(self, '$descendants', function $$descendants() { 4313 var self = this; 4314 4315 return $rb_plus(self.$subclasses(), $send(self.$subclasses(), 'map', [], "descendants".$to_proc()).$flatten()) 4316 }); 4317 4318 $def(self, '$inherited', $return_val(nil)); 4319 4320 $def(self, '$initialize_dup', function $$initialize_dup(original) { 4321 var self = this; 4322 4323 4324 self.$initialize_copy(original); 4325 4326 self.$$name = null; 4327 self.$$full_name = null; 4328 ; 4329 }); 4330 4331 $def(self, '$new', function $Class_new$2($a) { 4332 var block = $Class_new$2.$$p || nil, $post_args, args, self = this; 4333 4334 $Class_new$2.$$p = null; 4335 4336 ; 4337 $post_args = $slice(arguments); 4338 args = $post_args; 4339 4340 var object = self.$allocate(); 4341 Opal.send(object, object.$initialize, args, block); 4342 return object; 4343 ; 4344 }, -1); 4345 4346 $def(self, '$subclasses', function $$subclasses() { 4347 var self = this; 4348 4349 4350 if (typeof WeakRef !== 'undefined') { 4351 var i, subclass, out = []; 4352 for (i = 0; i < self.$$subclasses.length; i++) { 4353 subclass = self.$$subclasses[i].deref(); 4354 if (subclass !== undefined) { 4355 out.push(subclass); 4356 } 4357 } 4358 return out; 4359 } 4360 else { 4361 return self.$$subclasses; 4362 } 4363 4364 }); 4365 4366 $def(self, '$superclass', function $$superclass() { 4367 var self = this; 4368 4369 return self.$$super || nil; 4370 }); 4371 4372 $def(self, '$to_s', function $$to_s() { 4373 var $yield = $$to_s.$$p || nil, self = this; 4374 4375 $$to_s.$$p = null; 4376 4377 var singleton_of = self.$$singleton_of; 4378 4379 if (singleton_of && singleton_of.$$is_a_module) { 4380 return "#<Class:" + ((singleton_of).$name()) + ">"; 4381 } 4382 else if (singleton_of) { 4383 // a singleton class created from an object 4384 return "#<Class:#<" + ((singleton_of.$$class).$name()) + ":0x" + ((Opal.id(singleton_of)).$to_s(16)) + ">>"; 4385 } 4386 4387 return $send2(self, $find_super(self, 'to_s', $$to_s, false, true), 'to_s', [], null); 4388 4389 }); 4390 4391 $def(self, '$attached_object', function $$attached_object() { 4392 var self = this; 4393 4394 4395 if (self.$$singleton_of != null) { 4396 return self.$$singleton_of; 4397 } 4398 else { 4399 $Kernel.$raise($$$('TypeError'), "`" + (self) + "' is not a singleton class") 4400 } 4401 4402 }); 4403 return $alias(self, "inspect", "to_s"); 4404 })('::', null, $nesting); 4405}; 4406 4407Opal.modules["corelib/basic_object"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4408 "use strict"; 4409 var $klass = Opal.klass, $slice = Opal.slice, $def = Opal.def, $alias = Opal.alias, $return_val = Opal.return_val, $Opal = Opal.Opal, $truthy = Opal.truthy, $range = Opal.range, $Kernel = Opal.Kernel, $to_a = Opal.to_a, $hash2 = Opal.hash2, $send = Opal.send, $eqeq = Opal.eqeq, $rb_ge = Opal.rb_ge, nil = Opal.nil, $$$ = Opal.$$$; 4410 4411 Opal.add_stubs('==,raise,inspect,pristine,!,nil?,cover?,size,merge,compile,proc,[],first,>=,length,instance_variable_get,any?,new,caller'); 4412 return (function($base, $super) { 4413 var self = $klass($base, $super, 'BasicObject'); 4414 4415 4416 4417 4418 $def(self, '$initialize', function $$initialize($a) { 4419 var $post_args, $fwd_rest; 4420 4421 4422 $post_args = $slice(arguments); 4423 $fwd_rest = $post_args; 4424 return nil; 4425 }, -1); 4426 4427 $def(self, '$==', function $BasicObject_$eq_eq$1(other) { 4428 var self = this; 4429 4430 return self === other; 4431 }); 4432 4433 $def(self, '$eql?', function $BasicObject_eql$ques$2(other) { 4434 var self = this; 4435 4436 return self['$=='](other) 4437 }); 4438 $alias(self, "equal?", "=="); 4439 4440 $def(self, '$__id__', function $$__id__() { 4441 var self = this; 4442 4443 4444 if (self.$$id != null) { 4445 return self.$$id; 4446 } 4447 Opal.prop(self, '$$id', Opal.uid()); 4448 return self.$$id; 4449 4450 }); 4451 4452 $def(self, '$__send__', function $$__send__(symbol, $a) { 4453 var block = $$__send__.$$p || nil, $post_args, args, self = this; 4454 4455 $$__send__.$$p = null; 4456 4457 ; 4458 $post_args = $slice(arguments, 1); 4459 args = $post_args; 4460 4461 if (!symbol.$$is_string) { 4462 self.$raise($$$('TypeError'), "" + (self.$inspect()) + " is not a symbol nor a string") 4463 } 4464 4465 var func = self[Opal.jsid(symbol)]; 4466 4467 if (func) { 4468 if (block !== nil) { 4469 func.$$p = block; 4470 } 4471 4472 return func.apply(self, args); 4473 } 4474 4475 if (block !== nil) { 4476 self.$method_missing.$$p = block; 4477 } 4478 4479 return self.$method_missing.apply(self, [symbol].concat(args)); 4480 ; 4481 }, -2); 4482 4483 $def(self, '$!', $return_val(false)); 4484 $Opal.$pristine("!"); 4485 4486 $def(self, '$!=', function $BasicObject_$not_eq$3(other) { 4487 var self = this; 4488 4489 return self['$=='](other)['$!']() 4490 }); 4491 4492 $def(self, '$instance_eval', function $$instance_eval($a) { 4493 var block = $$instance_eval.$$p || nil, $post_args, args, $b, self = this, string = nil, file = nil, _lineno = nil, default_eval_options = nil, $ret_or_1 = nil, compiling_options = nil, compiled = nil; 4494 4495 $$instance_eval.$$p = null; 4496 4497 ; 4498 $post_args = $slice(arguments); 4499 args = $post_args; 4500 if (($truthy(block['$nil?']()) && ($truthy(!!Opal.compile)))) { 4501 4502 if (!$truthy($range(1, 3, false)['$cover?'](args.$size()))) { 4503 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)") 4504 }; 4505 $b = [].concat($to_a(args)), (string = ($b[0] == null ? nil : $b[0])), (file = ($b[1] == null ? nil : $b[1])), (_lineno = ($b[2] == null ? nil : $b[2])), $b; 4506 default_eval_options = $hash2(["file", "eval"], {"file": ($truthy(($ret_or_1 = file)) ? ($ret_or_1) : ("(eval)")), "eval": true}); 4507 compiling_options = Opal.hash({ arity_check: false }).$merge(default_eval_options); 4508 compiled = $Opal.$compile(string, compiling_options); 4509 block = $send($Kernel, 'proc', [], function $$4(){var self = $$4.$$s == null ? this : $$4.$$s; 4510 4511 return new Function("Opal,self", "return " + compiled)(Opal, self);}, {$$s: self}); 4512 } else if ((($truthy(block['$nil?']()) && ($truthy($rb_ge(args.$length(), 1)))) && ($eqeq(args.$first()['$[]'](0), "@")))) { 4513 return self.$instance_variable_get(args.$first()) 4514 } else if ($truthy(args['$any?']())) { 4515 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$size()) + " for 0)") 4516 }; 4517 4518 var old = block.$$s, 4519 result; 4520 4521 block.$$s = null; 4522 4523 // Need to pass $$eval so that method definitions know if this is 4524 // being done on a class/module. Cannot be compiler driven since 4525 // send(:instance_eval) needs to work. 4526 if (self.$$is_a_module) { 4527 self.$$eval = true; 4528 try { 4529 result = block.call(self, self); 4530 } 4531 finally { 4532 self.$$eval = false; 4533 } 4534 } 4535 else { 4536 result = block.call(self, self); 4537 } 4538 4539 block.$$s = old; 4540 4541 return result; 4542 ; 4543 }, -1); 4544 4545 $def(self, '$instance_exec', function $$instance_exec($a) { 4546 var block = $$instance_exec.$$p || nil, $post_args, args, self = this; 4547 4548 $$instance_exec.$$p = null; 4549 4550 ; 4551 $post_args = $slice(arguments); 4552 args = $post_args; 4553 if (!$truthy(block)) { 4554 $Kernel.$raise($$$('ArgumentError'), "no block given") 4555 }; 4556 4557 var block_self = block.$$s, 4558 result; 4559 4560 block.$$s = null; 4561 4562 if (self.$$is_a_module) { 4563 self.$$eval = true; 4564 try { 4565 result = block.apply(self, args); 4566 } 4567 finally { 4568 self.$$eval = false; 4569 } 4570 } 4571 else { 4572 result = block.apply(self, args); 4573 } 4574 4575 block.$$s = block_self; 4576 4577 return result; 4578 ; 4579 }, -1); 4580 4581 $def(self, '$singleton_method_added', function $$singleton_method_added($a) { 4582 var $post_args, $fwd_rest; 4583 4584 4585 $post_args = $slice(arguments); 4586 $fwd_rest = $post_args; 4587 return nil; 4588 }, -1); 4589 4590 $def(self, '$singleton_method_removed', function $$singleton_method_removed($a) { 4591 var $post_args, $fwd_rest; 4592 4593 4594 $post_args = $slice(arguments); 4595 $fwd_rest = $post_args; 4596 return nil; 4597 }, -1); 4598 4599 $def(self, '$singleton_method_undefined', function $$singleton_method_undefined($a) { 4600 var $post_args, $fwd_rest; 4601 4602 4603 $post_args = $slice(arguments); 4604 $fwd_rest = $post_args; 4605 return nil; 4606 }, -1); 4607 4608 $def(self, '$method_missing', function $$method_missing(symbol, $a) { 4609 var block = $$method_missing.$$p || nil, $post_args, args, self = this, inspect_result = nil; 4610 4611 $$method_missing.$$p = null; 4612 4613 ; 4614 $post_args = $slice(arguments, 1); 4615 args = $post_args; 4616 inspect_result = $Opal.$inspect(self); 4617 return $Kernel.$raise($$$('NoMethodError').$new("undefined method `" + (symbol) + "' for " + (inspect_result), symbol, args), nil, $Kernel.$caller(1)); 4618 }, -2); 4619 $Opal.$pristine(self, "method_missing"); 4620 return $def(self, '$respond_to_missing?', function $BasicObject_respond_to_missing$ques$5(method_name, include_all) { 4621 4622 4623 if (include_all == null) include_all = false; 4624 return false; 4625 }, -2); 4626 })('::', null) 4627}; 4628 4629Opal.modules["corelib/kernel"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4630 "use strict"; 4631 var $truthy = Opal.truthy, $coerce_to = Opal.coerce_to, $respond_to = Opal.respond_to, $Opal = Opal.Opal, $deny_frozen_access = Opal.deny_frozen_access, $freeze = Opal.freeze, $freeze_props = Opal.freeze_props, $jsid = Opal.jsid, $module = Opal.module, $return_val = Opal.return_val, $def = Opal.def, $Kernel = Opal.Kernel, $gvars = Opal.gvars, $slice = Opal.slice, $send = Opal.send, $to_a = Opal.to_a, $ensure_kwargs = Opal.ensure_kwargs, $eqeq = Opal.eqeq, $hash2 = Opal.hash2, $rb_plus = Opal.rb_plus, $eqeqeq = Opal.eqeqeq, $return_self = Opal.return_self, $rb_le = Opal.rb_le, $extract_kwargs = Opal.extract_kwargs, $rb_lt = Opal.rb_lt, $Object = Opal.Object, $alias = Opal.alias, $klass = Opal.klass, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 4632 4633 Opal.add_stubs('!,=~,==,object_id,raise,new,class,coerce_to?,<<,map,caller,nil?,allocate,copy_instance_variables,copy_singleton_methods,initialize_clone,frozen?,freeze,initialize_copy,define_method,singleton_class,to_proc,initialize_dup,for,empty?,pop,call,append_features,extend_object,extended,gets,__id__,include?,each,instance_variables,instance_variable_get,inspect,+,to_s,instance_variable_name!,respond_to?,to_int,coerce_to!,Integer,===,enum_for,result,any?,print,format,puts,<=,length,[],readline,<,first,split,to_str,exception,backtrace,rand,respond_to_missing?,pristine,try_convert!,expand_path,join,start_with?,new_seed,srand,tag,value,open,is_a?,__send__,yield_self,include'); 4634 4635 (function($base, $parent_nesting) { 4636 var self = $module($base, 'Kernel'); 4637 4638 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 4639 4640 4641 4642 $def(self, '$=~', $return_val(false)); 4643 4644 $def(self, '$!~', function $Kernel_$excl_tilde$1(obj) { 4645 var self = this; 4646 4647 return self['$=~'](obj)['$!']() 4648 }); 4649 4650 $def(self, '$===', function $Kernel_$eq_eq_eq$2(other) { 4651 var self = this, $ret_or_1 = nil; 4652 4653 if ($truthy(($ret_or_1 = self.$object_id()['$=='](other.$object_id())))) { 4654 return $ret_or_1 4655 } else { 4656 return self['$=='](other) 4657 } 4658 }); 4659 4660 $def(self, '$<=>', function $Kernel_$lt_eq_gt$3(other) { 4661 var self = this; 4662 4663 4664 // set guard for infinite recursion 4665 self.$$comparable = true; 4666 4667 var x = self['$=='](other); 4668 4669 if (x && x !== nil) { 4670 return 0; 4671 } 4672 4673 return nil; 4674 4675 }); 4676 4677 $def(self, '$method', function $$method(name) { 4678 var self = this; 4679 4680 4681 var meth = self[$jsid(name)]; 4682 4683 if (!meth || meth.$$stub) { 4684 $Kernel.$raise($$$('NameError').$new("undefined method `" + (name) + "' for class `" + (self.$class()) + "'", name)); 4685 } 4686 4687 return $$$('Method').$new(self, meth.$$owner || self.$class(), meth, name); 4688 4689 }); 4690 4691 $def(self, '$methods', function $$methods(all) { 4692 var self = this; 4693 4694 4695 if (all == null) all = true; 4696 4697 if ($truthy(all)) { 4698 return Opal.methods(self); 4699 } else { 4700 return Opal.own_methods(self); 4701 } 4702 ; 4703 }, -1); 4704 4705 $def(self, '$public_methods', function $$public_methods(all) { 4706 var self = this; 4707 4708 4709 if (all == null) all = true; 4710 4711 if ($truthy(all)) { 4712 return Opal.methods(self); 4713 } else { 4714 return Opal.receiver_methods(self); 4715 } 4716 ; 4717 }, -1); 4718 4719 $def(self, '$Array', function $$Array(object) { 4720 4721 4722 var coerced; 4723 4724 if (object === nil) { 4725 return []; 4726 } 4727 4728 if (object.$$is_array) { 4729 return object; 4730 } 4731 4732 coerced = $Opal['$coerce_to?'](object, $$$('Array'), "to_ary"); 4733 if (coerced !== nil) { return coerced; } 4734 4735 coerced = $Opal['$coerce_to?'](object, $$$('Array'), "to_a"); 4736 if (coerced !== nil) { return coerced; } 4737 4738 return [object]; 4739 4740 }); 4741 4742 $def(self, '$at_exit', function $$at_exit() { 4743 var block = $$at_exit.$$p || nil, $ret_or_1 = nil; 4744 if ($gvars.__at_exit__ == null) $gvars.__at_exit__ = nil; 4745 4746 $$at_exit.$$p = null; 4747 4748 ; 4749 $gvars.__at_exit__ = ($truthy(($ret_or_1 = $gvars.__at_exit__)) ? ($ret_or_1) : ([])); 4750 $gvars.__at_exit__['$<<'](block); 4751 return block; 4752 }); 4753 4754 $def(self, '$caller', function $$caller(start, length) { 4755 4756 4757 if (start == null) start = 1; 4758 if (length == null) length = nil; 4759 4760 var stack, result; 4761 4762 stack = new Error().$backtrace(); 4763 result = []; 4764 4765 for (var i = start + 1, ii = stack.length; i < ii; i++) { 4766 if (!stack[i].match(/runtime\.js/)) { 4767 result.push(stack[i]); 4768 } 4769 } 4770 if (length != nil) result = result.slice(0, length); 4771 return result; 4772 ; 4773 }, -1); 4774 4775 $def(self, '$caller_locations', function $$caller_locations($a) { 4776 var $post_args, args, self = this; 4777 4778 4779 $post_args = $slice(arguments); 4780 args = $post_args; 4781 return $send($send(self, 'caller', $to_a(args)), 'map', [], function $$4(loc){ 4782 4783 if (loc == null) loc = nil; 4784 return $$$($$$($$$('Thread'), 'Backtrace'), 'Location').$new(loc);}); 4785 }, -1); 4786 4787 $def(self, '$class', function $Kernel_class$5() { 4788 var self = this; 4789 4790 return self.$$class; 4791 }); 4792 4793 $def(self, '$copy_instance_variables', function $$copy_instance_variables(other) { 4794 var self = this; 4795 4796 4797 var keys = Object.keys(other), i, ii, name; 4798 for (i = 0, ii = keys.length; i < ii; i++) { 4799 name = keys[i]; 4800 if (name.charAt(0) !== '$' && other.hasOwnProperty(name)) { 4801 self[name] = other[name]; 4802 } 4803 } 4804 4805 }); 4806 4807 $def(self, '$copy_singleton_methods', function $$copy_singleton_methods(other) { 4808 var self = this; 4809 4810 4811 var i, name, names, length; 4812 4813 if (other.hasOwnProperty('$$meta') && other.$$meta !== null) { 4814 var other_singleton_class = Opal.get_singleton_class(other); 4815 var self_singleton_class = Opal.get_singleton_class(self); 4816 names = Object.getOwnPropertyNames(other_singleton_class.$$prototype); 4817 4818 for (i = 0, length = names.length; i < length; i++) { 4819 name = names[i]; 4820 if (Opal.is_method(name)) { 4821 self_singleton_class.$$prototype[name] = other_singleton_class.$$prototype[name]; 4822 } 4823 } 4824 4825 self_singleton_class.$$const = Object.assign({}, other_singleton_class.$$const); 4826 Object.setPrototypeOf( 4827 self_singleton_class.$$prototype, 4828 Object.getPrototypeOf(other_singleton_class.$$prototype) 4829 ); 4830 } 4831 4832 for (i = 0, names = Object.getOwnPropertyNames(other), length = names.length; i < length; i++) { 4833 name = names[i]; 4834 if (name.charAt(0) === '$' && name.charAt(1) !== '$' && other.hasOwnProperty(name)) { 4835 self[name] = other[name]; 4836 } 4837 } 4838 4839 }); 4840 4841 $def(self, '$clone', function $$clone($kwargs) { 4842 var freeze, self = this, copy = nil; 4843 4844 4845 $kwargs = $ensure_kwargs($kwargs); 4846 4847 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = nil; 4848 if (!(($truthy(freeze['$nil?']()) || ($eqeq(freeze, true))) || ($eqeq(freeze, false)))) { 4849 self.$raise($$('ArgumentError'), "unexpected value for freeze: " + (freeze.$class())) 4850 }; 4851 copy = self.$class().$allocate(); 4852 copy.$copy_instance_variables(self); 4853 copy.$copy_singleton_methods(self); 4854 copy.$initialize_clone(self, $hash2(["freeze"], {"freeze": freeze})); 4855 if (($eqeq(freeze, true) || (($truthy(freeze['$nil?']()) && ($truthy(self['$frozen?']())))))) { 4856 copy.$freeze() 4857 }; 4858 return copy; 4859 }, -1); 4860 4861 $def(self, '$initialize_clone', function $$initialize_clone(other, $kwargs) { 4862 var freeze, self = this; 4863 4864 4865 $kwargs = $ensure_kwargs($kwargs); 4866 4867 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = nil; 4868 self.$initialize_copy(other); 4869 return self; 4870 }, -2); 4871 4872 $def(self, '$define_singleton_method', function $$define_singleton_method(name, method) { 4873 var block = $$define_singleton_method.$$p || nil, self = this; 4874 4875 $$define_singleton_method.$$p = null; 4876 4877 ; 4878 ; 4879 return $send(self.$singleton_class(), 'define_method', [name, method], block.$to_proc()); 4880 }, -2); 4881 4882 $def(self, '$dup', function $$dup() { 4883 var self = this, copy = nil; 4884 4885 4886 copy = self.$class().$allocate(); 4887 copy.$copy_instance_variables(self); 4888 copy.$initialize_dup(self); 4889 return copy; 4890 }); 4891 4892 $def(self, '$initialize_dup', function $$initialize_dup(other) { 4893 var self = this; 4894 4895 return self.$initialize_copy(other) 4896 }); 4897 4898 $def(self, '$enum_for', function $$enum_for($a, $b) { 4899 var block = $$enum_for.$$p || nil, $post_args, method, args, self = this; 4900 4901 $$enum_for.$$p = null; 4902 4903 ; 4904 $post_args = $slice(arguments); 4905 4906 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 4907 args = $post_args; 4908 return $send($$$('Enumerator'), 'for', [self, method].concat($to_a(args)), block.$to_proc()); 4909 }, -1); 4910 4911 $def(self, '$equal?', function $Kernel_equal$ques$6(other) { 4912 var self = this; 4913 4914 return self === other; 4915 }); 4916 4917 $def(self, '$exit', function $$exit(status) { 4918 var $ret_or_1 = nil, block = nil; 4919 if ($gvars.__at_exit__ == null) $gvars.__at_exit__ = nil; 4920 4921 4922 if (status == null) status = true; 4923 $gvars.__at_exit__ = ($truthy(($ret_or_1 = $gvars.__at_exit__)) ? ($ret_or_1) : ([])); 4924 while (!($truthy($gvars.__at_exit__['$empty?']()))) { 4925 4926 block = $gvars.__at_exit__.$pop(); 4927 block.$call(); 4928 }; 4929 4930 if (status.$$is_boolean) { 4931 status = status ? 0 : 1; 4932 } else { 4933 status = $coerce_to(status, $$$('Integer'), 'to_int') 4934 } 4935 4936 Opal.exit(status); 4937 ; 4938 return nil; 4939 }, -1); 4940 4941 $def(self, '$extend', function $$extend($a) { 4942 var $post_args, mods, self = this; 4943 4944 4945 $post_args = $slice(arguments); 4946 mods = $post_args; 4947 4948 if (mods.length == 0) { 4949 self.$raise($$$('ArgumentError'), "wrong number of arguments (given 0, expected 1+)") 4950 } 4951 4952 $deny_frozen_access(self); 4953 4954 var singleton = self.$singleton_class(); 4955 4956 for (var i = mods.length - 1; i >= 0; i--) { 4957 var mod = mods[i]; 4958 4959 if (!mod.$$is_module) { 4960 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 4961 } 4962 4963 (mod).$append_features(singleton); 4964 (mod).$extend_object(self); 4965 (mod).$extended(self); 4966 } 4967 ; 4968 return self; 4969 }, -1); 4970 4971 $def(self, '$freeze', function $$freeze() { 4972 var self = this; 4973 4974 4975 if ($truthy(self['$frozen?']())) { 4976 return self 4977 }; 4978 4979 if (typeof(self) === "object") { 4980 $freeze_props(self); 4981 return $freeze(self); 4982 } 4983 return self; 4984 ; 4985 }); 4986 4987 $def(self, '$frozen?', function $Kernel_frozen$ques$7() { 4988 var self = this; 4989 4990 4991 switch (typeof(self)) { 4992 case "string": 4993 case "symbol": 4994 case "number": 4995 case "boolean": 4996 return true; 4997 case "object": 4998 return (self.$$frozen || false); 4999 default: 5000 return false; 5001 } 5002 5003 }); 5004 5005 $def(self, '$gets', function $$gets($a) { 5006 var $post_args, args; 5007 if ($gvars.stdin == null) $gvars.stdin = nil; 5008 5009 5010 $post_args = $slice(arguments); 5011 args = $post_args; 5012 return $send($gvars.stdin, 'gets', $to_a(args)); 5013 }, -1); 5014 5015 $def(self, '$hash', function $$hash() { 5016 var self = this; 5017 5018 return self.$__id__() 5019 }); 5020 5021 $def(self, '$initialize_copy', $return_val(nil)); 5022 var inspect_stack = []; 5023 5024 $def(self, '$inspect', function $$inspect() { 5025 var self = this, ivs = nil, id = nil, pushed = nil, e = nil; 5026 5027 return (function() { try { 5028 try { 5029 5030 ivs = ""; 5031 id = self.$__id__(); 5032 if ($truthy((inspect_stack)['$include?'](id))) { 5033 ivs = " ..." 5034 } else { 5035 5036 (inspect_stack)['$<<'](id); 5037 pushed = true; 5038 $send(self.$instance_variables(), 'each', [], function $$8(i){var self = $$8.$$s == null ? this : $$8.$$s, ivar = nil, inspect = nil; 5039 5040 5041 if (i == null) i = nil; 5042 ivar = self.$instance_variable_get(i); 5043 inspect = $$('Opal').$inspect(ivar); 5044 return (ivs = $rb_plus(ivs, " " + (i) + "=" + (inspect)));}, {$$s: self}); 5045 }; 5046 return "#<" + (self.$class()) + ":0x" + (id.$to_s(16)) + (ivs) + ">"; 5047 } catch ($err) { 5048 if (Opal.rescue($err, [$$('StandardError')])) {(e = $err) 5049 try { 5050 return "#<" + (self.$class()) + ":0x" + (id.$to_s(16)) + ">" 5051 } finally { Opal.pop_exception(); } 5052 } else { throw $err; } 5053 } 5054 } finally { 5055 ($truthy(pushed) ? ((inspect_stack).$pop()) : nil) 5056 }; })() 5057 }); 5058 5059 $def(self, '$instance_of?', function $Kernel_instance_of$ques$9(klass) { 5060 var self = this; 5061 5062 5063 if (!klass.$$is_class && !klass.$$is_module) { 5064 $Kernel.$raise($$$('TypeError'), "class or module required"); 5065 } 5066 5067 return self.$$class === klass; 5068 5069 }); 5070 5071 $def(self, '$instance_variable_defined?', function $Kernel_instance_variable_defined$ques$10(name) { 5072 var self = this; 5073 5074 5075 name = $Opal['$instance_variable_name!'](name); 5076 return Opal.hasOwnProperty.call(self, name.substr(1));; 5077 }); 5078 5079 $def(self, '$instance_variable_get', function $$instance_variable_get(name) { 5080 var self = this; 5081 5082 5083 name = $Opal['$instance_variable_name!'](name); 5084 5085 var ivar = self[Opal.ivar(name.substr(1))]; 5086 5087 return ivar == null ? nil : ivar; 5088 ; 5089 }); 5090 5091 $def(self, '$instance_variable_set', function $$instance_variable_set(name, value) { 5092 var self = this; 5093 5094 5095 $deny_frozen_access(self); 5096 name = $Opal['$instance_variable_name!'](name); 5097 return self[Opal.ivar(name.substr(1))] = value;; 5098 }); 5099 5100 $def(self, '$remove_instance_variable', function $$remove_instance_variable(name) { 5101 var self = this; 5102 5103 5104 name = $Opal['$instance_variable_name!'](name); 5105 5106 var key = Opal.ivar(name.substr(1)), 5107 val; 5108 if (self.hasOwnProperty(key)) { 5109 val = self[key]; 5110 delete self[key]; 5111 return val; 5112 } 5113 ; 5114 return $Kernel.$raise($$$('NameError'), "instance variable " + (name) + " not defined"); 5115 }); 5116 5117 $def(self, '$instance_variables', function $$instance_variables() { 5118 var self = this; 5119 5120 5121 var result = [], ivar; 5122 5123 for (var name in self) { 5124 if (self.hasOwnProperty(name) && name.charAt(0) !== '$') { 5125 if (name.substr(-1) === '$') { 5126 ivar = name.slice(0, name.length - 1); 5127 } else { 5128 ivar = name; 5129 } 5130 result.push('@' + ivar); 5131 } 5132 } 5133 5134 return result; 5135 5136 }); 5137 5138 $def(self, '$Integer', function $$Integer(value, base) { 5139 5140 5141 ; 5142 5143 var i, str, base_digits; 5144 5145 if (!value.$$is_string) { 5146 if (base !== undefined) { 5147 $Kernel.$raise($$$('ArgumentError'), "base specified for non string value") 5148 } 5149 if (value === nil) { 5150 $Kernel.$raise($$$('TypeError'), "can't convert nil into Integer") 5151 } 5152 if (value.$$is_number) { 5153 if (value === Infinity || value === -Infinity || isNaN(value)) { 5154 $Kernel.$raise($$$('FloatDomainError'), value) 5155 } 5156 return Math.floor(value); 5157 } 5158 if (value['$respond_to?']("to_int")) { 5159 i = value.$to_int(); 5160 if (i !== nil) { 5161 return i; 5162 } 5163 } 5164 return $Opal['$coerce_to!'](value, $$$('Integer'), "to_i"); 5165 } 5166 5167 if (value === "0") { 5168 return 0; 5169 } 5170 5171 if (base === undefined) { 5172 base = 0; 5173 } else { 5174 base = $coerce_to(base, $$$('Integer'), 'to_int'); 5175 if (base === 1 || base < 0 || base > 36) { 5176 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)) 5177 } 5178 } 5179 5180 str = value.toLowerCase(); 5181 5182 str = str.replace(/(\d)_(?=\d)/g, '$1'); 5183 5184 str = str.replace(/^(\s*[+-]?)(0[bodx]?)/, function (_, head, flag) { 5185 switch (flag) { 5186 case '0b': 5187 if (base === 0 || base === 2) { 5188 base = 2; 5189 return head; 5190 } 5191 // no-break 5192 case '0': 5193 case '0o': 5194 if (base === 0 || base === 8) { 5195 base = 8; 5196 return head; 5197 } 5198 // no-break 5199 case '0d': 5200 if (base === 0 || base === 10) { 5201 base = 10; 5202 return head; 5203 } 5204 // no-break 5205 case '0x': 5206 if (base === 0 || base === 16) { 5207 base = 16; 5208 return head; 5209 } 5210 // no-break 5211 } 5212 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\"") 5213 }); 5214 5215 base = (base === 0 ? 10 : base); 5216 5217 base_digits = '0-' + (base <= 10 ? base - 1 : '9a-' + String.fromCharCode(97 + (base - 11))); 5218 5219 if (!(new RegExp('^\\s*[+-]?[' + base_digits + ']+\\s*$')).test(str)) { 5220 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\"") 5221 } 5222 5223 i = parseInt(str, base); 5224 5225 if (isNaN(i)) { 5226 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\"") 5227 } 5228 5229 return i; 5230 ; 5231 }, -2); 5232 5233 $def(self, '$Float', function $$Float(value) { 5234 5235 5236 var str; 5237 5238 if (value === nil) { 5239 $Kernel.$raise($$$('TypeError'), "can't convert nil into Float") 5240 } 5241 5242 if (value.$$is_string) { 5243 str = value.toString(); 5244 5245 str = str.replace(/(\d)_(?=\d)/g, '$1'); 5246 5247 //Special case for hex strings only: 5248 if (/^\s*[-+]?0[xX][0-9a-fA-F]+\s*$/.test(str)) { 5249 return $Kernel.$Integer(str); 5250 } 5251 5252 if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) { 5253 $Kernel.$raise($$$('ArgumentError'), "invalid value for Float(): \"" + (value) + "\"") 5254 } 5255 5256 return parseFloat(str); 5257 } 5258 5259 return $Opal['$coerce_to!'](value, $$$('Float'), "to_f"); 5260 5261 }); 5262 5263 $def(self, '$Hash', function $$Hash(arg) { 5264 5265 5266 if (($truthy(arg['$nil?']()) || ($eqeq(arg, [])))) { 5267 return $hash2([], {}) 5268 }; 5269 if ($eqeqeq($$$('Hash'), arg)) { 5270 return arg 5271 }; 5272 return $Opal['$coerce_to!'](arg, $$$('Hash'), "to_hash"); 5273 }); 5274 5275 $def(self, '$is_a?', function $Kernel_is_a$ques$11(klass) { 5276 var self = this; 5277 5278 5279 if (!klass.$$is_class && !klass.$$is_module) { 5280 $Kernel.$raise($$$('TypeError'), "class or module required"); 5281 } 5282 5283 return Opal.is_a(self, klass); 5284 5285 }); 5286 5287 $def(self, '$itself', $return_self); 5288 5289 $def(self, '$lambda', function $$lambda() { 5290 var block = $$lambda.$$p || nil; 5291 5292 $$lambda.$$p = null; 5293 5294 ; 5295 return Opal.lambda(block);; 5296 }); 5297 5298 $def(self, '$load', function $$load(file) { 5299 5300 5301 file = $Opal['$coerce_to!'](file, $$$('String'), "to_str"); 5302 return Opal.load(file); 5303 }); 5304 5305 $def(self, '$loop', function $$loop() { 5306 var $yield = $$loop.$$p || nil, self = this, e = nil; 5307 5308 $$loop.$$p = null; 5309 5310 if (!($yield !== nil)) { 5311 return $send(self, 'enum_for', ["loop"], function $$12(){ 5312 return $$$($$$('Float'), 'INFINITY')}) 5313 }; 5314 while ($truthy(true)) { 5315 5316 try { 5317 Opal.yieldX($yield, []) 5318 } catch ($err) { 5319 if (Opal.rescue($err, [$$$('StopIteration')])) {(e = $err) 5320 try { 5321 return e.$result() 5322 } finally { Opal.pop_exception(); } 5323 } else { throw $err; } 5324 }; 5325 }; 5326 return self; 5327 }); 5328 5329 $def(self, '$nil?', $return_val(false)); 5330 5331 $def(self, '$printf', function $$printf($a) { 5332 var $post_args, args, self = this; 5333 5334 5335 $post_args = $slice(arguments); 5336 args = $post_args; 5337 if ($truthy(args['$any?']())) { 5338 self.$print($send(self, 'format', $to_a(args))) 5339 }; 5340 return nil; 5341 }, -1); 5342 5343 $def(self, '$proc', function $$proc() { 5344 var block = $$proc.$$p || nil; 5345 5346 $$proc.$$p = null; 5347 5348 ; 5349 if (!$truthy(block)) { 5350 $Kernel.$raise($$$('ArgumentError'), "tried to create Proc object without a block") 5351 }; 5352 block.$$is_lambda = false; 5353 return block; 5354 }); 5355 5356 $def(self, '$puts', function $$puts($a) { 5357 var $post_args, strs; 5358 if ($gvars.stdout == null) $gvars.stdout = nil; 5359 5360 5361 $post_args = $slice(arguments); 5362 strs = $post_args; 5363 return $send($gvars.stdout, 'puts', $to_a(strs)); 5364 }, -1); 5365 5366 $def(self, '$p', function $$p($a) { 5367 var $post_args, args; 5368 5369 5370 $post_args = $slice(arguments); 5371 args = $post_args; 5372 $send(args, 'each', [], function $$13(obj){ if ($gvars.stdout == null) $gvars.stdout = nil; 5373 5374 5375 if (obj == null) obj = nil; 5376 return $gvars.stdout.$puts(obj.$inspect());}); 5377 if ($truthy($rb_le(args.$length(), 1))) { 5378 return args['$[]'](0) 5379 } else { 5380 return args 5381 }; 5382 }, -1); 5383 5384 $def(self, '$print', function $$print($a) { 5385 var $post_args, strs; 5386 if ($gvars.stdout == null) $gvars.stdout = nil; 5387 5388 5389 $post_args = $slice(arguments); 5390 strs = $post_args; 5391 return $send($gvars.stdout, 'print', $to_a(strs)); 5392 }, -1); 5393 5394 $def(self, '$readline', function $$readline($a) { 5395 var $post_args, args; 5396 if ($gvars.stdin == null) $gvars.stdin = nil; 5397 5398 5399 $post_args = $slice(arguments); 5400 args = $post_args; 5401 return $send($gvars.stdin, 'readline', $to_a(args)); 5402 }, -1); 5403 5404 $def(self, '$warn', function $$warn($a, $b) { 5405 var $post_args, $kwargs, strs, uplevel, $c, $d, self = this, location = nil; 5406 if ($gvars.VERBOSE == null) $gvars.VERBOSE = nil; 5407 if ($gvars.stderr == null) $gvars.stderr = nil; 5408 5409 5410 $post_args = $slice(arguments); 5411 $kwargs = $extract_kwargs($post_args); 5412 $kwargs = $ensure_kwargs($kwargs); 5413 strs = $post_args; 5414 5415 uplevel = $kwargs.$$smap["uplevel"];if (uplevel == null) uplevel = nil; 5416 if ($truthy(uplevel)) { 5417 5418 uplevel = $Opal['$coerce_to!'](uplevel, $$$('Integer'), "to_str"); 5419 if ($truthy($rb_lt(uplevel, 0))) { 5420 $Kernel.$raise($$$('ArgumentError'), "negative level (" + (uplevel) + ")") 5421 }; 5422 location = ($c = ($d = self.$caller($rb_plus(uplevel, 1), 1).$first(), ($d === nil || $d == null) ? nil : $d.$split(":in `")), ($c === nil || $c == null) ? nil : $c.$first()); 5423 if ($truthy(location)) { 5424 location = "" + (location) + ": " 5425 }; 5426 strs = $send(strs, 'map', [], function $$14(s){ 5427 5428 if (s == null) s = nil; 5429 return "" + (location) + "warning: " + (s);}); 5430 }; 5431 if (($truthy($gvars.VERBOSE['$nil?']()) || ($truthy(strs['$empty?']())))) { 5432 return nil 5433 } else { 5434 return $send($gvars.stderr, 'puts', $to_a(strs)) 5435 }; 5436 }, -1); 5437 5438 $def(self, '$raise', function $$raise(exception, string, backtrace) { 5439 if ($gvars["!"] == null) $gvars["!"] = nil; 5440 if ($gvars["@"] == null) $gvars["@"] = nil; 5441 5442 5443 ; 5444 if (string == null) string = nil; 5445 if (backtrace == null) backtrace = nil; 5446 5447 if (exception == null && $gvars["!"] !== nil) { 5448 throw $gvars["!"]; 5449 } 5450 if (exception == null) { 5451 exception = $$$('RuntimeError').$new(""); 5452 } 5453 else if ($respond_to(exception, '$to_str')) { 5454 exception = $$$('RuntimeError').$new(exception.$to_str()); 5455 } 5456 // using respond_to? and not an undefined check to avoid method_missing matching as true 5457 else if (exception.$$is_class && $respond_to(exception, '$exception')) { 5458 exception = exception.$exception(string); 5459 } 5460 else if (exception.$$is_exception) { 5461 // exception is fine 5462 } 5463 else { 5464 exception = $$$('TypeError').$new("exception class/object expected"); 5465 } 5466 5467 if (backtrace !== nil) { 5468 exception.$set_backtrace(backtrace); 5469 } 5470 5471 if ($gvars["!"] !== nil) { 5472 Opal.exceptions.push($gvars["!"]); 5473 } 5474 5475 $gvars["!"] = exception; 5476 $gvars["@"] = (exception).$backtrace(); 5477 5478 throw exception; 5479 ; 5480 }, -1); 5481 5482 $def(self, '$rand', function $$rand(max) { 5483 5484 5485 ; 5486 5487 if (max === undefined) { 5488 return $$$($$$('Random'), 'DEFAULT').$rand(); 5489 } 5490 5491 if (max.$$is_number) { 5492 if (max < 0) { 5493 max = Math.abs(max); 5494 } 5495 5496 if (max % 1 !== 0) { 5497 max = max.$to_i(); 5498 } 5499 5500 if (max === 0) { 5501 max = undefined; 5502 } 5503 } 5504 ; 5505 return $$$($$$('Random'), 'DEFAULT').$rand(max); 5506 }, -1); 5507 5508 $def(self, '$respond_to?', function $Kernel_respond_to$ques$15(name, include_all) { 5509 var self = this; 5510 5511 5512 if (include_all == null) include_all = false; 5513 5514 var body = self[$jsid(name)]; 5515 5516 if (typeof(body) === "function" && !body.$$stub) { 5517 return true; 5518 } 5519 5520 if (self['$respond_to_missing?'].$$pristine === true) { 5521 return false; 5522 } else { 5523 return self['$respond_to_missing?'](name, include_all); 5524 } 5525 ; 5526 }, -2); 5527 5528 $def(self, '$respond_to_missing?', function $Kernel_respond_to_missing$ques$16(method_name, include_all) { 5529 5530 5531 if (include_all == null) include_all = false; 5532 return false; 5533 }, -2); 5534 $Opal.$pristine(self, "respond_to?", "respond_to_missing?"); 5535 5536 $def(self, '$require', function $$require(file) { 5537 5538 5539 // As Object.require refers to Kernel.require once Kernel has been loaded the String 5540 // class may not be available yet, the coercion requires both String and Array to be loaded. 5541 if (typeof file !== 'string' && Opal.String && Opal.Array) { 5542 (file = $Opal['$coerce_to!'](file, $$$('String'), "to_str")) 5543 } 5544 return Opal.require(file) 5545 5546 }); 5547 5548 $def(self, '$require_relative', function $$require_relative(file) { 5549 5550 5551 $Opal['$try_convert!'](file, $$$('String'), "to_str"); 5552 file = $$$('File').$expand_path($$$('File').$join(Opal.current_file, "..", file)); 5553 return Opal.require(file); 5554 }); 5555 5556 $def(self, '$require_tree', function $$require_tree(path, $kwargs) { 5557 var autoload; 5558 5559 5560 $kwargs = $ensure_kwargs($kwargs); 5561 5562 autoload = $kwargs.$$smap["autoload"];if (autoload == null) autoload = false; 5563 5564 var result = []; 5565 5566 path = $$$('File').$expand_path(path) 5567 path = Opal.normalize(path); 5568 if (path === '.') path = ''; 5569 for (var name in Opal.modules) { 5570 if ((name)['$start_with?'](path)) { 5571 if(!autoload) { 5572 result.push([name, Opal.require(name)]); 5573 } else { 5574 result.push([name, true]); // do nothing, delegated to a autoloading 5575 } 5576 } 5577 } 5578 5579 return result; 5580 ; 5581 }, -2); 5582 5583 $def(self, '$singleton_class', function $$singleton_class() { 5584 var self = this; 5585 5586 return Opal.get_singleton_class(self); 5587 }); 5588 5589 $def(self, '$sleep', function $$sleep(seconds) { 5590 5591 5592 if (seconds == null) seconds = nil; 5593 5594 if (seconds === nil) { 5595 $Kernel.$raise($$$('TypeError'), "can't convert NilClass into time interval") 5596 } 5597 if (!seconds.$$is_number) { 5598 $Kernel.$raise($$$('TypeError'), "can't convert " + (seconds.$class()) + " into time interval") 5599 } 5600 if (seconds < 0) { 5601 $Kernel.$raise($$$('ArgumentError'), "time interval must be positive") 5602 } 5603 var get_time = Opal.global.performance ? 5604 function() {return performance.now()} : 5605 function() {return new Date()} 5606 5607 var t = get_time(); 5608 while (get_time() - t <= seconds * 1000); 5609 return Math.round(seconds); 5610 ; 5611 }, -1); 5612 5613 $def(self, '$srand', function $$srand(seed) { 5614 5615 5616 if (seed == null) seed = $$('Random').$new_seed(); 5617 return $$$('Random').$srand(seed); 5618 }, -1); 5619 5620 $def(self, '$String', function $$String(str) { 5621 var $ret_or_1 = nil; 5622 5623 if ($truthy(($ret_or_1 = $Opal['$coerce_to?'](str, $$$('String'), "to_str")))) { 5624 return $ret_or_1 5625 } else { 5626 return $Opal['$coerce_to!'](str, $$$('String'), "to_s") 5627 } 5628 }); 5629 5630 $def(self, '$tap', function $$tap() { 5631 var block = $$tap.$$p || nil, self = this; 5632 5633 $$tap.$$p = null; 5634 5635 ; 5636 Opal.yield1(block, self); 5637 return self; 5638 }); 5639 5640 $def(self, '$to_proc', $return_self); 5641 5642 $def(self, '$to_s', function $$to_s() { 5643 var self = this; 5644 5645 return "#<" + (self.$class()) + ":0x" + (self.$__id__().$to_s(16)) + ">" 5646 }); 5647 5648 $def(self, '$catch', function $Kernel_catch$17(tag) { 5649 var $yield = $Kernel_catch$17.$$p || nil, $ret_or_1 = nil, e = nil; 5650 5651 $Kernel_catch$17.$$p = null; 5652 5653 if (tag == null) tag = nil; 5654 try { 5655 5656 tag = ($truthy(($ret_or_1 = tag)) ? ($ret_or_1) : ($Object.$new())); 5657 return Opal.yield1($yield, tag);; 5658 } catch ($err) { 5659 if (Opal.rescue($err, [$$$('UncaughtThrowError')])) {(e = $err) 5660 try { 5661 5662 if ($eqeq(e.$tag(), tag)) { 5663 return e.$value() 5664 }; 5665 return $Kernel.$raise(); 5666 } finally { Opal.pop_exception(); } 5667 } else { throw $err; } 5668 }; 5669 }, -1); 5670 5671 $def(self, '$throw', function $Kernel_throw$18(tag, obj) { 5672 5673 5674 if (obj == null) obj = nil; 5675 return $Kernel.$raise($$$('UncaughtThrowError').$new(tag, obj)); 5676 }, -2); 5677 5678 $def(self, '$open', function $$open($a) { 5679 var block = $$open.$$p || nil, $post_args, args; 5680 5681 $$open.$$p = null; 5682 5683 ; 5684 $post_args = $slice(arguments); 5685 args = $post_args; 5686 return $send($$$('File'), 'open', $to_a(args), block.$to_proc()); 5687 }, -1); 5688 5689 $def(self, '$yield_self', function $$yield_self() { 5690 var $yield = $$yield_self.$$p || nil, self = this; 5691 5692 $$yield_self.$$p = null; 5693 5694 if (!($yield !== nil)) { 5695 return $send(self, 'enum_for', ["yield_self"], $return_val(1)) 5696 }; 5697 return Opal.yield1($yield, self);; 5698 }); 5699 $alias(self, "fail", "raise"); 5700 $alias(self, "kind_of?", "is_a?"); 5701 $alias(self, "object_id", "__id__"); 5702 $alias(self, "public_send", "__send__"); 5703 $alias(self, "send", "__send__"); 5704 $alias(self, "then", "yield_self"); 5705 return $alias(self, "to_enum", "enum_for"); 5706 })('::', $nesting); 5707 return (function($base, $super) { 5708 var self = $klass($base, $super, 'Object'); 5709 5710 5711 5712 delete $Object.$$prototype.$require; 5713 return self.$include($Kernel); 5714 })('::', null); 5715}; 5716 5717Opal.modules["corelib/main"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5718 var $return_val = Opal.return_val, $def = Opal.def, $Object = Opal.Object, $slice = Opal.slice, $Kernel = Opal.Kernel, self = Opal.top, $nesting = [], nil = Opal.nil; 5719 5720 Opal.add_stubs('include,raise'); 5721 return (function(self, $parent_nesting) { 5722 5723 5724 5725 $def(self, '$to_s', $return_val("main")); 5726 5727 $def(self, '$include', function $$include(mod) { 5728 5729 return $Object.$include(mod) 5730 }); 5731 5732 $def(self, '$autoload', function $$autoload($a) { 5733 var $post_args, args; 5734 5735 5736 $post_args = $slice(arguments); 5737 args = $post_args; 5738 return Opal.Object.$autoload.apply(Opal.Object, args);; 5739 }, -1); 5740 return $def(self, '$using', function $$using(mod) { 5741 5742 return $Kernel.$raise("main.using is permitted only at toplevel") 5743 }); 5744 })(Opal.get_singleton_class(self), $nesting) 5745}; 5746 5747Opal.modules["corelib/error/errno"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5748 var $module = Opal.module, $truthy = Opal.truthy, $rb_plus = Opal.rb_plus, $send2 = Opal.send2, $find_super = Opal.find_super, $def = Opal.def, $klass = Opal.klass, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 5749 5750 Opal.add_stubs('+,errno,class,attr_reader'); 5751 5752 (function($base, $parent_nesting) { 5753 var self = $module($base, 'Errno'); 5754 5755 var $nesting = [self].concat($parent_nesting), errors = nil, klass = nil; 5756 5757 5758 errors = [["EINVAL", "Invalid argument", 22], ["EEXIST", "File exists", 17], ["EISDIR", "Is a directory", 21], ["EMFILE", "Too many open files", 24], ["ESPIPE", "Illegal seek", 29], ["EACCES", "Permission denied", 13], ["EPERM", "Operation not permitted", 1], ["ENOENT", "No such file or directory", 2], ["ENAMETOOLONG", "File name too long", 36]]; 5759 klass = nil; 5760 5761 var i; 5762 for (i = 0; i < errors.length; i++) { 5763 (function() { // Create a closure 5764 var class_name = errors[i][0]; 5765 var default_message = errors[i][1]; 5766 var errno = errors[i][2]; 5767 5768 klass = Opal.klass(self, Opal.SystemCallError, class_name); 5769 klass.errno = errno; 5770 5771 (function(self, $parent_nesting) { 5772 5773 return $def(self, '$new', function $new$1(name) { 5774 var $yield = $new$1.$$p || nil, self = this, message = nil; 5775 5776 $new$1.$$p = null; 5777 5778 if (name == null) name = nil; 5779 message = default_message; 5780 if ($truthy(name)) { 5781 message = $rb_plus(message, " - " + (name)) 5782 }; 5783 return $send2(self, $find_super(self, 'new', $new$1, false, true), 'new', [message], null); 5784 }, -1) 5785 })(Opal.get_singleton_class(klass), $nesting) 5786 })(); 5787 } 5788 ; 5789 })('::', $nesting); 5790 return (function($base, $super, $parent_nesting) { 5791 var self = $klass($base, $super, 'SystemCallError'); 5792 5793 var $nesting = [self].concat($parent_nesting); 5794 5795 5796 5797 $def(self, '$errno', function $$errno() { 5798 var self = this; 5799 5800 return self.$class().$errno() 5801 }); 5802 return (function(self, $parent_nesting) { 5803 5804 return self.$attr_reader("errno") 5805 })(Opal.get_singleton_class(self), $nesting); 5806 })('::', $$$('StandardError'), $nesting); 5807}; 5808 5809Opal.modules["corelib/error"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5810 var $klass = Opal.klass, $slice = Opal.slice, $gvars = Opal.gvars, $defs = Opal.defs, $send = Opal.send, $to_a = Opal.to_a, $def = Opal.def, $truthy = Opal.truthy, $hash2 = Opal.hash2, $Kernel = Opal.Kernel, $not = Opal.not, $rb_plus = Opal.rb_plus, $eqeq = Opal.eqeq, $Object = Opal.Object, $ensure_kwargs = Opal.ensure_kwargs, $send2 = Opal.send2, $find_super = Opal.find_super, $module = Opal.module, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 5811 5812 Opal.add_stubs('new,map,backtrace,clone,to_s,merge,tty?,[],include?,raise,dup,empty?,!,caller,shift,+,class,join,cause,full_message,==,reverse,split,autoload,attr_reader,inspect'); 5813 5814 (function($base, $super, $parent_nesting) { 5815 var self = $klass($base, $super, 'Exception'); 5816 5817 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 5818 5819 $proto.message = nil; 5820 5821 Opal.prop(self.$$prototype, '$$is_exception', true); 5822 var stack_trace_limit; 5823 Error.stackTraceLimit = 100; 5824 $defs(self, '$new', function $Exception_new$1($a) { 5825 var $post_args, args, self = this; 5826 if ($gvars["!"] == null) $gvars["!"] = nil; 5827 5828 5829 $post_args = $slice(arguments); 5830 args = $post_args; 5831 5832 var message = (args.length > 0) ? args[0] : nil; 5833 var error = new self.$$constructor(message); 5834 error.name = self.$$name; 5835 error.message = message; 5836 error.cause = $gvars["!"]; 5837 Opal.send(error, error.$initialize, args); 5838 5839 // Error.captureStackTrace() will use .name and .toString to build the 5840 // first line of the stack trace so it must be called after the error 5841 // has been initialized. 5842 // https://nodejs.org/dist/latest-v6.x/docs/api/errors.html 5843 if (Opal.config.enable_stack_trace && Error.captureStackTrace) { 5844 // Passing Kernel.raise will cut the stack trace from that point above 5845 Error.captureStackTrace(error, stack_trace_limit); 5846 } 5847 5848 return error; 5849 ; 5850 }, -1); 5851 stack_trace_limit = self.$new; 5852 $defs(self, '$exception', function $$exception($a) { 5853 var $post_args, args, self = this; 5854 5855 5856 $post_args = $slice(arguments); 5857 args = $post_args; 5858 return $send(self, 'new', $to_a(args)); 5859 }, -1); 5860 5861 $def(self, '$initialize', function $$initialize($a) { 5862 var $post_args, args, self = this; 5863 5864 5865 $post_args = $slice(arguments); 5866 args = $post_args; 5867 return self.message = (args.length > 0) ? args[0] : nil;; 5868 }, -1); 5869 5870 // Convert backtrace from any format to Ruby format 5871 function correct_backtrace(backtrace) { 5872 var new_bt = [], m; 5873 5874 for (var i = 0; i < backtrace.length; i++) { 5875 var loc = backtrace[i]; 5876 if (!loc || !loc.$$is_string) { 5877 /* Do nothing */ 5878 } 5879 /* Chromium format */ 5880 else if ((m = loc.match(/^ at (.*?) \((.*?)\)$/))) { 5881 new_bt.push(m[2] + ":in `" + m[1] + "'"); 5882 } 5883 else if ((m = loc.match(/^ at (.*?)$/))) { 5884 new_bt.push(m[1] + ":in `undefined'"); 5885 } 5886 /* Node format */ 5887 else if ((m = loc.match(/^ from (.*?)$/))) { 5888 new_bt.push(m[1]); 5889 } 5890 /* Mozilla/Apple format */ 5891 else if ((m = loc.match(/^(.*?)@(.*?)$/))) { 5892 new_bt.push(m[2] + ':in `' + m[1] + "'"); 5893 } 5894 } 5895 5896 return new_bt; 5897 } 5898 ; 5899 5900 $def(self, '$backtrace', function $$backtrace() { 5901 var self = this; 5902 5903 5904 if (self.backtrace) { 5905 // nil is a valid backtrace 5906 return self.backtrace; 5907 } 5908 5909 var backtrace = self.stack; 5910 5911 if (typeof(backtrace) !== 'undefined' && backtrace.$$is_string) { 5912 return self.backtrace = correct_backtrace(backtrace.split("\n")); 5913 } 5914 else if (backtrace) { 5915 return self.backtrace = correct_backtrace(backtrace); 5916 } 5917 5918 return []; 5919 5920 }); 5921 5922 $def(self, '$backtrace_locations', function $$backtrace_locations() { 5923 var $a, self = this; 5924 5925 5926 if (self.backtrace_locations) return self.backtrace_locations; 5927 self.backtrace_locations = ($a = self.$backtrace(), ($a === nil || $a == null) ? nil : $send($a, 'map', [], function $$2(loc){ 5928 5929 if (loc == null) loc = nil; 5930 return $$$($$$($$$('Thread'), 'Backtrace'), 'Location').$new(loc);})) 5931 return self.backtrace_locations; 5932 5933 }); 5934 5935 $def(self, '$cause', function $$cause() { 5936 var self = this; 5937 5938 return self.cause || nil; 5939 }); 5940 5941 $def(self, '$exception', function $$exception(str) { 5942 var self = this; 5943 5944 5945 if (str == null) str = nil; 5946 5947 if (str === nil || self === str) { 5948 return self; 5949 } 5950 5951 var cloned = self.$clone(); 5952 cloned.message = str; 5953 if (self.backtrace) cloned.backtrace = self.backtrace.$dup(); 5954 cloned.stack = self.stack; 5955 cloned.cause = self.cause; 5956 return cloned; 5957 ; 5958 }, -1); 5959 5960 $def(self, '$message', function $$message() { 5961 var self = this; 5962 5963 return self.$to_s() 5964 }); 5965 5966 $def(self, '$full_message', function $$full_message(kwargs) { 5967 var $a, $b, self = this, $ret_or_1 = nil, highlight = nil, order = nil, bold_underline = nil, bold = nil, reset = nil, bt = nil, first = nil, msg = nil; 5968 if ($gvars.stderr == null) $gvars.stderr = nil; 5969 5970 5971 if (kwargs == null) kwargs = nil; 5972 if (!$truthy((($a = $$('Hash', 'skip_raise')) ? 'constant' : nil))) { 5973 return "" + (self.message) + "\n" + (self.stack) 5974 }; 5975 kwargs = $hash2(["highlight", "order"], {"highlight": $gvars.stderr['$tty?'](), "order": "top"}).$merge(($truthy(($ret_or_1 = kwargs)) ? ($ret_or_1) : ($hash2([], {})))); 5976 $b = [kwargs['$[]']("highlight"), kwargs['$[]']("order")], (highlight = $b[0]), (order = $b[1]), $b; 5977 if (!$truthy([true, false]['$include?'](highlight))) { 5978 $Kernel.$raise($$$('ArgumentError'), "expected true or false as highlight: " + (highlight)) 5979 }; 5980 if (!$truthy(["top", "bottom"]['$include?'](order))) { 5981 $Kernel.$raise($$$('ArgumentError'), "expected :top or :bottom as order: " + (order)) 5982 }; 5983 if ($truthy(highlight)) { 5984 5985 bold_underline = "\u001b[1;4m"; 5986 bold = "\u001b[1m"; 5987 reset = "\u001b[m"; 5988 } else { 5989 bold_underline = (bold = (reset = "")) 5990 }; 5991 bt = self.$backtrace().$dup(); 5992 if (($not(bt) || ($truthy(bt['$empty?']())))) { 5993 bt = self.$caller() 5994 }; 5995 first = bt.$shift(); 5996 msg = "" + (first) + ": "; 5997 msg = $rb_plus(msg, "" + (bold) + (self.$to_s()) + " (" + (bold_underline) + (self.$class()) + (reset) + (bold) + ")" + (reset) + "\n"); 5998 msg = $rb_plus(msg, $send(bt, 'map', [], function $$3(loc){ 5999 6000 if (loc == null) loc = nil; 6001 return "\tfrom " + (loc) + "\n";}).$join()); 6002 if ($truthy(self.$cause())) { 6003 msg = $rb_plus(msg, self.$cause().$full_message($hash2(["highlight"], {"highlight": highlight}))) 6004 }; 6005 if ($eqeq(order, "bottom")) { 6006 6007 msg = msg.$split("\n").$reverse().$join("\n"); 6008 msg = $rb_plus("" + (bold) + "Traceback" + (reset) + " (most recent call last):\n", msg); 6009 }; 6010 return msg; 6011 }, -1); 6012 6013 $def(self, '$inspect', function $$inspect() { 6014 var self = this, as_str = nil; 6015 6016 6017 as_str = self.$to_s(); 6018 if ($truthy(as_str['$empty?']())) { 6019 return self.$class().$to_s() 6020 } else { 6021 return "#<" + (self.$class().$to_s()) + ": " + (self.$to_s()) + ">" 6022 }; 6023 }); 6024 6025 $def(self, '$set_backtrace', function $$set_backtrace(backtrace) { 6026 var self = this; 6027 6028 6029 var valid = true, i, ii; 6030 6031 if (backtrace === nil) { 6032 self.backtrace = nil; 6033 self.stack = ''; 6034 } else if (backtrace.$$is_string) { 6035 self.backtrace = [backtrace]; 6036 self.stack = ' from ' + backtrace; 6037 } else { 6038 if (backtrace.$$is_array) { 6039 for (i = 0, ii = backtrace.length; i < ii; i++) { 6040 if (!backtrace[i].$$is_string) { 6041 valid = false; 6042 break; 6043 } 6044 } 6045 } else { 6046 valid = false; 6047 } 6048 6049 if (valid === false) { 6050 $Kernel.$raise($$$('TypeError'), "backtrace must be Array of String") 6051 } 6052 6053 self.backtrace = backtrace; 6054 self.stack = $send((backtrace), 'map', [], function $$4(i){ 6055 6056 if (i == null) i = nil; 6057 return $rb_plus(" from ", i);}).join("\n"); 6058 } 6059 6060 return backtrace; 6061 6062 }); 6063 return $def(self, '$to_s', function $$to_s() { 6064 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 6065 6066 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = self.message)) ? (self.message.$to_s()) : ($ret_or_2))))) { 6067 return $ret_or_1 6068 } else { 6069 return self.$class().$to_s() 6070 } 6071 }); 6072 })('::', Error, $nesting); 6073 $klass('::', $$$('Exception'), 'ScriptError'); 6074 $klass('::', $$$('ScriptError'), 'SyntaxError'); 6075 $klass('::', $$$('ScriptError'), 'LoadError'); 6076 $klass('::', $$$('ScriptError'), 'NotImplementedError'); 6077 $klass('::', $$$('Exception'), 'SystemExit'); 6078 $klass('::', $$$('Exception'), 'NoMemoryError'); 6079 $klass('::', $$$('Exception'), 'SignalException'); 6080 $klass('::', $$$('SignalException'), 'Interrupt'); 6081 $klass('::', $$$('Exception'), 'SecurityError'); 6082 $klass('::', $$$('Exception'), 'SystemStackError'); 6083 $klass('::', $$$('Exception'), 'StandardError'); 6084 $klass('::', $$$('StandardError'), 'EncodingError'); 6085 $klass('::', $$$('StandardError'), 'ZeroDivisionError'); 6086 $klass('::', $$$('StandardError'), 'NameError'); 6087 $klass('::', $$$('NameError'), 'NoMethodError'); 6088 $klass('::', $$$('StandardError'), 'RuntimeError'); 6089 $klass('::', $$$('RuntimeError'), 'FrozenError'); 6090 $klass('::', $$$('StandardError'), 'LocalJumpError'); 6091 $klass('::', $$$('StandardError'), 'TypeError'); 6092 $klass('::', $$$('StandardError'), 'ArgumentError'); 6093 $klass('::', $$$('ArgumentError'), 'UncaughtThrowError'); 6094 $klass('::', $$$('StandardError'), 'IndexError'); 6095 $klass('::', $$$('IndexError'), 'StopIteration'); 6096 $klass('::', $$$('StopIteration'), 'ClosedQueueError'); 6097 $klass('::', $$$('IndexError'), 'KeyError'); 6098 $klass('::', $$$('StandardError'), 'RangeError'); 6099 $klass('::', $$$('RangeError'), 'FloatDomainError'); 6100 $klass('::', $$$('StandardError'), 'IOError'); 6101 $klass('::', $$$('IOError'), 'EOFError'); 6102 $klass('::', $$$('StandardError'), 'SystemCallError'); 6103 $klass('::', $$$('StandardError'), 'RegexpError'); 6104 $klass('::', $$$('StandardError'), 'ThreadError'); 6105 $klass('::', $$$('StandardError'), 'FiberError'); 6106 $Object.$autoload("Errno", "corelib/error/errno"); 6107 (function($base, $super) { 6108 var self = $klass($base, $super, 'FrozenError'); 6109 6110 6111 6112 self.$attr_reader("receiver"); 6113 return $def(self, '$initialize', function $$initialize(message, $kwargs) { 6114 var receiver, $yield = $$initialize.$$p || nil, self = this; 6115 6116 $$initialize.$$p = null; 6117 6118 $kwargs = $ensure_kwargs($kwargs); 6119 6120 receiver = $kwargs.$$smap["receiver"];if (receiver == null) receiver = nil; 6121 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 6122 return (self.receiver = receiver); 6123 }, -2); 6124 })('::', $$$('RuntimeError')); 6125 (function($base, $super) { 6126 var self = $klass($base, $super, 'UncaughtThrowError'); 6127 6128 var $proto = self.$$prototype; 6129 6130 $proto.tag = nil; 6131 6132 self.$attr_reader("tag", "value"); 6133 return $def(self, '$initialize', function $$initialize(tag, value) { 6134 var $yield = $$initialize.$$p || nil, self = this; 6135 6136 $$initialize.$$p = null; 6137 6138 if (value == null) value = nil; 6139 self.tag = tag; 6140 self.value = value; 6141 return $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', ["uncaught throw " + (self.tag.$inspect())], null); 6142 }, -2); 6143 })('::', $$$('ArgumentError')); 6144 (function($base, $super) { 6145 var self = $klass($base, $super, 'NameError'); 6146 6147 6148 6149 self.$attr_reader("name"); 6150 return $def(self, '$initialize', function $$initialize(message, name) { 6151 var $yield = $$initialize.$$p || nil, self = this; 6152 6153 $$initialize.$$p = null; 6154 6155 if (name == null) name = nil; 6156 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 6157 return (self.name = name); 6158 }, -2); 6159 })('::', null); 6160 (function($base, $super) { 6161 var self = $klass($base, $super, 'NoMethodError'); 6162 6163 6164 6165 self.$attr_reader("args"); 6166 return $def(self, '$initialize', function $$initialize(message, name, args) { 6167 var $yield = $$initialize.$$p || nil, self = this; 6168 6169 $$initialize.$$p = null; 6170 6171 if (name == null) name = nil; 6172 if (args == null) args = []; 6173 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message, name], null); 6174 return (self.args = args); 6175 }, -2); 6176 })('::', null); 6177 (function($base, $super) { 6178 var self = $klass($base, $super, 'StopIteration'); 6179 6180 6181 return self.$attr_reader("result") 6182 })('::', null); 6183 (function($base, $super) { 6184 var self = $klass($base, $super, 'KeyError'); 6185 6186 var $proto = self.$$prototype; 6187 6188 $proto.receiver = $proto.key = nil; 6189 6190 6191 $def(self, '$initialize', function $$initialize(message, $kwargs) { 6192 var receiver, key, $yield = $$initialize.$$p || nil, self = this; 6193 6194 $$initialize.$$p = null; 6195 6196 $kwargs = $ensure_kwargs($kwargs); 6197 6198 receiver = $kwargs.$$smap["receiver"];if (receiver == null) receiver = nil; 6199 6200 key = $kwargs.$$smap["key"];if (key == null) key = nil; 6201 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 6202 self.receiver = receiver; 6203 return (self.key = key); 6204 }, -2); 6205 6206 $def(self, '$receiver', function $$receiver() { 6207 var self = this, $ret_or_1 = nil; 6208 6209 if ($truthy(($ret_or_1 = self.receiver))) { 6210 return $ret_or_1 6211 } else { 6212 return $Kernel.$raise($$$('ArgumentError'), "no receiver is available") 6213 } 6214 }); 6215 return $def(self, '$key', function $$key() { 6216 var self = this, $ret_or_1 = nil; 6217 6218 if ($truthy(($ret_or_1 = self.key))) { 6219 return $ret_or_1 6220 } else { 6221 return $Kernel.$raise($$$('ArgumentError'), "no key is available") 6222 } 6223 }); 6224 })('::', null); 6225 return (function($base, $parent_nesting) { 6226 var self = $module($base, 'JS'); 6227 6228 var $nesting = [self].concat($parent_nesting); 6229 6230 return ($klass($nesting[0], null, 'Error'), nil) 6231 })('::', $nesting); 6232}; 6233 6234Opal.modules["corelib/constants"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6235 var $const_set = Opal.const_set, nil = Opal.nil, $$$ = Opal.$$$; 6236 6237 6238 $const_set('::', 'RUBY_PLATFORM', "opal"); 6239 $const_set('::', 'RUBY_ENGINE', "opal"); 6240 $const_set('::', 'RUBY_VERSION', "3.2.0"); 6241 $const_set('::', 'RUBY_ENGINE_VERSION', "1.7.3"); 6242 $const_set('::', 'RUBY_RELEASE_DATE', "2023-03-23"); 6243 $const_set('::', 'RUBY_PATCHLEVEL', 0); 6244 $const_set('::', 'RUBY_REVISION', "0"); 6245 $const_set('::', 'RUBY_COPYRIGHT', "opal - Copyright (C) 2011-2023 Adam Beynon and the Opal contributors"); 6246 return $const_set('::', 'RUBY_DESCRIPTION', "opal " + ($$$('RUBY_ENGINE_VERSION')) + " (" + ($$$('RUBY_RELEASE_DATE')) + " revision " + ($$$('RUBY_REVISION')) + ")"); 6247}; 6248 6249Opal.modules["opal/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6250 var $Object = Opal.Object, nil = Opal.nil; 6251 6252 Opal.add_stubs('require'); 6253 6254 $Object.$require("corelib/runtime"); 6255 $Object.$require("corelib/helpers"); 6256 $Object.$require("corelib/module"); 6257 $Object.$require("corelib/class"); 6258 $Object.$require("corelib/basic_object"); 6259 $Object.$require("corelib/kernel"); 6260 $Object.$require("corelib/main"); 6261 $Object.$require("corelib/error"); 6262 return $Object.$require("corelib/constants"); 6263}; 6264 6265Opal.modules["corelib/nil"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6266 var $klass = Opal.klass, $Kernel = Opal.Kernel, $def = Opal.def, $return_val = Opal.return_val, $ensure_kwargs = Opal.ensure_kwargs, $NilClass = Opal.NilClass, $slice = Opal.slice, $truthy = Opal.truthy, $rb_gt = Opal.rb_gt, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 6267 6268 Opal.add_stubs('raise,name,new,>,length,Rational,to_i'); 6269 return (function($base, $super, $parent_nesting) { 6270 var self = $klass($base, $super, 'NilClass'); 6271 6272 var $nesting = [self].concat($parent_nesting); 6273 6274 6275 self.$$prototype.$$meta = self; 6276 (function(self, $parent_nesting) { 6277 6278 6279 6280 $def(self, '$allocate', function $$allocate() { 6281 var self = this; 6282 6283 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 6284 }); 6285 6286 6287 Opal.udef(self, '$' + "new");; 6288 return nil;; 6289 })(Opal.get_singleton_class(self), $nesting); 6290 6291 $def(self, '$!', $return_val(true)); 6292 6293 $def(self, '$&', $return_val(false)); 6294 6295 $def(self, '$|', function $NilClass_$$1(other) { 6296 6297 return other !== false && other !== nil; 6298 }); 6299 6300 $def(self, '$^', function $NilClass_$$2(other) { 6301 6302 return other !== false && other !== nil; 6303 }); 6304 6305 $def(self, '$==', function $NilClass_$eq_eq$3(other) { 6306 6307 return other === nil; 6308 }); 6309 6310 $def(self, '$dup', $return_val(nil)); 6311 6312 $def(self, '$clone', function $$clone($kwargs) { 6313 var freeze; 6314 6315 6316 $kwargs = $ensure_kwargs($kwargs); 6317 6318 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = true; 6319 return nil; 6320 }, -1); 6321 6322 $def(self, '$inspect', $return_val("nil")); 6323 6324 $def(self, '$nil?', $return_val(true)); 6325 6326 $def(self, '$singleton_class', function $$singleton_class() { 6327 6328 return $NilClass 6329 }); 6330 6331 $def(self, '$to_a', function $$to_a() { 6332 6333 return [] 6334 }); 6335 6336 $def(self, '$to_h', function $$to_h() { 6337 6338 return Opal.hash(); 6339 }); 6340 6341 $def(self, '$to_i', $return_val(0)); 6342 6343 $def(self, '$to_s', $return_val("")); 6344 6345 $def(self, '$to_c', function $$to_c() { 6346 6347 return $$$('Complex').$new(0, 0) 6348 }); 6349 6350 $def(self, '$rationalize', function $$rationalize($a) { 6351 var $post_args, args; 6352 6353 6354 $post_args = $slice(arguments); 6355 args = $post_args; 6356 if ($truthy($rb_gt(args.$length(), 1))) { 6357 $Kernel.$raise($$$('ArgumentError')) 6358 }; 6359 return $Kernel.$Rational(0, 1); 6360 }, -1); 6361 6362 $def(self, '$to_r', function $$to_r() { 6363 6364 return $Kernel.$Rational(0, 1) 6365 }); 6366 6367 $def(self, '$instance_variables', function $$instance_variables() { 6368 6369 return [] 6370 }); 6371 return $alias(self, "to_f", "to_i"); 6372 })('::', null, $nesting) 6373}; 6374 6375Opal.modules["corelib/boolean"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6376 "use strict"; 6377 var $klass = Opal.klass, $Kernel = Opal.Kernel, $def = Opal.def, $return_self = Opal.return_self, $ensure_kwargs = Opal.ensure_kwargs, $slice = Opal.slice, $truthy = Opal.truthy, $send2 = Opal.send2, $find_super = Opal.find_super, $to_a = Opal.to_a, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 6378 6379 Opal.add_stubs('raise,name,==,to_s,__id__'); 6380 6381 (function($base, $super, $parent_nesting) { 6382 var self = $klass($base, $super, 'Boolean'); 6383 6384 var $nesting = [self].concat($parent_nesting); 6385 6386 6387 Opal.prop(self.$$prototype, '$$is_boolean', true); 6388 6389 var properties = ['$$class', '$$meta']; 6390 6391 for (var i = 0; i < properties.length; i++) { 6392 Object.defineProperty(self.$$prototype, properties[i], { 6393 configurable: true, 6394 enumerable: false, 6395 get: function() { 6396 return this == true ? Opal.TrueClass : 6397 this == false ? Opal.FalseClass : 6398 Opal.Boolean; 6399 } 6400 }); 6401 } 6402 6403 Object.defineProperty(self.$$prototype, "$$id", { 6404 configurable: true, 6405 enumerable: false, 6406 get: function() { 6407 return this == true ? 2 : 6408 this == false ? 0 : 6409 nil; 6410 } 6411 }); 6412 ; 6413 (function(self, $parent_nesting) { 6414 6415 6416 6417 $def(self, '$allocate', function $$allocate() { 6418 var self = this; 6419 6420 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 6421 }); 6422 6423 6424 Opal.udef(self, '$' + "new");; 6425 return nil;; 6426 })(Opal.get_singleton_class(self), $nesting); 6427 6428 $def(self, '$__id__', function $$__id__() { 6429 var self = this; 6430 6431 return self.valueOf() ? 2 : 0; 6432 }); 6433 6434 $def(self, '$!', function $Boolean_$excl$1() { 6435 var self = this; 6436 6437 return self != true; 6438 }); 6439 6440 $def(self, '$&', function $Boolean_$$2(other) { 6441 var self = this; 6442 6443 return (self == true) ? (other !== false && other !== nil) : false; 6444 }); 6445 6446 $def(self, '$|', function $Boolean_$$3(other) { 6447 var self = this; 6448 6449 return (self == true) ? true : (other !== false && other !== nil); 6450 }); 6451 6452 $def(self, '$^', function $Boolean_$$4(other) { 6453 var self = this; 6454 6455 return (self == true) ? (other === false || other === nil) : (other !== false && other !== nil); 6456 }); 6457 6458 $def(self, '$==', function $Boolean_$eq_eq$5(other) { 6459 var self = this; 6460 6461 return (self == true) === other.valueOf(); 6462 }); 6463 6464 $def(self, '$singleton_class', function $$singleton_class() { 6465 var self = this; 6466 6467 return self.$$meta; 6468 }); 6469 6470 $def(self, '$to_s', function $$to_s() { 6471 var self = this; 6472 6473 return (self == true) ? 'true' : 'false'; 6474 }); 6475 6476 $def(self, '$dup', $return_self); 6477 6478 $def(self, '$clone', function $$clone($kwargs) { 6479 var freeze, self = this; 6480 6481 6482 $kwargs = $ensure_kwargs($kwargs); 6483 6484 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = true; 6485 return self; 6486 }, -1); 6487 6488 $def(self, '$method_missing', function $$method_missing(method, $a) { 6489 var block = $$method_missing.$$p || nil, $post_args, args, self = this; 6490 6491 $$method_missing.$$p = null; 6492 6493 ; 6494 $post_args = $slice(arguments, 1); 6495 args = $post_args; 6496 var body = self.$$class.$$prototype[Opal.jsid(method)]; 6497 if (!$truthy(typeof body !== 'undefined' && !body.$$stub)) { 6498 $send2(self, $find_super(self, 'method_missing', $$method_missing, false, true), 'method_missing', [method].concat($to_a(args)), block) 6499 }; 6500 return Opal.send(self, body, args, block); 6501 }, -2); 6502 6503 $def(self, '$respond_to_missing?', function $Boolean_respond_to_missing$ques$6(method, _include_all) { 6504 var self = this; 6505 6506 6507 if (_include_all == null) _include_all = false; 6508 var body = self.$$class.$$prototype[Opal.jsid(method)]; 6509 return typeof body !== 'undefined' && !body.$$stub;; 6510 }, -2); 6511 $alias(self, "eql?", "=="); 6512 $alias(self, "equal?", "=="); 6513 $alias(self, "inspect", "to_s"); 6514 return $alias(self, "object_id", "__id__"); 6515 })('::', Boolean, $nesting); 6516 $klass('::', $$$('Boolean'), 'TrueClass'); 6517 return ($klass('::', $$$('Boolean'), 'FalseClass'), nil); 6518}; 6519 6520Opal.modules["corelib/comparable"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6521 var $truthy = Opal.truthy, $module = Opal.module, $rb_gt = Opal.rb_gt, $rb_lt = Opal.rb_lt, $eqeqeq = Opal.eqeqeq, $Kernel = Opal.Kernel, $def = Opal.def, nil = Opal.nil, $$$ = Opal.$$$; 6522 6523 Opal.add_stubs('>,<,===,raise,class,<=>,equal?'); 6524 return (function($base) { 6525 var self = $module($base, 'Comparable'); 6526 6527 var $ret_or_1 = nil; 6528 6529 6530 6531 function normalize(what) { 6532 if (Opal.is_a(what, Opal.Integer)) { return what; } 6533 6534 if ($rb_gt(what, 0)) { return 1; } 6535 if ($rb_lt(what, 0)) { return -1; } 6536 return 0; 6537 } 6538 6539 function fail_comparison(lhs, rhs) { 6540 var class_name; 6541 (($eqeqeq(nil, ($ret_or_1 = rhs)) || (($eqeqeq(true, $ret_or_1) || (($eqeqeq(false, $ret_or_1) || (($eqeqeq($$$('Integer'), $ret_or_1) || ($eqeqeq($$$('Float'), $ret_or_1))))))))) ? (class_name = rhs.$inspect()) : (class_name = rhs.$$class)) 6542 $Kernel.$raise($$$('ArgumentError'), "comparison of " + ((lhs).$class()) + " with " + (class_name) + " failed") 6543 } 6544 6545 function cmp_or_fail(lhs, rhs) { 6546 var cmp = (lhs)['$<=>'](rhs); 6547 if (!$truthy(cmp)) fail_comparison(lhs, rhs); 6548 return normalize(cmp); 6549 } 6550 ; 6551 6552 $def(self, '$==', function $Comparable_$eq_eq$1(other) { 6553 var self = this, cmp = nil; 6554 6555 6556 if ($truthy(self['$equal?'](other))) { 6557 return true 6558 }; 6559 6560 if (self["$<=>"] == Opal.Kernel["$<=>"]) { 6561 return false; 6562 } 6563 6564 // check for infinite recursion 6565 if (self.$$comparable) { 6566 self.$$comparable = false; 6567 return false; 6568 } 6569 ; 6570 if (!$truthy((cmp = self['$<=>'](other)))) { 6571 return false 6572 }; 6573 return normalize(cmp) == 0;; 6574 }); 6575 6576 $def(self, '$>', function $Comparable_$gt$2(other) { 6577 var self = this; 6578 6579 return cmp_or_fail(self, other) > 0; 6580 }); 6581 6582 $def(self, '$>=', function $Comparable_$gt_eq$3(other) { 6583 var self = this; 6584 6585 return cmp_or_fail(self, other) >= 0; 6586 }); 6587 6588 $def(self, '$<', function $Comparable_$lt$4(other) { 6589 var self = this; 6590 6591 return cmp_or_fail(self, other) < 0; 6592 }); 6593 6594 $def(self, '$<=', function $Comparable_$lt_eq$5(other) { 6595 var self = this; 6596 6597 return cmp_or_fail(self, other) <= 0; 6598 }); 6599 6600 $def(self, '$between?', function $Comparable_between$ques$6(min, max) { 6601 var self = this; 6602 6603 6604 if ($rb_lt(self, min)) { 6605 return false 6606 }; 6607 if ($rb_gt(self, max)) { 6608 return false 6609 }; 6610 return true; 6611 }); 6612 return $def(self, '$clamp', function $$clamp(min, max) { 6613 var self = this; 6614 6615 6616 if (max == null) max = nil; 6617 6618 var c, excl; 6619 6620 if (max === nil) { 6621 // We are dealing with a new Ruby 2.7 behaviour that we are able to 6622 // provide a single Range argument instead of 2 Comparables. 6623 6624 if (!Opal.is_a(min, Opal.Range)) { 6625 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (min.$class()) + " (expected Range)") 6626 } 6627 6628 excl = min.excl; 6629 max = min.end; 6630 min = min.begin; 6631 6632 if (max !== nil && excl) { 6633 $Kernel.$raise($$$('ArgumentError'), "cannot clamp with an exclusive range") 6634 } 6635 } 6636 6637 if (min !== nil && max !== nil && cmp_or_fail(min, max) > 0) { 6638 $Kernel.$raise($$$('ArgumentError'), "min argument must be smaller than max argument") 6639 } 6640 6641 if (min !== nil) { 6642 c = cmp_or_fail(self, min); 6643 6644 if (c == 0) return self; 6645 if (c < 0) return min; 6646 } 6647 6648 if (max !== nil) { 6649 c = cmp_or_fail(self, max); 6650 6651 if (c > 0) return max; 6652 } 6653 6654 return self; 6655 ; 6656 }, -2); 6657 })('::') 6658}; 6659 6660Opal.modules["corelib/regexp"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6661 var $coerce_to = Opal.coerce_to, $prop = Opal.prop, $freeze = Opal.freeze, $klass = Opal.klass, $const_set = Opal.const_set, $send2 = Opal.send2, $find_super = Opal.find_super, $def = Opal.def, $truthy = Opal.truthy, $gvars = Opal.gvars, $slice = Opal.slice, $Kernel = Opal.Kernel, $Opal = Opal.Opal, $alias = Opal.alias, $send = Opal.send, $hash2 = Opal.hash2, $rb_plus = Opal.rb_plus, $ensure_kwargs = Opal.ensure_kwargs, $rb_ge = Opal.rb_ge, $to_a = Opal.to_a, $eqeqeq = Opal.eqeqeq, $rb_minus = Opal.rb_minus, $return_ivar = Opal.return_ivar, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 6662 6663 Opal.add_stubs('nil?,[],raise,escape,options,to_str,new,join,coerce_to!,!,match,coerce_to?,begin,frozen?,uniq,map,scan,source,to_proc,transform_values,group_by,each_with_index,+,last,=~,==,attr_reader,>=,length,is_a?,include?,names,regexp,named_captures,===,captures,-,inspect,empty?,each,to_a'); 6664 6665 $klass('::', $$$('StandardError'), 'RegexpError'); 6666 (function($base, $super, $parent_nesting) { 6667 var self = $klass($base, $super, 'Regexp'); 6668 6669 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 6670 6671 6672 $const_set(self, 'IGNORECASE', 1); 6673 $const_set(self, 'EXTENDED', 2); 6674 $const_set(self, 'MULTILINE', 4); 6675 Opal.prop(self.$$prototype, '$$is_regexp', true); 6676 (function(self, $parent_nesting) { 6677 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 6678 6679 6680 6681 $def(self, '$allocate', function $$allocate() { 6682 var $yield = $$allocate.$$p || nil, self = this, allocated = nil; 6683 6684 $$allocate.$$p = null; 6685 6686 allocated = $send2(self, $find_super(self, 'allocate', $$allocate, false, true), 'allocate', [], $yield); 6687 allocated.uninitialized = true; 6688 return allocated; 6689 }); 6690 6691 $def(self, '$escape', function $$escape(string) { 6692 6693 return Opal.escape_regexp(string); 6694 }); 6695 6696 $def(self, '$last_match', function $$last_match(n) { 6697 if ($gvars["~"] == null) $gvars["~"] = nil; 6698 6699 6700 if (n == null) n = nil; 6701 if ($truthy(n['$nil?']())) { 6702 return $gvars["~"] 6703 } else if ($truthy($gvars["~"])) { 6704 return $gvars["~"]['$[]'](n) 6705 } else { 6706 return nil 6707 }; 6708 }, -1); 6709 6710 $def(self, '$union', function $$union($a) { 6711 var $post_args, parts, self = this; 6712 6713 6714 $post_args = $slice(arguments); 6715 parts = $post_args; 6716 6717 var is_first_part_array, quoted_validated, part, options, each_part_options; 6718 if (parts.length == 0) { 6719 return /(?!)/; 6720 } 6721 // return fast if there's only one element 6722 if (parts.length == 1 && parts[0].$$is_regexp) { 6723 return parts[0]; 6724 } 6725 // cover the 2 arrays passed as arguments case 6726 is_first_part_array = parts[0].$$is_array; 6727 if (parts.length > 1 && is_first_part_array) { 6728 $Kernel.$raise($$$('TypeError'), "no implicit conversion of Array into String") 6729 } 6730 // deal with splat issues (related to https://github.com/opal/opal/issues/858) 6731 if (is_first_part_array) { 6732 parts = parts[0]; 6733 } 6734 options = undefined; 6735 quoted_validated = []; 6736 for (var i=0; i < parts.length; i++) { 6737 part = parts[i]; 6738 if (part.$$is_string) { 6739 quoted_validated.push(self.$escape(part)); 6740 } 6741 else if (part.$$is_regexp) { 6742 each_part_options = (part).$options(); 6743 if (options != undefined && options != each_part_options) { 6744 $Kernel.$raise($$$('TypeError'), "All expressions must use the same options") 6745 } 6746 options = each_part_options; 6747 quoted_validated.push('('+part.source+')'); 6748 } 6749 else { 6750 quoted_validated.push(self.$escape((part).$to_str())); 6751 } 6752 } 6753 ; 6754 return self.$new((quoted_validated).$join("|"), options); 6755 }, -1); 6756 6757 $def(self, '$new', function $new$1(regexp, options) { 6758 6759 6760 ; 6761 6762 if (regexp.$$is_regexp) { 6763 return new RegExp(regexp); 6764 } 6765 6766 regexp = $Opal['$coerce_to!'](regexp, $$$('String'), "to_str"); 6767 6768 if (regexp.charAt(regexp.length - 1) === '\\' && regexp.charAt(regexp.length - 2) !== '\\') { 6769 $Kernel.$raise($$$('RegexpError'), "too short escape sequence: /" + (regexp) + "/") 6770 } 6771 6772 regexp = regexp.replace('\\A', '^').replace('\\z', '$') 6773 6774 if (options === undefined || options['$!']()) { 6775 return new RegExp(regexp); 6776 } 6777 6778 if (options.$$is_number) { 6779 var temp = ''; 6780 if ($$('IGNORECASE') & options) { temp += 'i'; } 6781 if ($$('MULTILINE') & options) { temp += 'm'; } 6782 options = temp; 6783 } 6784 else { 6785 options = 'i'; 6786 } 6787 6788 return new RegExp(regexp, options); 6789 ; 6790 }, -2); 6791 $alias(self, "compile", "new"); 6792 return $alias(self, "quote", "escape"); 6793 })(Opal.get_singleton_class(self), $nesting); 6794 6795 $def(self, '$==', function $Regexp_$eq_eq$2(other) { 6796 var self = this; 6797 6798 return other instanceof RegExp && self.toString() === other.toString(); 6799 }); 6800 6801 $def(self, '$===', function $Regexp_$eq_eq_eq$3(string) { 6802 var self = this; 6803 6804 return self.$match($Opal['$coerce_to?'](string, $$$('String'), "to_str")) !== nil 6805 }); 6806 6807 $def(self, '$=~', function $Regexp_$eq_tilde$4(string) { 6808 var self = this, $ret_or_1 = nil; 6809 if ($gvars["~"] == null) $gvars["~"] = nil; 6810 6811 if ($truthy(($ret_or_1 = self.$match(string)))) { 6812 return $gvars["~"].$begin(0) 6813 } else { 6814 return $ret_or_1 6815 } 6816 }); 6817 6818 $def(self, '$freeze', function $$freeze() { 6819 var self = this; 6820 6821 6822 if ($truthy(self['$frozen?']())) { 6823 return self 6824 }; 6825 6826 if (!self.hasOwnProperty('$$g')) { $prop(self, '$$g', null); } 6827 if (!self.hasOwnProperty('$$gm')) { $prop(self, '$$gm', null); } 6828 6829 return $freeze(self); 6830 ; 6831 }); 6832 6833 $def(self, '$inspect', function $$inspect() { 6834 var self = this; 6835 6836 6837 var regexp_format = /^\/(.*)\/([^\/]*)$/; 6838 var value = self.toString(); 6839 var matches = regexp_format.exec(value); 6840 if (matches) { 6841 var regexp_pattern = matches[1]; 6842 var regexp_flags = matches[2]; 6843 var chars = regexp_pattern.split(''); 6844 var chars_length = chars.length; 6845 var char_escaped = false; 6846 var regexp_pattern_escaped = ''; 6847 for (var i = 0; i < chars_length; i++) { 6848 var current_char = chars[i]; 6849 if (!char_escaped && current_char == '/') { 6850 regexp_pattern_escaped = regexp_pattern_escaped.concat('\\'); 6851 } 6852 regexp_pattern_escaped = regexp_pattern_escaped.concat(current_char); 6853 if (current_char == '\\') { 6854 if (char_escaped) { 6855 // does not over escape 6856 char_escaped = false; 6857 } else { 6858 char_escaped = true; 6859 } 6860 } else { 6861 char_escaped = false; 6862 } 6863 } 6864 return '/' + regexp_pattern_escaped + '/' + regexp_flags; 6865 } else { 6866 return value; 6867 } 6868 6869 }); 6870 6871 $def(self, '$match', function $$match(string, pos) { 6872 var block = $$match.$$p || nil, self = this; 6873 if ($gvars["~"] == null) $gvars["~"] = nil; 6874 6875 $$match.$$p = null; 6876 6877 ; 6878 ; 6879 6880 if (self.uninitialized) { 6881 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp") 6882 } 6883 6884 if (pos === undefined) { 6885 if (string === nil) return ($gvars["~"] = nil); 6886 var m = self.exec($coerce_to(string, $$$('String'), 'to_str')); 6887 if (m) { 6888 ($gvars["~"] = $$$('MatchData').$new(self, m)); 6889 return block === nil ? $gvars["~"] : Opal.yield1(block, $gvars["~"]); 6890 } else { 6891 return ($gvars["~"] = nil); 6892 } 6893 } 6894 6895 pos = $coerce_to(pos, $$$('Integer'), 'to_int'); 6896 6897 if (string === nil) { 6898 return ($gvars["~"] = nil); 6899 } 6900 6901 string = $coerce_to(string, $$$('String'), 'to_str'); 6902 6903 if (pos < 0) { 6904 pos += string.length; 6905 if (pos < 0) { 6906 return ($gvars["~"] = nil); 6907 } 6908 } 6909 6910 // global RegExp maintains state, so not using self/this 6911 var md, re = Opal.global_regexp(self); 6912 6913 while (true) { 6914 md = re.exec(string); 6915 if (md === null) { 6916 return ($gvars["~"] = nil); 6917 } 6918 if (md.index >= pos) { 6919 ($gvars["~"] = $$$('MatchData').$new(re, md)); 6920 return block === nil ? $gvars["~"] : Opal.yield1(block, $gvars["~"]); 6921 } 6922 re.lastIndex = md.index + 1; 6923 } 6924 ; 6925 }, -2); 6926 6927 $def(self, '$match?', function $Regexp_match$ques$5(string, pos) { 6928 var self = this; 6929 6930 6931 ; 6932 6933 if (self.uninitialized) { 6934 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp") 6935 } 6936 6937 if (pos === undefined) { 6938 return string === nil ? false : self.test($coerce_to(string, $$$('String'), 'to_str')); 6939 } 6940 6941 pos = $coerce_to(pos, $$$('Integer'), 'to_int'); 6942 6943 if (string === nil) { 6944 return false; 6945 } 6946 6947 string = $coerce_to(string, $$$('String'), 'to_str'); 6948 6949 if (pos < 0) { 6950 pos += string.length; 6951 if (pos < 0) { 6952 return false; 6953 } 6954 } 6955 6956 // global RegExp maintains state, so not using self/this 6957 var md, re = Opal.global_regexp(self); 6958 6959 md = re.exec(string); 6960 if (md === null || md.index < pos) { 6961 return false; 6962 } else { 6963 return true; 6964 } 6965 ; 6966 }, -2); 6967 6968 $def(self, '$names', function $$names() { 6969 var self = this; 6970 6971 return $send(self.$source().$scan(/\(?<(\w+)>/, $hash2(["no_matchdata"], {"no_matchdata": true})), 'map', [], "first".$to_proc()).$uniq() 6972 }); 6973 6974 $def(self, '$named_captures', function $$named_captures() { 6975 var self = this; 6976 6977 return $send($send($send(self.$source().$scan(/\(?<(\w+)>/, $hash2(["no_matchdata"], {"no_matchdata": true})), 'map', [], "first".$to_proc()).$each_with_index(), 'group_by', [], "first".$to_proc()), 'transform_values', [], function $$6(i){ 6978 6979 if (i == null) i = nil; 6980 return $send(i, 'map', [], function $$7(j){ 6981 6982 if (j == null) j = nil; 6983 return $rb_plus(j.$last(), 1);});}) 6984 }); 6985 6986 $def(self, '$~', function $Regexp_$$8() { 6987 var self = this; 6988 if ($gvars._ == null) $gvars._ = nil; 6989 6990 return self['$=~']($gvars._) 6991 }); 6992 6993 $def(self, '$source', function $$source() { 6994 var self = this; 6995 6996 return self.source; 6997 }); 6998 6999 $def(self, '$options', function $$options() { 7000 var self = this; 7001 7002 7003 if (self.uninitialized) { 7004 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp") 7005 } 7006 var result = 0; 7007 // should be supported in IE6 according to https://msdn.microsoft.com/en-us/library/7f5z26w4(v=vs.94).aspx 7008 if (self.multiline) { 7009 result |= $$('MULTILINE'); 7010 } 7011 if (self.ignoreCase) { 7012 result |= $$('IGNORECASE'); 7013 } 7014 return result; 7015 7016 }); 7017 7018 $def(self, '$casefold?', function $Regexp_casefold$ques$9() { 7019 var self = this; 7020 7021 return self.ignoreCase; 7022 }); 7023 $alias(self, "eql?", "=="); 7024 return $alias(self, "to_s", "source"); 7025 })('::', RegExp, $nesting); 7026 return (function($base, $super, $parent_nesting) { 7027 var self = $klass($base, $super, 'MatchData'); 7028 7029 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 7030 7031 $proto.matches = nil; 7032 7033 self.$attr_reader("post_match", "pre_match", "regexp", "string"); 7034 7035 $def(self, '$initialize', function $$initialize(regexp, match_groups, $kwargs) { 7036 var no_matchdata, self = this; 7037 7038 7039 $kwargs = $ensure_kwargs($kwargs); 7040 7041 no_matchdata = $kwargs.$$smap["no_matchdata"];if (no_matchdata == null) no_matchdata = false; 7042 if (!$truthy(no_matchdata)) { 7043 $gvars["~"] = self 7044 }; 7045 self.regexp = regexp; 7046 self.begin = match_groups.index; 7047 self.string = match_groups.input; 7048 self.pre_match = match_groups.input.slice(0, match_groups.index); 7049 self.post_match = match_groups.input.slice(match_groups.index + match_groups[0].length); 7050 self.matches = []; 7051 7052 for (var i = 0, length = match_groups.length; i < length; i++) { 7053 var group = match_groups[i]; 7054 7055 if (group == null) { 7056 self.matches.push(nil); 7057 } 7058 else { 7059 self.matches.push(group); 7060 } 7061 } 7062 ; 7063 }, -3); 7064 7065 $def(self, '$match', function $$match(idx) { 7066 var self = this, match = nil; 7067 7068 if ($truthy((match = self['$[]'](idx)))) { 7069 return match 7070 } else if (($truthy(idx['$is_a?']($$('Integer'))) && ($truthy($rb_ge(idx, self.$length()))))) { 7071 return $Kernel.$raise($$$('IndexError'), "index " + (idx) + " out of matches") 7072 } else { 7073 return nil 7074 } 7075 }); 7076 7077 $def(self, '$match_length', function $$match_length(idx) { 7078 var $a, self = this; 7079 7080 return ($a = self.$match(idx), ($a === nil || $a == null) ? nil : $a.$length()) 7081 }); 7082 7083 $def(self, '$[]', function $MatchData_$$$10($a) { 7084 var $post_args, args, self = this; 7085 7086 7087 $post_args = $slice(arguments); 7088 args = $post_args; 7089 7090 if (args[0].$$is_string) { 7091 if (self.$regexp().$names()['$include?'](args['$[]'](0))['$!']()) { 7092 $Kernel.$raise($$$('IndexError'), "undefined group name reference: " + (args['$[]'](0))) 7093 } 7094 return self.$named_captures()['$[]'](args['$[]'](0)) 7095 } 7096 else { 7097 return $send(self.matches, '[]', $to_a(args)) 7098 } 7099 ; 7100 }, -1); 7101 7102 $def(self, '$offset', function $$offset(n) { 7103 var self = this; 7104 7105 7106 if (n !== 0) { 7107 $Kernel.$raise($$$('ArgumentError'), "MatchData#offset only supports 0th element") 7108 } 7109 return [self.begin, self.begin + self.matches[n].length]; 7110 7111 }); 7112 7113 $def(self, '$==', function $MatchData_$eq_eq$11(other) { 7114 var self = this, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, $ret_or_4 = nil; 7115 7116 7117 if (!$eqeqeq($$$('MatchData'), other)) { 7118 return false 7119 }; 7120 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = ($truthy(($ret_or_3 = ($truthy(($ret_or_4 = self.string == other.string)) ? (self.regexp.toString() == other.regexp.toString()) : ($ret_or_4)))) ? (self.pre_match == other.pre_match) : ($ret_or_3)))) ? (self.post_match == other.post_match) : ($ret_or_2))))) { 7121 return self.begin == other.begin; 7122 } else { 7123 return $ret_or_1 7124 }; 7125 }); 7126 7127 $def(self, '$begin', function $$begin(n) { 7128 var self = this; 7129 7130 7131 if (n !== 0) { 7132 $Kernel.$raise($$$('ArgumentError'), "MatchData#begin only supports 0th element") 7133 } 7134 return self.begin; 7135 7136 }); 7137 7138 $def(self, '$end', function $$end(n) { 7139 var self = this; 7140 7141 7142 if (n !== 0) { 7143 $Kernel.$raise($$$('ArgumentError'), "MatchData#end only supports 0th element") 7144 } 7145 return self.begin + self.matches[n].length; 7146 7147 }); 7148 7149 $def(self, '$captures', function $$captures() { 7150 var self = this; 7151 7152 return self.matches.slice(1) 7153 }); 7154 7155 $def(self, '$named_captures', function $$named_captures() { 7156 var self = this, matches = nil; 7157 7158 7159 matches = self.$captures(); 7160 return $send(self.$regexp().$named_captures(), 'transform_values', [], function $$12(i){ 7161 7162 if (i == null) i = nil; 7163 return matches['$[]']($rb_minus(i.$last(), 1));}); 7164 }); 7165 7166 $def(self, '$names', function $$names() { 7167 var self = this; 7168 7169 return self.$regexp().$names() 7170 }); 7171 7172 $def(self, '$inspect', function $$inspect() { 7173 var self = this; 7174 7175 7176 var str = "#<MatchData " + (self.matches[0]).$inspect(); 7177 7178 if (self.$regexp().$names()['$empty?']()) { 7179 for (var i = 1, length = self.matches.length; i < length; i++) { 7180 str += " " + i + ":" + (self.matches[i]).$inspect(); 7181 } 7182 } 7183 else { 7184 $send(self.$named_captures(), 'each', [], function $$13(k, v){ 7185 7186 if (k == null) k = nil; 7187 if (v == null) v = nil; 7188 return str += " " + k + ":" + v.$inspect();}) 7189 } 7190 7191 return str + ">"; 7192 7193 }); 7194 7195 $def(self, '$length', function $$length() { 7196 var self = this; 7197 7198 return self.matches.length 7199 }); 7200 7201 $def(self, '$to_a', $return_ivar("matches")); 7202 7203 $def(self, '$to_s', function $$to_s() { 7204 var self = this; 7205 7206 return self.matches[0] 7207 }); 7208 7209 $def(self, '$values_at', function $$values_at($a) { 7210 var $post_args, args, self = this; 7211 7212 7213 $post_args = $slice(arguments); 7214 args = $post_args; 7215 7216 var i, a, index, values = []; 7217 7218 for (i = 0; i < args.length; i++) { 7219 7220 if (args[i].$$is_range) { 7221 a = (args[i]).$to_a(); 7222 a.unshift(i, 1); 7223 Array.prototype.splice.apply(args, a); 7224 } 7225 7226 index = $Opal['$coerce_to!'](args[i], $$$('Integer'), "to_int"); 7227 7228 if (index < 0) { 7229 index += self.matches.length; 7230 if (index < 0) { 7231 values.push(nil); 7232 continue; 7233 } 7234 } 7235 7236 values.push(self.matches[index]); 7237 } 7238 7239 return values; 7240 ; 7241 }, -1); 7242 $alias(self, "eql?", "=="); 7243 return $alias(self, "size", "length"); 7244 })($nesting[0], null, $nesting); 7245}; 7246 7247Opal.modules["corelib/string"] = function(Opal) {/* Generated by Opal 1.7.3 */ 7248 var $coerce_to = Opal.coerce_to, $respond_to = Opal.respond_to, $global_multiline_regexp = Opal.global_multiline_regexp, $prop = Opal.prop, $klass = Opal.klass, $def = Opal.def, $Opal = Opal.Opal, $defs = Opal.defs, $slice = Opal.slice, $send = Opal.send, $to_a = Opal.to_a, $extract_kwargs = Opal.extract_kwargs, $ensure_kwargs = Opal.ensure_kwargs, $eqeqeq = Opal.eqeqeq, $Kernel = Opal.Kernel, $truthy = Opal.truthy, $gvars = Opal.gvars, $rb_divide = Opal.rb_divide, $rb_plus = Opal.rb_plus, $eqeq = Opal.eqeq, $hash2 = Opal.hash2, $alias = Opal.alias, $const_set = Opal.const_set, self = Opal.top, $nesting = [], $$ = Opal.$r($nesting), nil = Opal.nil, $$$ = Opal.$$$; 7249 7250 Opal.add_stubs('require,include,coerce_to?,initialize,===,format,raise,respond_to?,to_s,to_str,<=>,==,=~,new,force_encoding,casecmp,empty?,ljust,ceil,/,+,rjust,floor,coerce_to!,nil?,class,copy_singleton_methods,initialize_clone,initialize_dup,enum_for,chomp,[],to_i,length,each_line,to_proc,to_a,match,match?,captures,proc,succ,escape,include?,upcase,unicode_normalize,dup,__id__,next,intern,pristine'); 7251 7252 self.$require("corelib/comparable"); 7253 self.$require("corelib/regexp"); 7254 (function($base, $super, $parent_nesting) { 7255 var self = $klass($base, $super, 'String'); 7256 7257 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 7258 7259 7260 self.$include($$$('Comparable')); 7261 7262 Opal.prop(self.$$prototype, '$$is_string', true); 7263 ; 7264 7265 $def(self, '$__id__', function $$__id__() { 7266 var self = this; 7267 7268 return self.toString(); 7269 }); 7270 $defs(self, '$try_convert', function $$try_convert(what) { 7271 7272 return $Opal['$coerce_to?'](what, $$$('String'), "to_str") 7273 }); 7274 $defs(self, '$new', function $String_new$1($a) { 7275 var $post_args, args, self = this; 7276 7277 7278 $post_args = $slice(arguments); 7279 args = $post_args; 7280 7281 var str = args[0] || ""; 7282 var opts = args[args.length-1]; 7283 str = $coerce_to(str, $$$('String'), 'to_str'); 7284 if (opts && opts.$$is_hash) { 7285 if (opts.$$smap.encoding) str = str.$force_encoding(opts.$$smap.encoding); 7286 } 7287 str = new self.$$constructor(str); 7288 if (!str.$initialize.$$pristine) $send((str), 'initialize', $to_a(args)); 7289 return str; 7290 ; 7291 }, -1); 7292 7293 $def(self, '$initialize', function $$initialize($a, $b) { 7294 var $post_args, $kwargs, str, encoding, capacity; 7295 7296 7297 $post_args = $slice(arguments); 7298 $kwargs = $extract_kwargs($post_args); 7299 $kwargs = $ensure_kwargs($kwargs); 7300 7301 if ($post_args.length > 0) str = $post_args.shift();; 7302 7303 encoding = $kwargs.$$smap["encoding"];if (encoding == null) encoding = nil; 7304 7305 capacity = $kwargs.$$smap["capacity"];if (capacity == null) capacity = nil; 7306 return nil; 7307 }, -1); 7308 7309 $def(self, '$%', function $String_$percent$2(data) { 7310 var self = this; 7311 7312 if ($eqeqeq($$$('Array'), data)) { 7313 return $send(self, 'format', [self].concat($to_a(data))) 7314 } else { 7315 return self.$format(self, data) 7316 } 7317 }); 7318 7319 $def(self, '$*', function $String_$$3(count) { 7320 var self = this; 7321 7322 7323 count = $coerce_to(count, $$$('Integer'), 'to_int'); 7324 7325 if (count < 0) { 7326 $Kernel.$raise($$$('ArgumentError'), "negative argument") 7327 } 7328 7329 if (count === 0) { 7330 return ''; 7331 } 7332 7333 var result = '', 7334 string = self.toString(); 7335 7336 // All credit for the bit-twiddling magic code below goes to Mozilla 7337 // polyfill implementation of String.prototype.repeat() posted here: 7338 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat 7339 7340 if (string.length * count >= 1 << 28) { 7341 $Kernel.$raise($$$('RangeError'), "multiply count must not overflow maximum string size") 7342 } 7343 7344 for (;;) { 7345 if ((count & 1) === 1) { 7346 result += string; 7347 } 7348 count >>>= 1; 7349 if (count === 0) { 7350 break; 7351 } 7352 string += string; 7353 } 7354 7355 return result; 7356 7357 }); 7358 7359 $def(self, '$+', function $String_$plus$4(other) { 7360 var self = this; 7361 7362 7363 other = $coerce_to(other, $$$('String'), 'to_str'); 7364 7365 if (other == "" && self.$$class === Opal.String) return self; 7366 if (self == "" && other.$$class === Opal.String) return other; 7367 var out = self + other; 7368 if (self.encoding === out.encoding && other.encoding === out.encoding) return out; 7369 if (self.encoding.name === "UTF-8" || other.encoding.name === "UTF-8") return out; 7370 return Opal.enc(out, self.encoding); 7371 ; 7372 }); 7373 7374 $def(self, '$<=>', function $String_$lt_eq_gt$5(other) { 7375 var self = this; 7376 7377 if ($truthy(other['$respond_to?']("to_str"))) { 7378 7379 other = other.$to_str().$to_s(); 7380 return self > other ? 1 : (self < other ? -1 : 0);; 7381 } else { 7382 7383 var cmp = other['$<=>'](self); 7384 7385 if (cmp === nil) { 7386 return nil; 7387 } 7388 else { 7389 return cmp > 0 ? -1 : (cmp < 0 ? 1 : 0); 7390 } 7391 7392 } 7393 }); 7394 7395 $def(self, '$==', function $String_$eq_eq$6(other) { 7396 var self = this; 7397 7398 7399 if (other.$$is_string) { 7400 return self.toString() === other.toString(); 7401 } 7402 if ($respond_to(other, '$to_str')) { 7403 return other['$=='](self); 7404 } 7405 return false; 7406 7407 }); 7408 7409 $def(self, '$=~', function $String_$eq_tilde$7(other) { 7410 var self = this; 7411 7412 7413 if (other.$$is_string) { 7414 $Kernel.$raise($$$('TypeError'), "type mismatch: String given"); 7415 } 7416 7417 return other['$=~'](self); 7418 7419 }); 7420 7421 $def(self, '$[]', function $String_$$$8(index, length) { 7422 var self = this; 7423 7424 7425 ; 7426 7427 var size = self.length, exclude, range; 7428 7429 if (index.$$is_range) { 7430 exclude = index.excl; 7431 range = index; 7432 length = index.end === nil ? -1 : $coerce_to(index.end, $$$('Integer'), 'to_int'); 7433 index = index.begin === nil ? 0 : $coerce_to(index.begin, $$$('Integer'), 'to_int'); 7434 7435 if (Math.abs(index) > size) { 7436 return nil; 7437 } 7438 7439 if (index < 0) { 7440 index += size; 7441 } 7442 7443 if (length < 0) { 7444 length += size; 7445 } 7446 7447 if (!exclude || range.end === nil) { 7448 length += 1; 7449 } 7450 7451 length = length - index; 7452 7453 if (length < 0) { 7454 length = 0; 7455 } 7456 7457 return self.substr(index, length); 7458 } 7459 7460 7461 if (index.$$is_string) { 7462 if (length != null) { 7463 $Kernel.$raise($$$('TypeError')) 7464 } 7465 return self.indexOf(index) !== -1 ? index : nil; 7466 } 7467 7468 7469 if (index.$$is_regexp) { 7470 var match = self.match(index); 7471 7472 if (match === null) { 7473 ($gvars["~"] = nil) 7474 return nil; 7475 } 7476 7477 ($gvars["~"] = $$$('MatchData').$new(index, match)) 7478 7479 if (length == null) { 7480 return match[0]; 7481 } 7482 7483 length = $coerce_to(length, $$$('Integer'), 'to_int'); 7484 7485 if (length < 0 && -length < match.length) { 7486 return match[length += match.length]; 7487 } 7488 7489 if (length >= 0 && length < match.length) { 7490 return match[length]; 7491 } 7492 7493 return nil; 7494 } 7495 7496 7497 index = $coerce_to(index, $$$('Integer'), 'to_int'); 7498 7499 if (index < 0) { 7500 index += size; 7501 } 7502 7503 if (length == null) { 7504 if (index >= size || index < 0) { 7505 return nil; 7506 } 7507 return self.substr(index, 1); 7508 } 7509 7510 length = $coerce_to(length, $$$('Integer'), 'to_int'); 7511 7512 if (length < 0) { 7513 return nil; 7514 } 7515 7516 if (index > size || index < 0) { 7517 return nil; 7518 } 7519 7520 return self.substr(index, length); 7521 ; 7522 }, -2); 7523 7524 $def(self, '$b', function $$b() { 7525 var self = this; 7526 7527 return (new String(self)).$force_encoding("binary") 7528 }); 7529 7530 $def(self, '$capitalize', function $$capitalize() { 7531 var self = this; 7532 7533 return self.charAt(0).toUpperCase() + self.substr(1).toLowerCase(); 7534 }); 7535 7536 $def(self, '$casecmp', function $$casecmp(other) { 7537 var self = this; 7538 7539 7540 if (!$truthy(other['$respond_to?']("to_str"))) { 7541 return nil 7542 }; 7543 other = ($coerce_to(other, $$$('String'), 'to_str')).$to_s(); 7544 7545 var ascii_only = /^[\x00-\x7F]*$/; 7546 if (ascii_only.test(self) && ascii_only.test(other)) { 7547 self = self.toLowerCase(); 7548 other = other.toLowerCase(); 7549 } 7550 ; 7551 return self['$<=>'](other); 7552 }); 7553 7554 $def(self, '$casecmp?', function $String_casecmp$ques$9(other) { 7555 var self = this; 7556 7557 7558 var cmp = self.$casecmp(other); 7559 if (cmp === nil) { 7560 return nil; 7561 } else { 7562 return cmp === 0; 7563 } 7564 7565 }); 7566 7567 $def(self, '$center', function $$center(width, padstr) { 7568 var self = this; 7569 7570 7571 if (padstr == null) padstr = " "; 7572 width = $coerce_to(width, $$$('Integer'), 'to_int'); 7573 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 7574 if ($truthy(padstr['$empty?']())) { 7575 $Kernel.$raise($$$('ArgumentError'), "zero width padding") 7576 }; 7577 if ($truthy(width <= self.length)) { 7578 return self 7579 }; 7580 7581 var ljustified = self.$ljust($rb_divide($rb_plus(width, self.length), 2).$ceil(), padstr), 7582 rjustified = self.$rjust($rb_divide($rb_plus(width, self.length), 2).$floor(), padstr); 7583 7584 return rjustified + ljustified.slice(self.length); 7585 ; 7586 }, -2); 7587 7588 $def(self, '$chomp', function $$chomp(separator) { 7589 var self = this; 7590 if ($gvars["/"] == null) $gvars["/"] = nil; 7591 7592 7593 if (separator == null) separator = $gvars["/"]; 7594 if ($truthy(separator === nil || self.length === 0)) { 7595 return self 7596 }; 7597 separator = $Opal['$coerce_to!'](separator, $$$('String'), "to_str").$to_s(); 7598 7599 var result; 7600 7601 if (separator === "\n") { 7602 result = self.replace(/\r?\n?$/, ''); 7603 } 7604 else if (separator === "") { 7605 result = self.replace(/(\r?\n)+$/, ''); 7606 } 7607 else if (self.length >= separator.length) { 7608 var tail = self.substr(self.length - separator.length, separator.length); 7609 7610 if (tail === separator) { 7611 result = self.substr(0, self.length - separator.length); 7612 } 7613 } 7614 7615 if (result != null) { 7616 return result; 7617 } 7618 ; 7619 return self; 7620 }, -1); 7621 7622 $def(self, '$chop', function $$chop() { 7623 var self = this; 7624 7625 7626 var length = self.length, result; 7627 7628 if (length <= 1) { 7629 result = ""; 7630 } else if (self.charAt(length - 1) === "\n" && self.charAt(length - 2) === "\r") { 7631 result = self.substr(0, length - 2); 7632 } else { 7633 result = self.substr(0, length - 1); 7634 } 7635 7636 return result; 7637 7638 }); 7639 7640 $def(self, '$chr', function $$chr() { 7641 var self = this; 7642 7643 return self.charAt(0); 7644 }); 7645 7646 $def(self, '$clone', function $$clone($kwargs) { 7647 var freeze, self = this, copy = nil; 7648 7649 7650 $kwargs = $ensure_kwargs($kwargs); 7651 7652 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = nil; 7653 if (!(($truthy(freeze['$nil?']()) || ($eqeq(freeze, true))) || ($eqeq(freeze, false)))) { 7654 self.$raise($$('ArgumentError'), "unexpected value for freeze: " + (freeze.$class())) 7655 }; 7656 copy = new String(self); 7657 copy.$copy_singleton_methods(self); 7658 copy.$initialize_clone(self, $hash2(["freeze"], {"freeze": freeze})); 7659 if ($eqeq(freeze, true)) { 7660 if (!copy.$$frozen) { copy.$$frozen = true; } 7661 } else if ($truthy(freeze['$nil?']())) { 7662 if (self.$$frozen) { copy.$$frozen = true; } 7663 }; 7664 return copy; 7665 }, -1); 7666 7667 $def(self, '$dup', function $$dup() { 7668 var self = this, copy = nil; 7669 7670 7671 copy = new String(self); 7672 copy.$initialize_dup(self); 7673 return copy; 7674 }); 7675 7676 $def(self, '$count', function $$count($a) { 7677 var $post_args, sets, self = this; 7678 7679 7680 $post_args = $slice(arguments); 7681 sets = $post_args; 7682 7683 if (sets.length === 0) { 7684 $Kernel.$raise($$$('ArgumentError'), "ArgumentError: wrong number of arguments (0 for 1+)") 7685 } 7686 var char_class = char_class_from_char_sets(sets); 7687 if (char_class === null) { 7688 return 0; 7689 } 7690 return self.length - self.replace(new RegExp(char_class, 'g'), '').length; 7691 ; 7692 }, -1); 7693 7694 $def(self, '$delete', function $String_delete$10($a) { 7695 var $post_args, sets, self = this; 7696 7697 7698 $post_args = $slice(arguments); 7699 sets = $post_args; 7700 7701 if (sets.length === 0) { 7702 $Kernel.$raise($$$('ArgumentError'), "ArgumentError: wrong number of arguments (0 for 1+)") 7703 } 7704 var char_class = char_class_from_char_sets(sets); 7705 if (char_class === null) { 7706 return self; 7707 } 7708 return self.replace(new RegExp(char_class, 'g'), ''); 7709 ; 7710 }, -1); 7711 7712 $def(self, '$delete_prefix', function $$delete_prefix(prefix) { 7713 var self = this; 7714 7715 7716 if (!prefix.$$is_string) { 7717 prefix = $coerce_to(prefix, $$$('String'), 'to_str'); 7718 } 7719 7720 if (self.slice(0, prefix.length) === prefix) { 7721 return self.slice(prefix.length); 7722 } else { 7723 return self; 7724 } 7725 7726 }); 7727 7728 $def(self, '$delete_suffix', function $$delete_suffix(suffix) { 7729 var self = this; 7730 7731 7732 if (!suffix.$$is_string) { 7733 suffix = $coerce_to(suffix, $$$('String'), 'to_str'); 7734 } 7735 7736 if (self.slice(self.length - suffix.length) === suffix) { 7737 return self.slice(0, self.length - suffix.length); 7738 } else { 7739 return self; 7740 } 7741 7742 }); 7743 7744 $def(self, '$downcase', function $$downcase() { 7745 var self = this; 7746 7747 return self.toLowerCase(); 7748 }); 7749 7750 $def(self, '$each_line', function $$each_line($a, $b) { 7751 var block = $$each_line.$$p || nil, $post_args, $kwargs, separator, chomp, self = this; 7752 if ($gvars["/"] == null) $gvars["/"] = nil; 7753 7754 $$each_line.$$p = null; 7755 7756 ; 7757 $post_args = $slice(arguments); 7758 $kwargs = $extract_kwargs($post_args); 7759 $kwargs = $ensure_kwargs($kwargs); 7760 7761 if ($post_args.length > 0) separator = $post_args.shift();if (separator == null) separator = $gvars["/"]; 7762 7763 chomp = $kwargs.$$smap["chomp"];if (chomp == null) chomp = false; 7764 if (!(block !== nil)) { 7765 return self.$enum_for("each_line", separator, $hash2(["chomp"], {"chomp": chomp})) 7766 }; 7767 7768 if (separator === nil) { 7769 Opal.yield1(block, self); 7770 7771 return self; 7772 } 7773 7774 separator = $coerce_to(separator, $$$('String'), 'to_str'); 7775 7776 var a, i, n, length, chomped, trailing, splitted, value; 7777 7778 if (separator.length === 0) { 7779 for (a = self.split(/((?:\r?\n){2})(?:(?:\r?\n)*)/), i = 0, n = a.length; i < n; i += 2) { 7780 if (a[i] || a[i + 1]) { 7781 value = (a[i] || "") + (a[i + 1] || ""); 7782 if (chomp) { 7783 value = (value).$chomp("\n"); 7784 } 7785 Opal.yield1(block, value); 7786 } 7787 } 7788 7789 return self; 7790 } 7791 7792 chomped = self.$chomp(separator); 7793 trailing = self.length != chomped.length; 7794 splitted = chomped.split(separator); 7795 7796 for (i = 0, length = splitted.length; i < length; i++) { 7797 value = splitted[i]; 7798 if (i < length - 1 || trailing) { 7799 value += separator; 7800 } 7801 if (chomp) { 7802 value = (value).$chomp(separator); 7803 } 7804 Opal.yield1(block, value); 7805 } 7806 ; 7807 return self; 7808 }, -1); 7809 7810 $def(self, '$empty?', function $String_empty$ques$11() { 7811 var self = this; 7812 7813 return self.length === 0; 7814 }); 7815 7816 $def(self, '$end_with?', function $String_end_with$ques$12($a) { 7817 var $post_args, suffixes, self = this; 7818 7819 7820 $post_args = $slice(arguments); 7821 suffixes = $post_args; 7822 7823 for (var i = 0, length = suffixes.length; i < length; i++) { 7824 var suffix = $coerce_to(suffixes[i], $$$('String'), 'to_str').$to_s(); 7825 7826 if (self.length >= suffix.length && 7827 self.substr(self.length - suffix.length, suffix.length) == suffix) { 7828 return true; 7829 } 7830 } 7831 ; 7832 return false; 7833 }, -1); 7834 7835 $def(self, '$gsub', function $$gsub(pattern, replacement) { 7836 var block = $$gsub.$$p || nil, self = this; 7837 7838 $$gsub.$$p = null; 7839 7840 ; 7841 ; 7842 7843 if (replacement === undefined && block === nil) { 7844 return self.$enum_for("gsub", pattern); 7845 } 7846 7847 var result = '', match_data = nil, index = 0, match, _replacement; 7848 7849 if (pattern.$$is_regexp) { 7850 pattern = $global_multiline_regexp(pattern); 7851 } else { 7852 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 7853 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 7854 } 7855 7856 var lastIndex; 7857 while (true) { 7858 match = pattern.exec(self); 7859 7860 if (match === null) { 7861 ($gvars["~"] = nil) 7862 result += self.slice(index); 7863 break; 7864 } 7865 7866 match_data = $$$('MatchData').$new(pattern, match); 7867 7868 if (replacement === undefined) { 7869 lastIndex = pattern.lastIndex; 7870 _replacement = block(match[0]); 7871 pattern.lastIndex = lastIndex; // save and restore lastIndex 7872 } 7873 else if (replacement.$$is_hash) { 7874 _replacement = (replacement)['$[]'](match[0]).$to_s(); 7875 } 7876 else { 7877 if (!replacement.$$is_string) { 7878 replacement = $coerce_to(replacement, $$$('String'), 'to_str'); 7879 } 7880 _replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) { 7881 if (slashes.length % 2 === 0) { 7882 return original; 7883 } 7884 switch (command) { 7885 case "+": 7886 for (var i = match.length - 1; i > 0; i--) { 7887 if (match[i] !== undefined) { 7888 return slashes.slice(1) + match[i]; 7889 } 7890 } 7891 return ''; 7892 case "&": return slashes.slice(1) + match[0]; 7893 case "`": return slashes.slice(1) + self.slice(0, match.index); 7894 case "'": return slashes.slice(1) + self.slice(match.index + match[0].length); 7895 default: return slashes.slice(1) + (match[command] || ''); 7896 } 7897 }).replace(/\\\\/g, '\\'); 7898 } 7899 7900 if (pattern.lastIndex === match.index) { 7901 result += (self.slice(index, match.index) + _replacement + (self[match.index] || "")); 7902 pattern.lastIndex += 1; 7903 } 7904 else { 7905 result += (self.slice(index, match.index) + _replacement) 7906 } 7907 index = pattern.lastIndex; 7908 } 7909 7910 ($gvars["~"] = match_data) 7911 return result; 7912 ; 7913 }, -2); 7914 7915 $def(self, '$hash', function $$hash() { 7916 var self = this; 7917 7918 return self.toString(); 7919 }); 7920 7921 $def(self, '$hex', function $$hex() { 7922 var self = this; 7923 7924 return self.$to_i(16) 7925 }); 7926 7927 $def(self, '$include?', function $String_include$ques$13(other) { 7928 var self = this; 7929 7930 7931 if (!other.$$is_string) { 7932 other = $coerce_to(other, $$$('String'), 'to_str'); 7933 } 7934 return self.indexOf(other) !== -1; 7935 7936 }); 7937 7938 $def(self, '$index', function $$index(search, offset) { 7939 var self = this; 7940 7941 7942 ; 7943 7944 var index, 7945 match, 7946 regex; 7947 7948 if (offset === undefined) { 7949 offset = 0; 7950 } else { 7951 offset = $coerce_to(offset, $$$('Integer'), 'to_int'); 7952 if (offset < 0) { 7953 offset += self.length; 7954 if (offset < 0) { 7955 return nil; 7956 } 7957 } 7958 } 7959 7960 if (search.$$is_regexp) { 7961 regex = $global_multiline_regexp(search); 7962 while (true) { 7963 match = regex.exec(self); 7964 if (match === null) { 7965 ($gvars["~"] = nil); 7966 index = -1; 7967 break; 7968 } 7969 if (match.index >= offset) { 7970 ($gvars["~"] = $$$('MatchData').$new(regex, match)) 7971 index = match.index; 7972 break; 7973 } 7974 regex.lastIndex = match.index + 1; 7975 } 7976 } else { 7977 search = $coerce_to(search, $$$('String'), 'to_str'); 7978 if (search.length === 0 && offset > self.length) { 7979 index = -1; 7980 } else { 7981 index = self.indexOf(search, offset); 7982 } 7983 } 7984 7985 return index === -1 ? nil : index; 7986 ; 7987 }, -2); 7988 7989 $def(self, '$inspect', function $$inspect() { 7990 var self = this; 7991 7992 7993 /* eslint-disable no-misleading-character-class */ 7994 var escapable = /[\\\"\x00-\x1f\u007F-\u009F\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, 7995 meta = { 7996 '\u0007': '\\a', 7997 '\u001b': '\\e', 7998 '\b': '\\b', 7999 '\t': '\\t', 8000 '\n': '\\n', 8001 '\f': '\\f', 8002 '\r': '\\r', 8003 '\v': '\\v', 8004 '"' : '\\"', 8005 '\\': '\\\\' 8006 }, 8007 escaped = self.replace(escapable, function (chr) { 8008 if (meta[chr]) return meta[chr]; 8009 chr = chr.charCodeAt(0); 8010 if (chr <= 0xff && (self.encoding["$binary?"]() || self.internal_encoding["$binary?"]())) { 8011 return '\\x' + ('00' + chr.toString(16).toUpperCase()).slice(-2); 8012 } else { 8013 return '\\u' + ('0000' + chr.toString(16).toUpperCase()).slice(-4); 8014 } 8015 }); 8016 return '"' + escaped.replace(/\#[\$\@\{]/g, '\\$&') + '"'; 8017 /* eslint-enable no-misleading-character-class */ 8018 8019 }); 8020 8021 $def(self, '$intern', function $$intern() { 8022 var self = this; 8023 8024 return self.toString(); 8025 }); 8026 8027 $def(self, '$length', function $$length() { 8028 var self = this; 8029 8030 return self.length; 8031 }); 8032 $alias(self, "size", "length"); 8033 8034 $def(self, '$lines', function $$lines($a, $b) { 8035 var block = $$lines.$$p || nil, $post_args, $kwargs, separator, chomp, self = this, e = nil; 8036 if ($gvars["/"] == null) $gvars["/"] = nil; 8037 8038 $$lines.$$p = null; 8039 8040 ; 8041 $post_args = $slice(arguments); 8042 $kwargs = $extract_kwargs($post_args); 8043 $kwargs = $ensure_kwargs($kwargs); 8044 8045 if ($post_args.length > 0) separator = $post_args.shift();if (separator == null) separator = $gvars["/"]; 8046 8047 chomp = $kwargs.$$smap["chomp"];if (chomp == null) chomp = false; 8048 e = $send(self, 'each_line', [separator, $hash2(["chomp"], {"chomp": chomp})], block.$to_proc()); 8049 if ($truthy(block)) { 8050 return self 8051 } else { 8052 return e.$to_a() 8053 }; 8054 }, -1); 8055 8056 $def(self, '$ljust', function $$ljust(width, padstr) { 8057 var self = this; 8058 8059 8060 if (padstr == null) padstr = " "; 8061 width = $coerce_to(width, $$$('Integer'), 'to_int'); 8062 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 8063 if ($truthy(padstr['$empty?']())) { 8064 $Kernel.$raise($$$('ArgumentError'), "zero width padding") 8065 }; 8066 if ($truthy(width <= self.length)) { 8067 return self 8068 }; 8069 8070 var index = -1, 8071 result = ""; 8072 8073 width -= self.length; 8074 8075 while (++index < width) { 8076 result += padstr; 8077 } 8078 8079 return self + result.slice(0, width); 8080 ; 8081 }, -2); 8082 8083 $def(self, '$lstrip', function $$lstrip() { 8084 var self = this; 8085 8086 return self.replace(/^[\u0000\s]*/, ''); 8087 }); 8088 8089 $def(self, '$ascii_only?', function $String_ascii_only$ques$14() { 8090 var self = this; 8091 8092 8093 if (!self.encoding.ascii) return false; 8094 return /^[\x00-\x7F]*$/.test(self); 8095 8096 }); 8097 8098 $def(self, '$match', function $$match(pattern, pos) { 8099 var block = $$match.$$p || nil, self = this; 8100 8101 $$match.$$p = null; 8102 8103 ; 8104 ; 8105 if (($eqeqeq($$('String'), pattern) || ($truthy(pattern['$respond_to?']("to_str"))))) { 8106 pattern = $$$('Regexp').$new(pattern.$to_str()) 8107 }; 8108 if (!$eqeqeq($$$('Regexp'), pattern)) { 8109 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (pattern.$class()) + " (expected Regexp)") 8110 }; 8111 return $send(pattern, 'match', [self, pos], block.$to_proc()); 8112 }, -2); 8113 8114 $def(self, '$match?', function $String_match$ques$15(pattern, pos) { 8115 var self = this; 8116 8117 8118 ; 8119 if (($eqeqeq($$('String'), pattern) || ($truthy(pattern['$respond_to?']("to_str"))))) { 8120 pattern = $$$('Regexp').$new(pattern.$to_str()) 8121 }; 8122 if (!$eqeqeq($$$('Regexp'), pattern)) { 8123 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (pattern.$class()) + " (expected Regexp)") 8124 }; 8125 return pattern['$match?'](self, pos); 8126 }, -2); 8127 8128 $def(self, '$next', function $$next() { 8129 var self = this; 8130 8131 8132 var i = self.length; 8133 if (i === 0) { 8134 return ''; 8135 } 8136 var result = self; 8137 var first_alphanum_char_index = self.search(/[a-zA-Z0-9]/); 8138 var carry = false; 8139 var code; 8140 while (i--) { 8141 code = self.charCodeAt(i); 8142 if ((code >= 48 && code <= 57) || 8143 (code >= 65 && code <= 90) || 8144 (code >= 97 && code <= 122)) { 8145 switch (code) { 8146 case 57: 8147 carry = true; 8148 code = 48; 8149 break; 8150 case 90: 8151 carry = true; 8152 code = 65; 8153 break; 8154 case 122: 8155 carry = true; 8156 code = 97; 8157 break; 8158 default: 8159 carry = false; 8160 code += 1; 8161 } 8162 } else { 8163 if (first_alphanum_char_index === -1) { 8164 if (code === 255) { 8165 carry = true; 8166 code = 0; 8167 } else { 8168 carry = false; 8169 code += 1; 8170 } 8171 } else { 8172 carry = true; 8173 } 8174 } 8175 result = result.slice(0, i) + String.fromCharCode(code) + result.slice(i + 1); 8176 if (carry && (i === 0 || i === first_alphanum_char_index)) { 8177 switch (code) { 8178 case 65: 8179 break; 8180 case 97: 8181 break; 8182 default: 8183 code += 1; 8184 } 8185 if (i === 0) { 8186 result = String.fromCharCode(code) + result; 8187 } else { 8188 result = result.slice(0, i) + String.fromCharCode(code) + result.slice(i); 8189 } 8190 carry = false; 8191 } 8192 if (!carry) { 8193 break; 8194 } 8195 } 8196 return result; 8197 8198 }); 8199 8200 $def(self, '$oct', function $$oct() { 8201 var self = this; 8202 8203 8204 var result, 8205 string = self, 8206 radix = 8; 8207 8208 if (/^\s*_/.test(string)) { 8209 return 0; 8210 } 8211 8212 string = string.replace(/^(\s*[+-]?)(0[bodx]?)(.+)$/i, function (original, head, flag, tail) { 8213 switch (tail.charAt(0)) { 8214 case '+': 8215 case '-': 8216 return original; 8217 case '0': 8218 if (tail.charAt(1) === 'x' && flag === '0x') { 8219 return original; 8220 } 8221 } 8222 switch (flag) { 8223 case '0b': 8224 radix = 2; 8225 break; 8226 case '0': 8227 case '0o': 8228 radix = 8; 8229 break; 8230 case '0d': 8231 radix = 10; 8232 break; 8233 case '0x': 8234 radix = 16; 8235 break; 8236 } 8237 return head + tail; 8238 }); 8239 8240 result = parseInt(string.replace(/_(?!_)/g, ''), radix); 8241 return isNaN(result) ? 0 : result; 8242 8243 }); 8244 8245 $def(self, '$ord', function $$ord() { 8246 var self = this; 8247 8248 8249 if (typeof self.codePointAt === "function") { 8250 return self.codePointAt(0); 8251 } 8252 else { 8253 return self.charCodeAt(0); 8254 } 8255 8256 }); 8257 8258 $def(self, '$partition', function $$partition(sep) { 8259 var self = this; 8260 8261 8262 var i, m; 8263 8264 if (sep.$$is_regexp) { 8265 m = sep.exec(self); 8266 if (m === null) { 8267 i = -1; 8268 } else { 8269 $$$('MatchData').$new(sep, m); 8270 sep = m[0]; 8271 i = m.index; 8272 } 8273 } else { 8274 sep = $coerce_to(sep, $$$('String'), 'to_str'); 8275 i = self.indexOf(sep); 8276 } 8277 8278 if (i === -1) { 8279 return [self, '', '']; 8280 } 8281 8282 return [ 8283 self.slice(0, i), 8284 self.slice(i, i + sep.length), 8285 self.slice(i + sep.length) 8286 ]; 8287 8288 }); 8289 8290 $def(self, '$reverse', function $$reverse() { 8291 var self = this; 8292 8293 return self.split('').reverse().join(''); 8294 }); 8295 8296 $def(self, '$rindex', function $$rindex(search, offset) { 8297 var self = this; 8298 8299 8300 ; 8301 8302 var i, m, r, _m; 8303 8304 if (offset === undefined) { 8305 offset = self.length; 8306 } else { 8307 offset = $coerce_to(offset, $$$('Integer'), 'to_int'); 8308 if (offset < 0) { 8309 offset += self.length; 8310 if (offset < 0) { 8311 return nil; 8312 } 8313 } 8314 } 8315 8316 if (search.$$is_regexp) { 8317 m = null; 8318 r = $global_multiline_regexp(search); 8319 while (true) { 8320 _m = r.exec(self); 8321 if (_m === null || _m.index > offset) { 8322 break; 8323 } 8324 m = _m; 8325 r.lastIndex = m.index + 1; 8326 } 8327 if (m === null) { 8328 ($gvars["~"] = nil) 8329 i = -1; 8330 } else { 8331 $$$('MatchData').$new(r, m); 8332 i = m.index; 8333 } 8334 } else { 8335 search = $coerce_to(search, $$$('String'), 'to_str'); 8336 i = self.lastIndexOf(search, offset); 8337 } 8338 8339 return i === -1 ? nil : i; 8340 ; 8341 }, -2); 8342 8343 $def(self, '$rjust', function $$rjust(width, padstr) { 8344 var self = this; 8345 8346 8347 if (padstr == null) padstr = " "; 8348 width = $coerce_to(width, $$$('Integer'), 'to_int'); 8349 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 8350 if ($truthy(padstr['$empty?']())) { 8351 $Kernel.$raise($$$('ArgumentError'), "zero width padding") 8352 }; 8353 if ($truthy(width <= self.length)) { 8354 return self 8355 }; 8356 8357 var chars = Math.floor(width - self.length), 8358 patterns = Math.floor(chars / padstr.length), 8359 result = Array(patterns + 1).join(padstr), 8360 remaining = chars - result.length; 8361 8362 return result + padstr.slice(0, remaining) + self; 8363 ; 8364 }, -2); 8365 8366 $def(self, '$rpartition', function $$rpartition(sep) { 8367 var self = this; 8368 8369 8370 var i, m, r, _m; 8371 8372 if (sep.$$is_regexp) { 8373 m = null; 8374 r = $global_multiline_regexp(sep); 8375 8376 while (true) { 8377 _m = r.exec(self); 8378 if (_m === null) { 8379 break; 8380 } 8381 m = _m; 8382 r.lastIndex = m.index + 1; 8383 } 8384 8385 if (m === null) { 8386 i = -1; 8387 } else { 8388 $$$('MatchData').$new(r, m); 8389 sep = m[0]; 8390 i = m.index; 8391 } 8392 8393 } else { 8394 sep = $coerce_to(sep, $$$('String'), 'to_str'); 8395 i = self.lastIndexOf(sep); 8396 } 8397 8398 if (i === -1) { 8399 return ['', '', self]; 8400 } 8401 8402 return [ 8403 self.slice(0, i), 8404 self.slice(i, i + sep.length), 8405 self.slice(i + sep.length) 8406 ]; 8407 8408 }); 8409 8410 $def(self, '$rstrip', function $$rstrip() { 8411 var self = this; 8412 8413 return self.replace(/[\s\u0000]*$/, ''); 8414 }); 8415 8416 $def(self, '$scan', function $$scan(pattern, $kwargs) { 8417 var block = $$scan.$$p || nil, no_matchdata, self = this; 8418 8419 $$scan.$$p = null; 8420 8421 ; 8422 $kwargs = $ensure_kwargs($kwargs); 8423 8424 no_matchdata = $kwargs.$$smap["no_matchdata"];if (no_matchdata == null) no_matchdata = false; 8425 8426 var result = [], 8427 match_data = nil, 8428 match; 8429 8430 if (pattern.$$is_regexp) { 8431 pattern = $global_multiline_regexp(pattern); 8432 } else { 8433 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 8434 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 8435 } 8436 8437 while ((match = pattern.exec(self)) != null) { 8438 match_data = $$$('MatchData').$new(pattern, match, $hash2(["no_matchdata"], {"no_matchdata": no_matchdata})); 8439 if (block === nil) { 8440 match.length == 1 ? result.push(match[0]) : result.push((match_data).$captures()); 8441 } else { 8442 match.length == 1 ? Opal.yield1(block, match[0]) : Opal.yield1(block, (match_data).$captures()); 8443 } 8444 if (pattern.lastIndex === match.index) { 8445 pattern.lastIndex += 1; 8446 } 8447 } 8448 8449 if (!no_matchdata) ($gvars["~"] = match_data); 8450 8451 return (block !== nil ? self : result); 8452 ; 8453 }, -2); 8454 8455 $def(self, '$singleton_class', function $$singleton_class() { 8456 var self = this; 8457 8458 return Opal.get_singleton_class(self); 8459 }); 8460 8461 $def(self, '$split', function $$split(pattern, limit) { 8462 var self = this, $ret_or_1 = nil; 8463 if ($gvars[";"] == null) $gvars[";"] = nil; 8464 8465 8466 ; 8467 ; 8468 8469 if (self.length === 0) { 8470 return []; 8471 } 8472 8473 if (limit === undefined) { 8474 limit = 0; 8475 } else { 8476 limit = $Opal['$coerce_to!'](limit, $$$('Integer'), "to_int"); 8477 if (limit === 1) { 8478 return [self]; 8479 } 8480 } 8481 8482 if (pattern === undefined || pattern === nil) { 8483 pattern = ($truthy(($ret_or_1 = $gvars[";"])) ? ($ret_or_1) : (" ")); 8484 } 8485 8486 var result = [], 8487 string = self.toString(), 8488 index = 0, 8489 match, 8490 i, ii; 8491 8492 if (pattern.$$is_regexp) { 8493 pattern = $global_multiline_regexp(pattern); 8494 } else { 8495 pattern = $coerce_to(pattern, $$$('String'), 'to_str').$to_s(); 8496 if (pattern === ' ') { 8497 pattern = /\s+/gm; 8498 string = string.replace(/^\s+/, ''); 8499 } else { 8500 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 8501 } 8502 } 8503 8504 result = string.split(pattern); 8505 8506 if (result.length === 1 && result[0] === string) { 8507 return [result[0]]; 8508 } 8509 8510 while ((i = result.indexOf(undefined)) !== -1) { 8511 result.splice(i, 1); 8512 } 8513 8514 if (limit === 0) { 8515 while (result[result.length - 1] === '') { 8516 result.length -= 1; 8517 } 8518 return result; 8519 } 8520 8521 match = pattern.exec(string); 8522 8523 if (limit < 0) { 8524 if (match !== null && match[0] === '' && pattern.source.indexOf('(?=') === -1) { 8525 for (i = 0, ii = match.length; i < ii; i++) { 8526 result.push(''); 8527 } 8528 } 8529 return result; 8530 } 8531 8532 if (match !== null && match[0] === '') { 8533 result.splice(limit - 1, result.length - 1, result.slice(limit - 1).join('')); 8534 return result; 8535 } 8536 8537 if (limit >= result.length) { 8538 return result; 8539 } 8540 8541 i = 0; 8542 while (match !== null) { 8543 i++; 8544 index = pattern.lastIndex; 8545 if (i + 1 === limit) { 8546 break; 8547 } 8548 match = pattern.exec(string); 8549 } 8550 result.splice(limit - 1, result.length - 1, string.slice(index)); 8551 return result; 8552 ; 8553 }, -1); 8554 8555 $def(self, '$squeeze', function $$squeeze($a) { 8556 var $post_args, sets, self = this; 8557 8558 8559 $post_args = $slice(arguments); 8560 sets = $post_args; 8561 8562 if (sets.length === 0) { 8563 return self.replace(/(.)\1+/g, '$1'); 8564 } 8565 var char_class = char_class_from_char_sets(sets); 8566 if (char_class === null) { 8567 return self; 8568 } 8569 return self.replace(new RegExp('(' + char_class + ')\\1+', 'g'), '$1'); 8570 ; 8571 }, -1); 8572 8573 $def(self, '$start_with?', function $String_start_with$ques$16($a) { 8574 var $post_args, prefixes, self = this; 8575 8576 8577 $post_args = $slice(arguments); 8578 prefixes = $post_args; 8579 8580 for (var i = 0, length = prefixes.length; i < length; i++) { 8581 if (prefixes[i].$$is_regexp) { 8582 var regexp = prefixes[i]; 8583 var match = regexp.exec(self); 8584 8585 if (match != null && match.index === 0) { 8586 ($gvars["~"] = $$$('MatchData').$new(regexp, match)); 8587 return true; 8588 } else { 8589 ($gvars["~"] = nil) 8590 } 8591 } else { 8592 var prefix = $coerce_to(prefixes[i], $$$('String'), 'to_str').$to_s(); 8593 8594 if (self.indexOf(prefix) === 0) { 8595 return true; 8596 } 8597 } 8598 } 8599 8600 return false; 8601 ; 8602 }, -1); 8603 8604 $def(self, '$strip', function $$strip() { 8605 var self = this; 8606 8607 return self.replace(/^[\s\u0000]*|[\s\u0000]*$/g, ''); 8608 }); 8609 8610 $def(self, '$sub', function $$sub(pattern, replacement) { 8611 var block = $$sub.$$p || nil, self = this; 8612 8613 $$sub.$$p = null; 8614 8615 ; 8616 ; 8617 8618 if (!pattern.$$is_regexp) { 8619 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 8620 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); 8621 } 8622 8623 var result, match = pattern.exec(self); 8624 8625 if (match === null) { 8626 ($gvars["~"] = nil) 8627 result = self.toString(); 8628 } else { 8629 $$$('MatchData').$new(pattern, match) 8630 8631 if (replacement === undefined) { 8632 8633 if (block === nil) { 8634 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (1 for 2)") 8635 } 8636 result = self.slice(0, match.index) + block(match[0]) + self.slice(match.index + match[0].length); 8637 8638 } else if (replacement.$$is_hash) { 8639 8640 result = self.slice(0, match.index) + (replacement)['$[]'](match[0]).$to_s() + self.slice(match.index + match[0].length); 8641 8642 } else { 8643 8644 replacement = $coerce_to(replacement, $$$('String'), 'to_str'); 8645 8646 replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) { 8647 if (slashes.length % 2 === 0) { 8648 return original; 8649 } 8650 switch (command) { 8651 case "+": 8652 for (var i = match.length - 1; i > 0; i--) { 8653 if (match[i] !== undefined) { 8654 return slashes.slice(1) + match[i]; 8655 } 8656 } 8657 return ''; 8658 case "&": return slashes.slice(1) + match[0]; 8659 case "`": return slashes.slice(1) + self.slice(0, match.index); 8660 case "'": return slashes.slice(1) + self.slice(match.index + match[0].length); 8661 default: return slashes.slice(1) + (match[command] || ''); 8662 } 8663 }).replace(/\\\\/g, '\\'); 8664 8665 result = self.slice(0, match.index) + replacement + self.slice(match.index + match[0].length); 8666 } 8667 } 8668 8669 return result; 8670 ; 8671 }, -2); 8672 8673 $def(self, '$sum', function $$sum(n) { 8674 var self = this; 8675 8676 8677 if (n == null) n = 16; 8678 8679 n = $coerce_to(n, $$$('Integer'), 'to_int'); 8680 8681 var result = 0, 8682 length = self.length, 8683 i = 0; 8684 8685 for (; i < length; i++) { 8686 result += self.charCodeAt(i); 8687 } 8688 8689 if (n <= 0) { 8690 return result; 8691 } 8692 8693 return result & (Math.pow(2, n) - 1); 8694 ; 8695 }, -1); 8696 8697 $def(self, '$swapcase', function $$swapcase() { 8698 var self = this; 8699 8700 8701 var str = self.replace(/([a-z]+)|([A-Z]+)/g, function($0,$1,$2) { 8702 return $1 ? $0.toUpperCase() : $0.toLowerCase(); 8703 }); 8704 8705 return str; 8706 8707 }); 8708 8709 $def(self, '$to_f', function $$to_f() { 8710 var self = this; 8711 8712 8713 if (self.charAt(0) === '_') { 8714 return 0; 8715 } 8716 8717 var result = parseFloat(self.replace(/_/g, '')); 8718 8719 if (isNaN(result) || result == Infinity || result == -Infinity) { 8720 return 0; 8721 } 8722 else { 8723 return result; 8724 } 8725 8726 }); 8727 8728 $def(self, '$to_i', function $$to_i(base) { 8729 var self = this; 8730 8731 8732 if (base == null) base = 10; 8733 8734 var result, 8735 string = self.toLowerCase(), 8736 radix = $coerce_to(base, $$$('Integer'), 'to_int'); 8737 8738 if (radix === 1 || radix < 0 || radix > 36) { 8739 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (radix)) 8740 } 8741 8742 if (/^\s*_/.test(string)) { 8743 return 0; 8744 } 8745 8746 string = string.replace(/^(\s*[+-]?)(0[bodx]?)(.+)$/, function (original, head, flag, tail) { 8747 switch (tail.charAt(0)) { 8748 case '+': 8749 case '-': 8750 return original; 8751 case '0': 8752 if (tail.charAt(1) === 'x' && flag === '0x' && (radix === 0 || radix === 16)) { 8753 return original; 8754 } 8755 } 8756 switch (flag) { 8757 case '0b': 8758 if (radix === 0 || radix === 2) { 8759 radix = 2; 8760 return head + tail; 8761 } 8762 break; 8763 case '0': 8764 case '0o': 8765 if (radix === 0 || radix === 8) { 8766 radix = 8; 8767 return head + tail; 8768 } 8769 break; 8770 case '0d': 8771 if (radix === 0 || radix === 10) { 8772 radix = 10; 8773 return head + tail; 8774 } 8775 break; 8776 case '0x': 8777 if (radix === 0 || radix === 16) { 8778 radix = 16; 8779 return head + tail; 8780 } 8781 break; 8782 } 8783 return original 8784 }); 8785 8786 result = parseInt(string.replace(/_(?!_)/g, ''), radix); 8787 return isNaN(result) ? 0 : result; 8788 ; 8789 }, -1); 8790 8791 $def(self, '$to_proc', function $$to_proc() { 8792 var $yield = $$to_proc.$$p || nil, self = this, method_name = nil, jsid = nil, proc = nil; 8793 8794 $$to_proc.$$p = null; 8795 8796 method_name = self.valueOf(); 8797 jsid = Opal.jsid(method_name); 8798 proc = $send($Kernel, 'proc', [], function $$17($a){var block = $$17.$$p || nil, $post_args, args; 8799 8800 $$17.$$p = null; 8801 8802 ; 8803 $post_args = $slice(arguments); 8804 args = $post_args; 8805 8806 if (args.length === 0) { 8807 $Kernel.$raise($$$('ArgumentError'), "no receiver given") 8808 } 8809 8810 var recv = args[0]; 8811 8812 if (recv == null) recv = nil; 8813 8814 var body = recv[jsid]; 8815 8816 if (!body) { 8817 body = recv.$method_missing; 8818 args[0] = method_name; 8819 } else { 8820 args = args.slice(1); 8821 } 8822 8823 if (typeof block === 'function') { 8824 body.$$p = block; 8825 } 8826 8827 if (args.length === 0) { 8828 return body.call(recv); 8829 } else { 8830 return body.apply(recv, args); 8831 } 8832 ;}, -1); 8833 proc.$$source_location = nil; 8834 return proc; 8835 }); 8836 8837 $def(self, '$to_s', function $$to_s() { 8838 var self = this; 8839 8840 return self.toString(); 8841 }); 8842 8843 $def(self, '$tr', function $$tr(from, to) { 8844 var self = this; 8845 8846 8847 from = $coerce_to(from, $$$('String'), 'to_str').$to_s(); 8848 to = $coerce_to(to, $$$('String'), 'to_str').$to_s(); 8849 8850 if (from.length == 0 || from === to) { 8851 return self; 8852 } 8853 8854 var i, in_range, c, ch, start, end, length; 8855 var subs = {}; 8856 var from_chars = from.split(''); 8857 var from_length = from_chars.length; 8858 var to_chars = to.split(''); 8859 var to_length = to_chars.length; 8860 8861 var inverse = false; 8862 var global_sub = null; 8863 if (from_chars[0] === '^' && from_chars.length > 1) { 8864 inverse = true; 8865 from_chars.shift(); 8866 global_sub = to_chars[to_length - 1] 8867 from_length -= 1; 8868 } 8869 8870 var from_chars_expanded = []; 8871 var last_from = null; 8872 in_range = false; 8873 for (i = 0; i < from_length; i++) { 8874 ch = from_chars[i]; 8875 if (last_from == null) { 8876 last_from = ch; 8877 from_chars_expanded.push(ch); 8878 } 8879 else if (ch === '-') { 8880 if (last_from === '-') { 8881 from_chars_expanded.push('-'); 8882 from_chars_expanded.push('-'); 8883 } 8884 else if (i == from_length - 1) { 8885 from_chars_expanded.push('-'); 8886 } 8887 else { 8888 in_range = true; 8889 } 8890 } 8891 else if (in_range) { 8892 start = last_from.charCodeAt(0); 8893 end = ch.charCodeAt(0); 8894 if (start > end) { 8895 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration") 8896 } 8897 for (c = start + 1; c < end; c++) { 8898 from_chars_expanded.push(String.fromCharCode(c)); 8899 } 8900 from_chars_expanded.push(ch); 8901 in_range = null; 8902 last_from = null; 8903 } 8904 else { 8905 from_chars_expanded.push(ch); 8906 } 8907 } 8908 8909 from_chars = from_chars_expanded; 8910 from_length = from_chars.length; 8911 8912 if (inverse) { 8913 for (i = 0; i < from_length; i++) { 8914 subs[from_chars[i]] = true; 8915 } 8916 } 8917 else { 8918 if (to_length > 0) { 8919 var to_chars_expanded = []; 8920 var last_to = null; 8921 in_range = false; 8922 for (i = 0; i < to_length; i++) { 8923 ch = to_chars[i]; 8924 if (last_to == null) { 8925 last_to = ch; 8926 to_chars_expanded.push(ch); 8927 } 8928 else if (ch === '-') { 8929 if (last_to === '-') { 8930 to_chars_expanded.push('-'); 8931 to_chars_expanded.push('-'); 8932 } 8933 else if (i == to_length - 1) { 8934 to_chars_expanded.push('-'); 8935 } 8936 else { 8937 in_range = true; 8938 } 8939 } 8940 else if (in_range) { 8941 start = last_to.charCodeAt(0); 8942 end = ch.charCodeAt(0); 8943 if (start > end) { 8944 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration") 8945 } 8946 for (c = start + 1; c < end; c++) { 8947 to_chars_expanded.push(String.fromCharCode(c)); 8948 } 8949 to_chars_expanded.push(ch); 8950 in_range = null; 8951 last_to = null; 8952 } 8953 else { 8954 to_chars_expanded.push(ch); 8955 } 8956 } 8957 8958 to_chars = to_chars_expanded; 8959 to_length = to_chars.length; 8960 } 8961 8962 var length_diff = from_length - to_length; 8963 if (length_diff > 0) { 8964 var pad_char = (to_length > 0 ? to_chars[to_length - 1] : ''); 8965 for (i = 0; i < length_diff; i++) { 8966 to_chars.push(pad_char); 8967 } 8968 } 8969 8970 for (i = 0; i < from_length; i++) { 8971 subs[from_chars[i]] = to_chars[i]; 8972 } 8973 } 8974 8975 var new_str = '' 8976 for (i = 0, length = self.length; i < length; i++) { 8977 ch = self.charAt(i); 8978 var sub = subs[ch]; 8979 if (inverse) { 8980 new_str += (sub == null ? global_sub : ch); 8981 } 8982 else { 8983 new_str += (sub != null ? sub : ch); 8984 } 8985 } 8986 return new_str; 8987 8988 }); 8989 8990 $def(self, '$tr_s', function $$tr_s(from, to) { 8991 var self = this; 8992 8993 8994 from = $coerce_to(from, $$$('String'), 'to_str').$to_s(); 8995 to = $coerce_to(to, $$$('String'), 'to_str').$to_s(); 8996 8997 if (from.length == 0) { 8998 return self; 8999 } 9000 9001 var i, in_range, c, ch, start, end, length; 9002 var subs = {}; 9003 var from_chars = from.split(''); 9004 var from_length = from_chars.length; 9005 var to_chars = to.split(''); 9006 var to_length = to_chars.length; 9007 9008 var inverse = false; 9009 var global_sub = null; 9010 if (from_chars[0] === '^' && from_chars.length > 1) { 9011 inverse = true; 9012 from_chars.shift(); 9013 global_sub = to_chars[to_length - 1] 9014 from_length -= 1; 9015 } 9016 9017 var from_chars_expanded = []; 9018 var last_from = null; 9019 in_range = false; 9020 for (i = 0; i < from_length; i++) { 9021 ch = from_chars[i]; 9022 if (last_from == null) { 9023 last_from = ch; 9024 from_chars_expanded.push(ch); 9025 } 9026 else if (ch === '-') { 9027 if (last_from === '-') { 9028 from_chars_expanded.push('-'); 9029 from_chars_expanded.push('-'); 9030 } 9031 else if (i == from_length - 1) { 9032 from_chars_expanded.push('-'); 9033 } 9034 else { 9035 in_range = true; 9036 } 9037 } 9038 else if (in_range) { 9039 start = last_from.charCodeAt(0); 9040 end = ch.charCodeAt(0); 9041 if (start > end) { 9042 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration") 9043 } 9044 for (c = start + 1; c < end; c++) { 9045 from_chars_expanded.push(String.fromCharCode(c)); 9046 } 9047 from_chars_expanded.push(ch); 9048 in_range = null; 9049 last_from = null; 9050 } 9051 else { 9052 from_chars_expanded.push(ch); 9053 } 9054 } 9055 9056 from_chars = from_chars_expanded; 9057 from_length = from_chars.length; 9058 9059 if (inverse) { 9060 for (i = 0; i < from_length; i++) { 9061 subs[from_chars[i]] = true; 9062 } 9063 } 9064 else { 9065 if (to_length > 0) { 9066 var to_chars_expanded = []; 9067 var last_to = null; 9068 in_range = false; 9069 for (i = 0; i < to_length; i++) { 9070 ch = to_chars[i]; 9071 if (last_from == null) { 9072 last_from = ch; 9073 to_chars_expanded.push(ch); 9074 } 9075 else if (ch === '-') { 9076 if (last_to === '-') { 9077 to_chars_expanded.push('-'); 9078 to_chars_expanded.push('-'); 9079 } 9080 else if (i == to_length - 1) { 9081 to_chars_expanded.push('-'); 9082 } 9083 else { 9084 in_range = true; 9085 } 9086 } 9087 else if (in_range) { 9088 start = last_from.charCodeAt(0); 9089 end = ch.charCodeAt(0); 9090 if (start > end) { 9091 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration") 9092 } 9093 for (c = start + 1; c < end; c++) { 9094 to_chars_expanded.push(String.fromCharCode(c)); 9095 } 9096 to_chars_expanded.push(ch); 9097 in_range = null; 9098 last_from = null; 9099 } 9100 else { 9101 to_chars_expanded.push(ch); 9102 } 9103 } 9104 9105 to_chars = to_chars_expanded; 9106 to_length = to_chars.length; 9107 } 9108 9109 var length_diff = from_length - to_length; 9110 if (length_diff > 0) { 9111 var pad_char = (to_length > 0 ? to_chars[to_length - 1] : ''); 9112 for (i = 0; i < length_diff; i++) { 9113 to_chars.push(pad_char); 9114 } 9115 } 9116 9117 for (i = 0; i < from_length; i++) { 9118 subs[from_chars[i]] = to_chars[i]; 9119 } 9120 } 9121 var new_str = '' 9122 var last_substitute = null 9123 for (i = 0, length = self.length; i < length; i++) { 9124 ch = self.charAt(i); 9125 var sub = subs[ch] 9126 if (inverse) { 9127 if (sub == null) { 9128 if (last_substitute == null) { 9129 new_str += global_sub; 9130 last_substitute = true; 9131 } 9132 } 9133 else { 9134 new_str += ch; 9135 last_substitute = null; 9136 } 9137 } 9138 else { 9139 if (sub != null) { 9140 if (last_substitute == null || last_substitute !== sub) { 9141 new_str += sub; 9142 last_substitute = sub; 9143 } 9144 } 9145 else { 9146 new_str += ch; 9147 last_substitute = null; 9148 } 9149 } 9150 } 9151 return new_str; 9152 9153 }); 9154 9155 $def(self, '$upcase', function $$upcase() { 9156 var self = this; 9157 9158 return self.toUpperCase(); 9159 }); 9160 9161 $def(self, '$upto', function $$upto(stop, excl) { 9162 var block = $$upto.$$p || nil, self = this; 9163 9164 $$upto.$$p = null; 9165 9166 ; 9167 if (excl == null) excl = false; 9168 if (!(block !== nil)) { 9169 return self.$enum_for("upto", stop, excl) 9170 }; 9171 9172 var a, b, s = self.toString(); 9173 9174 stop = $coerce_to(stop, $$$('String'), 'to_str'); 9175 9176 if (s.length === 1 && stop.length === 1) { 9177 9178 a = s.charCodeAt(0); 9179 b = stop.charCodeAt(0); 9180 9181 while (a <= b) { 9182 if (excl && a === b) { 9183 break; 9184 } 9185 9186 block(String.fromCharCode(a)); 9187 9188 a += 1; 9189 } 9190 9191 } else if (parseInt(s, 10).toString() === s && parseInt(stop, 10).toString() === stop) { 9192 9193 a = parseInt(s, 10); 9194 b = parseInt(stop, 10); 9195 9196 while (a <= b) { 9197 if (excl && a === b) { 9198 break; 9199 } 9200 9201 block(a.toString()); 9202 9203 a += 1; 9204 } 9205 9206 } else { 9207 9208 while (s.length <= stop.length && s <= stop) { 9209 if (excl && s === stop) { 9210 break; 9211 } 9212 9213 block(s); 9214 9215 s = (s).$succ(); 9216 } 9217 9218 } 9219 return self; 9220 ; 9221 }, -2); 9222 9223 function char_class_from_char_sets(sets) { 9224 function explode_sequences_in_character_set(set) { 9225 var result = '', 9226 i, len = set.length, 9227 curr_char, 9228 skip_next_dash, 9229 char_code_from, 9230 char_code_upto, 9231 char_code; 9232 for (i = 0; i < len; i++) { 9233 curr_char = set.charAt(i); 9234 if (curr_char === '-' && i > 0 && i < (len - 1) && !skip_next_dash) { 9235 char_code_from = set.charCodeAt(i - 1); 9236 char_code_upto = set.charCodeAt(i + 1); 9237 if (char_code_from > char_code_upto) { 9238 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (char_code_from) + "-" + (char_code_upto) + "\" in string transliteration") 9239 } 9240 for (char_code = char_code_from + 1; char_code < char_code_upto + 1; char_code++) { 9241 result += String.fromCharCode(char_code); 9242 } 9243 skip_next_dash = true; 9244 i++; 9245 } else { 9246 skip_next_dash = (curr_char === '\\'); 9247 result += curr_char; 9248 } 9249 } 9250 return result; 9251 } 9252 9253 function intersection(setA, setB) { 9254 if (setA.length === 0) { 9255 return setB; 9256 } 9257 var result = '', 9258 i, len = setA.length, 9259 chr; 9260 for (i = 0; i < len; i++) { 9261 chr = setA.charAt(i); 9262 if (setB.indexOf(chr) !== -1) { 9263 result += chr; 9264 } 9265 } 9266 return result; 9267 } 9268 9269 var i, len, set, neg, chr, tmp, 9270 pos_intersection = '', 9271 neg_intersection = ''; 9272 9273 for (i = 0, len = sets.length; i < len; i++) { 9274 set = $coerce_to(sets[i], $$$('String'), 'to_str'); 9275 neg = (set.charAt(0) === '^' && set.length > 1); 9276 set = explode_sequences_in_character_set(neg ? set.slice(1) : set); 9277 if (neg) { 9278 neg_intersection = intersection(neg_intersection, set); 9279 } else { 9280 pos_intersection = intersection(pos_intersection, set); 9281 } 9282 } 9283 9284 if (pos_intersection.length > 0 && neg_intersection.length > 0) { 9285 tmp = ''; 9286 for (i = 0, len = pos_intersection.length; i < len; i++) { 9287 chr = pos_intersection.charAt(i); 9288 if (neg_intersection.indexOf(chr) === -1) { 9289 tmp += chr; 9290 } 9291 } 9292 pos_intersection = tmp; 9293 neg_intersection = ''; 9294 } 9295 9296 if (pos_intersection.length > 0) { 9297 return '[' + $$$('Regexp').$escape(pos_intersection) + ']'; 9298 } 9299 9300 if (neg_intersection.length > 0) { 9301 return '[^' + $$$('Regexp').$escape(neg_intersection) + ']'; 9302 } 9303 9304 return null; 9305 } 9306 ; 9307 9308 $def(self, '$instance_variables', function $$instance_variables() { 9309 9310 return [] 9311 }); 9312 $defs(self, '$_load', function $$_load($a) { 9313 var $post_args, args, self = this; 9314 9315 9316 $post_args = $slice(arguments); 9317 args = $post_args; 9318 return $send(self, 'new', $to_a(args)); 9319 }, -1); 9320 9321 $def(self, '$unicode_normalize', function $$unicode_normalize(form) { 9322 var self = this; 9323 9324 9325 if (form == null) form = "nfc"; 9326 if (!$truthy(["nfc", "nfd", "nfkc", "nfkd"]['$include?'](form))) { 9327 $Kernel.$raise($$$('ArgumentError'), "Invalid normalization form " + (form)) 9328 }; 9329 return self.normalize(form.$upcase()); 9330 }, -1); 9331 9332 $def(self, '$unicode_normalized?', function $String_unicode_normalized$ques$18(form) { 9333 var self = this; 9334 9335 9336 if (form == null) form = "nfc"; 9337 return self.$unicode_normalize(form)['$=='](self); 9338 }, -1); 9339 9340 $def(self, '$unpack', function $$unpack(format) { 9341 9342 return $Kernel.$raise("To use String#unpack, you must first require 'corelib/string/unpack'.") 9343 }); 9344 9345 $def(self, '$unpack1', function $$unpack1(format) { 9346 9347 return $Kernel.$raise("To use String#unpack1, you must first require 'corelib/string/unpack'.") 9348 }); 9349 9350 $def(self, '$freeze', function $$freeze() { 9351 var self = this; 9352 9353 9354 if (typeof self === 'string') { return self; } 9355 $prop(self, "$$frozen", true); 9356 return self; 9357 9358 }); 9359 9360 $def(self, '$-@', function $String_$minus$$19() { 9361 var self = this; 9362 9363 9364 if (typeof self === 'string') return self; 9365 if (self.$$frozen) return self; 9366 if (self.encoding.name == 'UTF-8' && self.internal_encoding.name == 'UTF-8') return self.toString(); 9367 return self.$dup().$freeze(); 9368 9369 }); 9370 9371 $def(self, '$frozen?', function $String_frozen$ques$20() { 9372 var self = this; 9373 9374 return typeof self === 'string' || self.$$frozen === true; 9375 }); 9376 $alias(self, "+@", "dup"); 9377 $alias(self, "===", "=="); 9378 $alias(self, "byteslice", "[]"); 9379 $alias(self, "eql?", "=="); 9380 $alias(self, "equal?", "==="); 9381 $alias(self, "object_id", "__id__"); 9382 $alias(self, "slice", "[]"); 9383 $alias(self, "succ", "next"); 9384 $alias(self, "to_str", "to_s"); 9385 $alias(self, "to_sym", "intern"); 9386 return $Opal.$pristine(self, "initialize"); 9387 })('::', String, $nesting); 9388 return $const_set($nesting[0], 'Symbol', $$('String')); 9389}; 9390 9391Opal.modules["corelib/enumerable"] = function(Opal) {/* Generated by Opal 1.7.3 */ 9392 var $truthy = Opal.truthy, $coerce_to = Opal.coerce_to, $yield1 = Opal.yield1, $yieldX = Opal.yieldX, $deny_frozen_access = Opal.deny_frozen_access, $module = Opal.module, $send = Opal.send, $slice = Opal.slice, $to_a = Opal.to_a, $Opal = Opal.Opal, $thrower = Opal.thrower, $def = Opal.def, $Kernel = Opal.Kernel, $return_val = Opal.return_val, $rb_gt = Opal.rb_gt, $rb_times = Opal.rb_times, $rb_lt = Opal.rb_lt, $eqeq = Opal.eqeq, $rb_plus = Opal.rb_plus, $rb_minus = Opal.rb_minus, $rb_divide = Opal.rb_divide, $rb_le = Opal.rb_le, $hash2 = Opal.hash2, $lambda = Opal.lambda, $not = Opal.not, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 9393 9394 Opal.add_stubs('each,public_send,destructure,to_enum,enumerator_size,new,yield,raise,slice_when,!,enum_for,flatten,map,to_proc,compact,to_a,warn,proc,==,nil?,respond_to?,coerce_to!,>,*,try_convert,<,+,-,ceil,/,size,select,__send__,length,<=,[],push,<<,[]=,===,inspect,<=>,first,reverse,sort,take,sort_by,compare,call,dup,sort!,map!,include?,-@,key?,values,transform_values,group_by,fetch,to_h,coerce_to?,class,zip,detect,find_all,collect_concat,collect,inject,entries'); 9395 return (function($base, $parent_nesting) { 9396 var self = $module($base, 'Enumerable'); 9397 9398 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 9399 9400 9401 9402 function comparableForPattern(value) { 9403 if (value.length === 0) { 9404 value = [nil]; 9405 } 9406 9407 if (value.length > 1) { 9408 value = [value]; 9409 } 9410 9411 return value; 9412 } 9413 ; 9414 9415 $def(self, '$all?', function $Enumerable_all$ques$1(pattern) {try { var $t_return = $thrower('return'); 9416 var block = $Enumerable_all$ques$1.$$p || nil, self = this; 9417 9418 $Enumerable_all$ques$1.$$p = null; 9419 9420 ; 9421 ; 9422 if ($truthy(pattern !== undefined)) { 9423 $send(self, 'each', [], function $$2($a){var $post_args, value, comparable = nil; 9424 9425 9426 $post_args = $slice(arguments); 9427 value = $post_args; 9428 comparable = comparableForPattern(value); 9429 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 9430 return nil 9431 } else { 9432 $t_return.$throw(false) 9433 };}, {$$arity: -1, $$ret: $t_return}) 9434 } else if ((block !== nil)) { 9435 $send(self, 'each', [], function $$3($a){var $post_args, value; 9436 9437 9438 $post_args = $slice(arguments); 9439 value = $post_args; 9440 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 9441 return nil 9442 } else { 9443 $t_return.$throw(false) 9444 };}, {$$arity: -1, $$ret: $t_return}) 9445 } else { 9446 $send(self, 'each', [], function $$4($a){var $post_args, value; 9447 9448 9449 $post_args = $slice(arguments); 9450 value = $post_args; 9451 if ($truthy($Opal.$destructure(value))) { 9452 return nil 9453 } else { 9454 $t_return.$throw(false) 9455 };}, {$$arity: -1, $$ret: $t_return}) 9456 }; 9457 return true;} catch($e) { 9458 if ($e === $t_return) return $e.$v; 9459 throw $e; 9460 } 9461 }, -1); 9462 9463 $def(self, '$any?', function $Enumerable_any$ques$5(pattern) {try { var $t_return = $thrower('return'); 9464 var block = $Enumerable_any$ques$5.$$p || nil, self = this; 9465 9466 $Enumerable_any$ques$5.$$p = null; 9467 9468 ; 9469 ; 9470 if ($truthy(pattern !== undefined)) { 9471 $send(self, 'each', [], function $$6($a){var $post_args, value, comparable = nil; 9472 9473 9474 $post_args = $slice(arguments); 9475 value = $post_args; 9476 comparable = comparableForPattern(value); 9477 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 9478 $t_return.$throw(true) 9479 } else { 9480 return nil 9481 };}, {$$arity: -1, $$ret: $t_return}) 9482 } else if ((block !== nil)) { 9483 $send(self, 'each', [], function $$7($a){var $post_args, value; 9484 9485 9486 $post_args = $slice(arguments); 9487 value = $post_args; 9488 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 9489 $t_return.$throw(true) 9490 } else { 9491 return nil 9492 };}, {$$arity: -1, $$ret: $t_return}) 9493 } else { 9494 $send(self, 'each', [], function $$8($a){var $post_args, value; 9495 9496 9497 $post_args = $slice(arguments); 9498 value = $post_args; 9499 if ($truthy($Opal.$destructure(value))) { 9500 $t_return.$throw(true) 9501 } else { 9502 return nil 9503 };}, {$$arity: -1, $$ret: $t_return}) 9504 }; 9505 return false;} catch($e) { 9506 if ($e === $t_return) return $e.$v; 9507 throw $e; 9508 } 9509 }, -1); 9510 9511 $def(self, '$chunk', function $$chunk() { 9512 var block = $$chunk.$$p || nil, self = this; 9513 9514 $$chunk.$$p = null; 9515 9516 ; 9517 if (!(block !== nil)) { 9518 return $send(self, 'to_enum', ["chunk"], function $$9(){var self = $$9.$$s == null ? this : $$9.$$s; 9519 9520 return self.$enumerator_size()}, {$$s: self}) 9521 }; 9522 return $send($$$('Enumerator'), 'new', [], function $$10(yielder){var self = $$10.$$s == null ? this : $$10.$$s; 9523 9524 9525 if (yielder == null) yielder = nil; 9526 9527 var previous = nil, accumulate = []; 9528 9529 function releaseAccumulate() { 9530 if (accumulate.length > 0) { 9531 yielder.$yield(previous, accumulate) 9532 } 9533 } 9534 9535 self.$each.$$p = function(value) { 9536 var key = $yield1(block, value); 9537 9538 if (key === nil) { 9539 releaseAccumulate(); 9540 accumulate = []; 9541 previous = nil; 9542 } else { 9543 if (previous === nil || previous === key) { 9544 accumulate.push(value); 9545 } else { 9546 releaseAccumulate(); 9547 accumulate = [value]; 9548 } 9549 9550 previous = key; 9551 } 9552 } 9553 9554 self.$each(); 9555 9556 releaseAccumulate(); 9557 ;}, {$$s: self}); 9558 }); 9559 9560 $def(self, '$chunk_while', function $$chunk_while() { 9561 var block = $$chunk_while.$$p || nil, self = this; 9562 9563 $$chunk_while.$$p = null; 9564 9565 ; 9566 if (!(block !== nil)) { 9567 $Kernel.$raise($$$('ArgumentError'), "no block given") 9568 }; 9569 return $send(self, 'slice_when', [], function $$11(before, after){ 9570 9571 if (before == null) before = nil; 9572 if (after == null) after = nil; 9573 return Opal.yieldX(block, [before, after])['$!']();}); 9574 }); 9575 9576 $def(self, '$collect', function $$collect() { 9577 var block = $$collect.$$p || nil, self = this; 9578 9579 $$collect.$$p = null; 9580 9581 ; 9582 if (!(block !== nil)) { 9583 return $send(self, 'enum_for', ["collect"], function $$12(){var self = $$12.$$s == null ? this : $$12.$$s; 9584 9585 return self.$enumerator_size()}, {$$s: self}) 9586 }; 9587 9588 var result = []; 9589 9590 self.$each.$$p = function() { 9591 var value = $yieldX(block, arguments); 9592 9593 result.push(value); 9594 }; 9595 9596 self.$each(); 9597 9598 return result; 9599 ; 9600 }); 9601 9602 $def(self, '$collect_concat', function $$collect_concat() { 9603 var block = $$collect_concat.$$p || nil, self = this; 9604 9605 $$collect_concat.$$p = null; 9606 9607 ; 9608 if (!(block !== nil)) { 9609 return $send(self, 'enum_for', ["collect_concat"], function $$13(){var self = $$13.$$s == null ? this : $$13.$$s; 9610 9611 return self.$enumerator_size()}, {$$s: self}) 9612 }; 9613 return $send(self, 'map', [], block.$to_proc()).$flatten(1); 9614 }); 9615 9616 $def(self, '$compact', function $$compact() { 9617 var self = this; 9618 9619 return self.$to_a().$compact() 9620 }); 9621 9622 $def(self, '$count', function $$count(object) { 9623 var block = $$count.$$p || nil, self = this, result = nil; 9624 9625 $$count.$$p = null; 9626 9627 ; 9628 ; 9629 result = 0; 9630 9631 if (object != null && block !== nil) { 9632 self.$warn("warning: given block not used") 9633 } 9634 ; 9635 if ($truthy(object != null)) { 9636 block = $send($Kernel, 'proc', [], function $$14($a){var $post_args, args; 9637 9638 9639 $post_args = $slice(arguments); 9640 args = $post_args; 9641 return $Opal.$destructure(args)['$=='](object);}, -1) 9642 } else if ($truthy(block['$nil?']())) { 9643 block = $send($Kernel, 'proc', [], $return_val(true)) 9644 }; 9645 $send(self, 'each', [], function $$15($a){var $post_args, args; 9646 9647 9648 $post_args = $slice(arguments); 9649 args = $post_args; 9650 if ($truthy($yieldX(block, args))) { 9651 return result++; 9652 } else { 9653 return nil 9654 };}, -1); 9655 return result; 9656 }, -1); 9657 9658 $def(self, '$cycle', function $$cycle(n) { 9659 var block = $$cycle.$$p || nil, self = this; 9660 9661 $$cycle.$$p = null; 9662 9663 ; 9664 if (n == null) n = nil; 9665 if (!(block !== nil)) { 9666 return $send(self, 'enum_for', ["cycle", n], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 9667 9668 if ($truthy(n['$nil?']())) { 9669 if ($truthy(self['$respond_to?']("size"))) { 9670 return $$$($$$('Float'), 'INFINITY') 9671 } else { 9672 return nil 9673 } 9674 } else { 9675 9676 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 9677 if ($truthy($rb_gt(n, 0))) { 9678 return $rb_times(self.$enumerator_size(), n) 9679 } else { 9680 return 0 9681 }; 9682 }}, {$$s: self}) 9683 }; 9684 if (!$truthy(n['$nil?']())) { 9685 9686 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 9687 if ($truthy(n <= 0)) { 9688 return nil 9689 }; 9690 }; 9691 9692 var all = [], i, length, value; 9693 9694 self.$each.$$p = function() { 9695 var param = $Opal.$destructure(arguments), 9696 value = $yield1(block, param); 9697 9698 all.push(param); 9699 } 9700 9701 self.$each(); 9702 9703 if (all.length === 0) { 9704 return nil; 9705 } 9706 9707 if (n === nil) { 9708 while (true) { 9709 for (i = 0, length = all.length; i < length; i++) { 9710 value = $yield1(block, all[i]); 9711 } 9712 } 9713 } 9714 else { 9715 while (n > 1) { 9716 for (i = 0, length = all.length; i < length; i++) { 9717 value = $yield1(block, all[i]); 9718 } 9719 9720 n--; 9721 } 9722 } 9723 ; 9724 }, -1); 9725 9726 $def(self, '$detect', function $$detect(ifnone) {try { var $t_return = $thrower('return'); 9727 var block = $$detect.$$p || nil, self = this; 9728 9729 $$detect.$$p = null; 9730 9731 ; 9732 ; 9733 if (!(block !== nil)) { 9734 return self.$enum_for("detect", ifnone) 9735 }; 9736 $send(self, 'each', [], function $$17($a){var $post_args, args, value = nil; 9737 9738 9739 $post_args = $slice(arguments); 9740 args = $post_args; 9741 value = $Opal.$destructure(args); 9742 if ($truthy(Opal.yield1(block, value))) { 9743 $t_return.$throw(value) 9744 } else { 9745 return nil 9746 };}, {$$arity: -1, $$ret: $t_return}); 9747 9748 if (ifnone !== undefined) { 9749 if (typeof(ifnone) === 'function') { 9750 return ifnone(); 9751 } else { 9752 return ifnone; 9753 } 9754 } 9755 ; 9756 return nil;} catch($e) { 9757 if ($e === $t_return) return $e.$v; 9758 throw $e; 9759 } 9760 }, -1); 9761 9762 $def(self, '$drop', function $$drop(number) { 9763 var self = this; 9764 9765 9766 number = $coerce_to(number, $$$('Integer'), 'to_int'); 9767 if ($truthy(number < 0)) { 9768 $Kernel.$raise($$$('ArgumentError'), "attempt to drop negative size") 9769 }; 9770 9771 var result = [], 9772 current = 0; 9773 9774 self.$each.$$p = function() { 9775 if (number <= current) { 9776 result.push($Opal.$destructure(arguments)); 9777 } 9778 9779 current++; 9780 }; 9781 9782 self.$each() 9783 9784 return result; 9785 ; 9786 }); 9787 9788 $def(self, '$drop_while', function $$drop_while() { 9789 var block = $$drop_while.$$p || nil, self = this; 9790 9791 $$drop_while.$$p = null; 9792 9793 ; 9794 if (!(block !== nil)) { 9795 return self.$enum_for("drop_while") 9796 }; 9797 9798 var result = [], 9799 dropping = true; 9800 9801 self.$each.$$p = function() { 9802 var param = $Opal.$destructure(arguments); 9803 9804 if (dropping) { 9805 var value = $yield1(block, param); 9806 9807 if (!$truthy(value)) { 9808 dropping = false; 9809 result.push(param); 9810 } 9811 } 9812 else { 9813 result.push(param); 9814 } 9815 }; 9816 9817 self.$each(); 9818 9819 return result; 9820 ; 9821 }); 9822 9823 $def(self, '$each_cons', function $$each_cons(n) { 9824 var block = $$each_cons.$$p || nil, self = this; 9825 9826 $$each_cons.$$p = null; 9827 9828 ; 9829 if ($truthy(arguments.length != 1)) { 9830 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 1)") 9831 }; 9832 n = $Opal.$try_convert(n, $$$('Integer'), "to_int"); 9833 if ($truthy(n <= 0)) { 9834 $Kernel.$raise($$$('ArgumentError'), "invalid size") 9835 }; 9836 if (!(block !== nil)) { 9837 return $send(self, 'enum_for', ["each_cons", n], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s, enum_size = nil; 9838 9839 9840 enum_size = self.$enumerator_size(); 9841 if ($truthy(enum_size['$nil?']())) { 9842 return nil 9843 } else if (($eqeq(enum_size, 0) || ($truthy($rb_lt(enum_size, n))))) { 9844 return 0 9845 } else { 9846 return $rb_plus($rb_minus(enum_size, n), 1) 9847 };}, {$$s: self}) 9848 }; 9849 9850 var buffer = []; 9851 9852 self.$each.$$p = function() { 9853 var element = $Opal.$destructure(arguments); 9854 buffer.push(element); 9855 if (buffer.length > n) { 9856 buffer.shift(); 9857 } 9858 if (buffer.length == n) { 9859 $yield1(block, buffer.slice(0, n)); 9860 } 9861 } 9862 9863 self.$each(); 9864 9865 return self; 9866 ; 9867 }); 9868 9869 $def(self, '$each_entry', function $$each_entry($a) { 9870 var block = $$each_entry.$$p || nil, $post_args, data, self = this; 9871 9872 $$each_entry.$$p = null; 9873 9874 ; 9875 $post_args = $slice(arguments); 9876 data = $post_args; 9877 if (!(block !== nil)) { 9878 return $send(self, 'to_enum', ["each_entry"].concat($to_a(data)), function $$19(){var self = $$19.$$s == null ? this : $$19.$$s; 9879 9880 return self.$enumerator_size()}, {$$s: self}) 9881 }; 9882 9883 self.$each.$$p = function() { 9884 var item = $Opal.$destructure(arguments); 9885 9886 $yield1(block, item); 9887 } 9888 9889 self.$each.apply(self, data); 9890 9891 return self; 9892 ; 9893 }, -1); 9894 9895 $def(self, '$each_slice', function $$each_slice(n) { 9896 var block = $$each_slice.$$p || nil, self = this; 9897 9898 $$each_slice.$$p = null; 9899 9900 ; 9901 n = $coerce_to(n, $$$('Integer'), 'to_int'); 9902 if ($truthy(n <= 0)) { 9903 $Kernel.$raise($$$('ArgumentError'), "invalid slice size") 9904 }; 9905 if (!(block !== nil)) { 9906 return $send(self, 'enum_for', ["each_slice", n], function $$20(){var self = $$20.$$s == null ? this : $$20.$$s; 9907 9908 if ($truthy(self['$respond_to?']("size"))) { 9909 return $rb_divide(self.$size(), n).$ceil() 9910 } else { 9911 return nil 9912 }}, {$$s: self}) 9913 }; 9914 9915 var slice = [] 9916 9917 self.$each.$$p = function() { 9918 var param = $Opal.$destructure(arguments); 9919 9920 slice.push(param); 9921 9922 if (slice.length === n) { 9923 $yield1(block, slice); 9924 slice = []; 9925 } 9926 }; 9927 9928 self.$each(); 9929 9930 // our "last" group, if smaller than n then won't have been yielded 9931 if (slice.length > 0) { 9932 $yield1(block, slice); 9933 } 9934 ; 9935 return self; 9936 }); 9937 9938 $def(self, '$each_with_index', function $$each_with_index($a) { 9939 var block = $$each_with_index.$$p || nil, $post_args, args, self = this; 9940 9941 $$each_with_index.$$p = null; 9942 9943 ; 9944 $post_args = $slice(arguments); 9945 args = $post_args; 9946 if (!(block !== nil)) { 9947 return $send(self, 'enum_for', ["each_with_index"].concat($to_a(args)), function $$21(){var self = $$21.$$s == null ? this : $$21.$$s; 9948 9949 return self.$enumerator_size()}, {$$s: self}) 9950 }; 9951 9952 var index = 0; 9953 9954 self.$each.$$p = function() { 9955 var param = $Opal.$destructure(arguments); 9956 9957 block(param, index); 9958 9959 index++; 9960 }; 9961 9962 self.$each.apply(self, args); 9963 ; 9964 return self; 9965 }, -1); 9966 9967 $def(self, '$each_with_object', function $$each_with_object(object) { 9968 var block = $$each_with_object.$$p || nil, self = this; 9969 9970 $$each_with_object.$$p = null; 9971 9972 ; 9973 if (!(block !== nil)) { 9974 return $send(self, 'enum_for', ["each_with_object", object], function $$22(){var self = $$22.$$s == null ? this : $$22.$$s; 9975 9976 return self.$enumerator_size()}, {$$s: self}) 9977 }; 9978 9979 self.$each.$$p = function() { 9980 var param = $Opal.$destructure(arguments); 9981 9982 block(param, object); 9983 }; 9984 9985 self.$each(); 9986 ; 9987 return object; 9988 }); 9989 9990 $def(self, '$entries', function $$entries($a) { 9991 var $post_args, args, self = this; 9992 9993 9994 $post_args = $slice(arguments); 9995 args = $post_args; 9996 9997 var result = []; 9998 9999 self.$each.$$p = function() { 10000 result.push($Opal.$destructure(arguments)); 10001 }; 10002 10003 self.$each.apply(self, args); 10004 10005 return result; 10006 ; 10007 }, -1); 10008 10009 $def(self, '$filter_map', function $$filter_map() { 10010 var block = $$filter_map.$$p || nil, self = this; 10011 10012 $$filter_map.$$p = null; 10013 10014 ; 10015 if (!(block !== nil)) { 10016 return $send(self, 'enum_for', ["filter_map"], function $$23(){var self = $$23.$$s == null ? this : $$23.$$s; 10017 10018 return self.$enumerator_size()}, {$$s: self}) 10019 }; 10020 return $send($send(self, 'map', [], block.$to_proc()), 'select', [], "itself".$to_proc()); 10021 }); 10022 10023 $def(self, '$find_all', function $$find_all() { 10024 var block = $$find_all.$$p || nil, self = this; 10025 10026 $$find_all.$$p = null; 10027 10028 ; 10029 if (!(block !== nil)) { 10030 return $send(self, 'enum_for', ["find_all"], function $$24(){var self = $$24.$$s == null ? this : $$24.$$s; 10031 10032 return self.$enumerator_size()}, {$$s: self}) 10033 }; 10034 10035 var result = []; 10036 10037 self.$each.$$p = function() { 10038 var param = $Opal.$destructure(arguments), 10039 value = $yield1(block, param); 10040 10041 if ($truthy(value)) { 10042 result.push(param); 10043 } 10044 }; 10045 10046 self.$each(); 10047 10048 return result; 10049 ; 10050 }); 10051 10052 $def(self, '$find_index', function $$find_index(object) {try { var $t_return = $thrower('return'); 10053 var block = $$find_index.$$p || nil, self = this, index = nil; 10054 10055 $$find_index.$$p = null; 10056 10057 ; 10058 ; 10059 if ($truthy(object === undefined && block === nil)) { 10060 return self.$enum_for("find_index") 10061 }; 10062 10063 if (object != null && block !== nil) { 10064 self.$warn("warning: given block not used") 10065 } 10066 ; 10067 index = 0; 10068 if ($truthy(object != null)) { 10069 $send(self, 'each', [], function $$25($a){var $post_args, value; 10070 10071 10072 $post_args = $slice(arguments); 10073 value = $post_args; 10074 if ($eqeq($Opal.$destructure(value), object)) { 10075 $t_return.$throw(index) 10076 }; 10077 return index += 1;;}, {$$arity: -1, $$ret: $t_return}) 10078 } else { 10079 $send(self, 'each', [], function $$26($a){var $post_args, value; 10080 10081 10082 $post_args = $slice(arguments); 10083 value = $post_args; 10084 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 10085 $t_return.$throw(index) 10086 }; 10087 return index += 1;;}, {$$arity: -1, $$ret: $t_return}) 10088 }; 10089 return nil;} catch($e) { 10090 if ($e === $t_return) return $e.$v; 10091 throw $e; 10092 } 10093 }, -1); 10094 10095 $def(self, '$first', function $$first(number) {try { var $t_return = $thrower('return'); 10096 var self = this, result = nil, current = nil; 10097 10098 10099 ; 10100 if ($truthy(number === undefined)) { 10101 return $send(self, 'each', [], function $$27(value){ 10102 10103 if (value == null) value = nil; 10104 $t_return.$throw(value);}, {$$ret: $t_return}) 10105 } else { 10106 10107 result = []; 10108 number = $coerce_to(number, $$$('Integer'), 'to_int'); 10109 if ($truthy(number < 0)) { 10110 $Kernel.$raise($$$('ArgumentError'), "attempt to take negative size") 10111 }; 10112 if ($truthy(number == 0)) { 10113 return [] 10114 }; 10115 current = 0; 10116 $send(self, 'each', [], function $$28($a){var $post_args, args; 10117 10118 10119 $post_args = $slice(arguments); 10120 args = $post_args; 10121 result.push($Opal.$destructure(args)); 10122 if ($truthy(number <= ++current)) { 10123 $t_return.$throw(result) 10124 } else { 10125 return nil 10126 };}, {$$arity: -1, $$ret: $t_return}); 10127 return result; 10128 };} catch($e) { 10129 if ($e === $t_return) return $e.$v; 10130 throw $e; 10131 } 10132 }, -1); 10133 10134 $def(self, '$grep', function $$grep(pattern) { 10135 var block = $$grep.$$p || nil, self = this, result = nil; 10136 10137 $$grep.$$p = null; 10138 10139 ; 10140 result = []; 10141 $send(self, 'each', [], function $$29($a){var $post_args, value, cmp = nil; 10142 10143 10144 $post_args = $slice(arguments); 10145 value = $post_args; 10146 cmp = comparableForPattern(value); 10147 if (!$truthy($send(pattern, '__send__', ["==="].concat($to_a(cmp))))) { 10148 return nil 10149 }; 10150 if ((block !== nil)) { 10151 10152 if ($truthy($rb_gt(value.$length(), 1))) { 10153 value = [value] 10154 }; 10155 value = Opal.yieldX(block, $to_a(value)); 10156 } else if ($truthy($rb_le(value.$length(), 1))) { 10157 value = value['$[]'](0) 10158 }; 10159 return result.$push(value);}, -1); 10160 return result; 10161 }); 10162 10163 $def(self, '$grep_v', function $$grep_v(pattern) { 10164 var block = $$grep_v.$$p || nil, self = this, result = nil; 10165 10166 $$grep_v.$$p = null; 10167 10168 ; 10169 result = []; 10170 $send(self, 'each', [], function $$30($a){var $post_args, value, cmp = nil; 10171 10172 10173 $post_args = $slice(arguments); 10174 value = $post_args; 10175 cmp = comparableForPattern(value); 10176 if ($truthy($send(pattern, '__send__', ["==="].concat($to_a(cmp))))) { 10177 return nil 10178 }; 10179 if ((block !== nil)) { 10180 10181 if ($truthy($rb_gt(value.$length(), 1))) { 10182 value = [value] 10183 }; 10184 value = Opal.yieldX(block, $to_a(value)); 10185 } else if ($truthy($rb_le(value.$length(), 1))) { 10186 value = value['$[]'](0) 10187 }; 10188 return result.$push(value);}, -1); 10189 return result; 10190 }); 10191 10192 $def(self, '$group_by', function $$group_by() { 10193 var block = $$group_by.$$p || nil, $a, self = this, hash = nil, $ret_or_1 = nil; 10194 10195 $$group_by.$$p = null; 10196 10197 ; 10198 if (!(block !== nil)) { 10199 return $send(self, 'enum_for', ["group_by"], function $$31(){var self = $$31.$$s == null ? this : $$31.$$s; 10200 10201 return self.$enumerator_size()}, {$$s: self}) 10202 }; 10203 hash = $hash2([], {}); 10204 10205 var result; 10206 10207 self.$each.$$p = function() { 10208 var param = $Opal.$destructure(arguments), 10209 value = $yield1(block, param); 10210 10211 ($truthy(($ret_or_1 = hash['$[]'](value))) ? ($ret_or_1) : (($a = [value, []], $send(hash, '[]=', $a), $a[$a.length - 1])))['$<<'](param); 10212 } 10213 10214 self.$each(); 10215 10216 if (result !== undefined) { 10217 return result; 10218 } 10219 ; 10220 return hash; 10221 }); 10222 10223 $def(self, '$include?', function $Enumerable_include$ques$32(obj) {try { var $t_return = $thrower('return'); 10224 var self = this; 10225 10226 10227 $send(self, 'each', [], function $$33($a){var $post_args, args; 10228 10229 10230 $post_args = $slice(arguments); 10231 args = $post_args; 10232 if ($eqeq($Opal.$destructure(args), obj)) { 10233 $t_return.$throw(true) 10234 } else { 10235 return nil 10236 };}, {$$arity: -1, $$ret: $t_return}); 10237 return false;} catch($e) { 10238 if ($e === $t_return) return $e.$v; 10239 throw $e; 10240 } 10241 }); 10242 10243 $def(self, '$inject', function $$inject(object, sym) { 10244 var block = $$inject.$$p || nil, self = this; 10245 10246 $$inject.$$p = null; 10247 10248 ; 10249 ; 10250 ; 10251 10252 var result = object; 10253 10254 if (block !== nil && sym === undefined) { 10255 self.$each.$$p = function() { 10256 var value = $Opal.$destructure(arguments); 10257 10258 if (result === undefined) { 10259 result = value; 10260 return; 10261 } 10262 10263 value = $yieldX(block, [result, value]); 10264 10265 result = value; 10266 }; 10267 } 10268 else { 10269 if (sym === undefined) { 10270 if (!$$$('Symbol')['$==='](object)) { 10271 $Kernel.$raise($$$('TypeError'), "" + (object.$inspect()) + " is not a Symbol"); 10272 } 10273 10274 sym = object; 10275 result = undefined; 10276 } 10277 10278 self.$each.$$p = function() { 10279 var value = $Opal.$destructure(arguments); 10280 10281 if (result === undefined) { 10282 result = value; 10283 return; 10284 } 10285 10286 result = (result).$__send__(sym, value); 10287 }; 10288 } 10289 10290 self.$each(); 10291 10292 return result == undefined ? nil : result; 10293 ; 10294 }, -1); 10295 10296 $def(self, '$lazy', function $$lazy() { 10297 var self = this; 10298 10299 return $send($$$($$$('Enumerator'), 'Lazy'), 'new', [self, self.$enumerator_size()], function $$34(enum$, $a){var $post_args, args; 10300 10301 10302 if (enum$ == null) enum$ = nil; 10303 $post_args = $slice(arguments, 1); 10304 args = $post_args; 10305 return $send(enum$, 'yield', $to_a(args));}, -2) 10306 }); 10307 10308 $def(self, '$enumerator_size', function $$enumerator_size() { 10309 var self = this; 10310 10311 if ($truthy(self['$respond_to?']("size"))) { 10312 return self.$size() 10313 } else { 10314 return nil 10315 } 10316 }); 10317 10318 $def(self, '$max', function $$max(n) { 10319 var block = $$max.$$p || nil, self = this; 10320 10321 $$max.$$p = null; 10322 10323 ; 10324 ; 10325 10326 if (n === undefined || n === nil) { 10327 var result, value; 10328 10329 self.$each.$$p = function() { 10330 var item = $Opal.$destructure(arguments); 10331 10332 if (result === undefined) { 10333 result = item; 10334 return; 10335 } 10336 10337 if (block !== nil) { 10338 value = $yieldX(block, [item, result]); 10339 } else { 10340 value = (item)['$<=>'](result); 10341 } 10342 10343 if (value === nil) { 10344 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 10345 } 10346 10347 if (value > 0) { 10348 result = item; 10349 } 10350 } 10351 10352 self.$each(); 10353 10354 if (result === undefined) { 10355 return nil; 10356 } else { 10357 return result; 10358 } 10359 } 10360 10361 n = $coerce_to(n, $$$('Integer'), 'to_int'); 10362 ; 10363 return $send(self, 'sort', [], block.$to_proc()).$reverse().$first(n); 10364 }, -1); 10365 10366 $def(self, '$max_by', function $$max_by(n) { 10367 var block = $$max_by.$$p || nil, self = this; 10368 10369 $$max_by.$$p = null; 10370 10371 ; 10372 if (n == null) n = nil; 10373 if (!$truthy(block)) { 10374 return $send(self, 'enum_for', ["max_by", n], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 10375 10376 return self.$enumerator_size()}, {$$s: self}) 10377 }; 10378 if (!$truthy(n['$nil?']())) { 10379 return $send(self, 'sort_by', [], block.$to_proc()).$reverse().$take(n) 10380 }; 10381 10382 var result, 10383 by; 10384 10385 self.$each.$$p = function() { 10386 var param = $Opal.$destructure(arguments), 10387 value = $yield1(block, param); 10388 10389 if (result === undefined) { 10390 result = param; 10391 by = value; 10392 return; 10393 } 10394 10395 if ((value)['$<=>'](by) > 0) { 10396 result = param 10397 by = value; 10398 } 10399 }; 10400 10401 self.$each(); 10402 10403 return result === undefined ? nil : result; 10404 ; 10405 }, -1); 10406 10407 $def(self, '$min', function $$min(n) { 10408 var block = $$min.$$p || nil, self = this; 10409 10410 $$min.$$p = null; 10411 10412 ; 10413 if (n == null) n = nil; 10414 if (!$truthy(n['$nil?']())) { 10415 if ((block !== nil)) { 10416 return $send(self, 'sort', [], function $$36(a, b){ 10417 10418 if (a == null) a = nil; 10419 if (b == null) b = nil; 10420 return Opal.yieldX(block, [a, b]);;}).$take(n) 10421 } else { 10422 return self.$sort().$take(n) 10423 } 10424 }; 10425 10426 var result; 10427 10428 if (block !== nil) { 10429 self.$each.$$p = function() { 10430 var param = $Opal.$destructure(arguments); 10431 10432 if (result === undefined) { 10433 result = param; 10434 return; 10435 } 10436 10437 var value = block(param, result); 10438 10439 if (value === nil) { 10440 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 10441 } 10442 10443 if (value < 0) { 10444 result = param; 10445 } 10446 }; 10447 } 10448 else { 10449 self.$each.$$p = function() { 10450 var param = $Opal.$destructure(arguments); 10451 10452 if (result === undefined) { 10453 result = param; 10454 return; 10455 } 10456 10457 if ($Opal.$compare(param, result) < 0) { 10458 result = param; 10459 } 10460 }; 10461 } 10462 10463 self.$each(); 10464 10465 return result === undefined ? nil : result; 10466 ; 10467 }, -1); 10468 10469 $def(self, '$min_by', function $$min_by(n) { 10470 var block = $$min_by.$$p || nil, self = this; 10471 10472 $$min_by.$$p = null; 10473 10474 ; 10475 if (n == null) n = nil; 10476 if (!$truthy(block)) { 10477 return $send(self, 'enum_for', ["min_by", n], function $$37(){var self = $$37.$$s == null ? this : $$37.$$s; 10478 10479 return self.$enumerator_size()}, {$$s: self}) 10480 }; 10481 if (!$truthy(n['$nil?']())) { 10482 return $send(self, 'sort_by', [], block.$to_proc()).$take(n) 10483 }; 10484 10485 var result, 10486 by; 10487 10488 self.$each.$$p = function() { 10489 var param = $Opal.$destructure(arguments), 10490 value = $yield1(block, param); 10491 10492 if (result === undefined) { 10493 result = param; 10494 by = value; 10495 return; 10496 } 10497 10498 if ((value)['$<=>'](by) < 0) { 10499 result = param 10500 by = value; 10501 } 10502 }; 10503 10504 self.$each(); 10505 10506 return result === undefined ? nil : result; 10507 ; 10508 }, -1); 10509 10510 $def(self, '$minmax', function $$minmax() { 10511 var block = $$minmax.$$p || nil, self = this, $ret_or_1 = nil; 10512 10513 $$minmax.$$p = null; 10514 10515 ; 10516 block = ($truthy(($ret_or_1 = block)) ? ($ret_or_1) : ($send($Kernel, 'proc', [], function $$38(a, b){ 10517 10518 if (a == null) a = nil; 10519 if (b == null) b = nil; 10520 return a['$<=>'](b);}))); 10521 10522 var min = nil, max = nil, first_time = true; 10523 10524 self.$each.$$p = function() { 10525 var element = $Opal.$destructure(arguments); 10526 if (first_time) { 10527 min = max = element; 10528 first_time = false; 10529 } else { 10530 var min_cmp = block.$call(min, element); 10531 10532 if (min_cmp === nil) { 10533 $Kernel.$raise($$$('ArgumentError'), "comparison failed") 10534 } else if (min_cmp > 0) { 10535 min = element; 10536 } 10537 10538 var max_cmp = block.$call(max, element); 10539 10540 if (max_cmp === nil) { 10541 $Kernel.$raise($$$('ArgumentError'), "comparison failed") 10542 } else if (max_cmp < 0) { 10543 max = element; 10544 } 10545 } 10546 } 10547 10548 self.$each(); 10549 10550 return [min, max]; 10551 ; 10552 }); 10553 10554 $def(self, '$minmax_by', function $$minmax_by() { 10555 var block = $$minmax_by.$$p || nil, self = this; 10556 10557 $$minmax_by.$$p = null; 10558 10559 ; 10560 if (!$truthy(block)) { 10561 return $send(self, 'enum_for', ["minmax_by"], function $$39(){var self = $$39.$$s == null ? this : $$39.$$s; 10562 10563 return self.$enumerator_size()}, {$$s: self}) 10564 }; 10565 10566 var min_result = nil, 10567 max_result = nil, 10568 min_by, 10569 max_by; 10570 10571 self.$each.$$p = function() { 10572 var param = $Opal.$destructure(arguments), 10573 value = $yield1(block, param); 10574 10575 if ((min_by === undefined) || (value)['$<=>'](min_by) < 0) { 10576 min_result = param; 10577 min_by = value; 10578 } 10579 10580 if ((max_by === undefined) || (value)['$<=>'](max_by) > 0) { 10581 max_result = param; 10582 max_by = value; 10583 } 10584 }; 10585 10586 self.$each(); 10587 10588 return [min_result, max_result]; 10589 ; 10590 }); 10591 10592 $def(self, '$none?', function $Enumerable_none$ques$40(pattern) {try { var $t_return = $thrower('return'); 10593 var block = $Enumerable_none$ques$40.$$p || nil, self = this; 10594 10595 $Enumerable_none$ques$40.$$p = null; 10596 10597 ; 10598 ; 10599 if ($truthy(pattern !== undefined)) { 10600 $send(self, 'each', [], function $$41($a){var $post_args, value, comparable = nil; 10601 10602 10603 $post_args = $slice(arguments); 10604 value = $post_args; 10605 comparable = comparableForPattern(value); 10606 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 10607 $t_return.$throw(false) 10608 } else { 10609 return nil 10610 };}, {$$arity: -1, $$ret: $t_return}) 10611 } else if ((block !== nil)) { 10612 $send(self, 'each', [], function $$42($a){var $post_args, value; 10613 10614 10615 $post_args = $slice(arguments); 10616 value = $post_args; 10617 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 10618 $t_return.$throw(false) 10619 } else { 10620 return nil 10621 };}, {$$arity: -1, $$ret: $t_return}) 10622 } else { 10623 $send(self, 'each', [], function $$43($a){var $post_args, value, item = nil; 10624 10625 10626 $post_args = $slice(arguments); 10627 value = $post_args; 10628 item = $Opal.$destructure(value); 10629 if ($truthy(item)) { 10630 $t_return.$throw(false) 10631 } else { 10632 return nil 10633 };}, {$$arity: -1, $$ret: $t_return}) 10634 }; 10635 return true;} catch($e) { 10636 if ($e === $t_return) return $e.$v; 10637 throw $e; 10638 } 10639 }, -1); 10640 10641 $def(self, '$one?', function $Enumerable_one$ques$44(pattern) {try { var $t_return = $thrower('return'); 10642 var block = $Enumerable_one$ques$44.$$p || nil, self = this, count = nil; 10643 10644 $Enumerable_one$ques$44.$$p = null; 10645 10646 ; 10647 ; 10648 count = 0; 10649 if ($truthy(pattern !== undefined)) { 10650 $send(self, 'each', [], function $$45($a){var $post_args, value, comparable = nil; 10651 10652 10653 $post_args = $slice(arguments); 10654 value = $post_args; 10655 comparable = comparableForPattern(value); 10656 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 10657 10658 count = $rb_plus(count, 1); 10659 if ($truthy($rb_gt(count, 1))) { 10660 $t_return.$throw(false) 10661 } else { 10662 return nil 10663 }; 10664 } else { 10665 return nil 10666 };}, {$$arity: -1, $$ret: $t_return}) 10667 } else if ((block !== nil)) { 10668 $send(self, 'each', [], function $$46($a){var $post_args, value; 10669 10670 10671 $post_args = $slice(arguments); 10672 value = $post_args; 10673 if (!$truthy(Opal.yieldX(block, $to_a(value)))) { 10674 return nil 10675 }; 10676 count = $rb_plus(count, 1); 10677 if ($truthy($rb_gt(count, 1))) { 10678 $t_return.$throw(false) 10679 } else { 10680 return nil 10681 };}, {$$arity: -1, $$ret: $t_return}) 10682 } else { 10683 $send(self, 'each', [], function $$47($a){var $post_args, value; 10684 10685 10686 $post_args = $slice(arguments); 10687 value = $post_args; 10688 if (!$truthy($Opal.$destructure(value))) { 10689 return nil 10690 }; 10691 count = $rb_plus(count, 1); 10692 if ($truthy($rb_gt(count, 1))) { 10693 $t_return.$throw(false) 10694 } else { 10695 return nil 10696 };}, {$$arity: -1, $$ret: $t_return}) 10697 }; 10698 return count['$=='](1);} catch($e) { 10699 if ($e === $t_return) return $e.$v; 10700 throw $e; 10701 } 10702 }, -1); 10703 10704 $def(self, '$partition', function $$partition() { 10705 var block = $$partition.$$p || nil, self = this; 10706 10707 $$partition.$$p = null; 10708 10709 ; 10710 if (!(block !== nil)) { 10711 return $send(self, 'enum_for', ["partition"], function $$48(){var self = $$48.$$s == null ? this : $$48.$$s; 10712 10713 return self.$enumerator_size()}, {$$s: self}) 10714 }; 10715 10716 var truthy = [], falsy = [], result; 10717 10718 self.$each.$$p = function() { 10719 var param = $Opal.$destructure(arguments), 10720 value = $yield1(block, param); 10721 10722 if ($truthy(value)) { 10723 truthy.push(param); 10724 } 10725 else { 10726 falsy.push(param); 10727 } 10728 }; 10729 10730 self.$each(); 10731 10732 return [truthy, falsy]; 10733 ; 10734 }); 10735 10736 $def(self, '$reject', function $$reject() { 10737 var block = $$reject.$$p || nil, self = this; 10738 10739 $$reject.$$p = null; 10740 10741 ; 10742 if (!(block !== nil)) { 10743 return $send(self, 'enum_for', ["reject"], function $$49(){var self = $$49.$$s == null ? this : $$49.$$s; 10744 10745 return self.$enumerator_size()}, {$$s: self}) 10746 }; 10747 10748 var result = []; 10749 10750 self.$each.$$p = function() { 10751 var param = $Opal.$destructure(arguments), 10752 value = $yield1(block, param); 10753 10754 if (!$truthy(value)) { 10755 result.push(param); 10756 } 10757 }; 10758 10759 self.$each(); 10760 10761 return result; 10762 ; 10763 }); 10764 10765 $def(self, '$reverse_each', function $$reverse_each() { 10766 var block = $$reverse_each.$$p || nil, self = this; 10767 10768 $$reverse_each.$$p = null; 10769 10770 ; 10771 if (!(block !== nil)) { 10772 return $send(self, 'enum_for', ["reverse_each"], function $$50(){var self = $$50.$$s == null ? this : $$50.$$s; 10773 10774 return self.$enumerator_size()}, {$$s: self}) 10775 }; 10776 10777 var result = []; 10778 10779 self.$each.$$p = function() { 10780 result.push(arguments); 10781 }; 10782 10783 self.$each(); 10784 10785 for (var i = result.length - 1; i >= 0; i--) { 10786 $yieldX(block, result[i]); 10787 } 10788 10789 return result; 10790 ; 10791 }); 10792 10793 $def(self, '$slice_before', function $$slice_before(pattern) { 10794 var block = $$slice_before.$$p || nil, self = this; 10795 10796 $$slice_before.$$p = null; 10797 10798 ; 10799 ; 10800 if ($truthy(pattern === undefined && block === nil)) { 10801 $Kernel.$raise($$$('ArgumentError'), "both pattern and block are given") 10802 }; 10803 if ($truthy(pattern !== undefined && block !== nil || arguments.length > 1)) { 10804 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " expected 1)") 10805 }; 10806 return $send($$$('Enumerator'), 'new', [], function $$51(e){var self = $$51.$$s == null ? this : $$51.$$s; 10807 10808 10809 if (e == null) e = nil; 10810 10811 var slice = []; 10812 10813 if (block !== nil) { 10814 if (pattern === undefined) { 10815 self.$each.$$p = function() { 10816 var param = $Opal.$destructure(arguments), 10817 value = $yield1(block, param); 10818 10819 if ($truthy(value) && slice.length > 0) { 10820 e['$<<'](slice); 10821 slice = []; 10822 } 10823 10824 slice.push(param); 10825 }; 10826 } 10827 else { 10828 self.$each.$$p = function() { 10829 var param = $Opal.$destructure(arguments), 10830 value = block(param, pattern.$dup()); 10831 10832 if ($truthy(value) && slice.length > 0) { 10833 e['$<<'](slice); 10834 slice = []; 10835 } 10836 10837 slice.push(param); 10838 }; 10839 } 10840 } 10841 else { 10842 self.$each.$$p = function() { 10843 var param = $Opal.$destructure(arguments), 10844 value = pattern['$==='](param); 10845 10846 if ($truthy(value) && slice.length > 0) { 10847 e['$<<'](slice); 10848 slice = []; 10849 } 10850 10851 slice.push(param); 10852 }; 10853 } 10854 10855 self.$each(); 10856 10857 if (slice.length > 0) { 10858 e['$<<'](slice); 10859 } 10860 ;}, {$$s: self}); 10861 }, -1); 10862 10863 $def(self, '$slice_after', function $$slice_after(pattern) { 10864 var block = $$slice_after.$$p || nil, self = this; 10865 10866 $$slice_after.$$p = null; 10867 10868 ; 10869 ; 10870 if ($truthy(pattern === undefined && block === nil)) { 10871 $Kernel.$raise($$$('ArgumentError'), "both pattern and block are given") 10872 }; 10873 if ($truthy(pattern !== undefined && block !== nil || arguments.length > 1)) { 10874 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " expected 1)") 10875 }; 10876 if ($truthy(pattern !== undefined)) { 10877 block = $send($Kernel, 'proc', [], function $$52(e){ 10878 10879 if (e == null) e = nil; 10880 return pattern['$==='](e);}) 10881 }; 10882 return $send($$$('Enumerator'), 'new', [], function $$53(yielder){var self = $$53.$$s == null ? this : $$53.$$s; 10883 10884 10885 if (yielder == null) yielder = nil; 10886 10887 var accumulate; 10888 10889 self.$each.$$p = function() { 10890 var element = $Opal.$destructure(arguments), 10891 end_chunk = $yield1(block, element); 10892 10893 if (accumulate == null) { 10894 accumulate = []; 10895 } 10896 10897 if ($truthy(end_chunk)) { 10898 accumulate.push(element); 10899 yielder.$yield(accumulate); 10900 accumulate = null; 10901 } else { 10902 accumulate.push(element) 10903 } 10904 } 10905 10906 self.$each(); 10907 10908 if (accumulate != null) { 10909 yielder.$yield(accumulate); 10910 } 10911 ;}, {$$s: self}); 10912 }, -1); 10913 10914 $def(self, '$slice_when', function $$slice_when() { 10915 var block = $$slice_when.$$p || nil, self = this; 10916 10917 $$slice_when.$$p = null; 10918 10919 ; 10920 if (!(block !== nil)) { 10921 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1)") 10922 }; 10923 return $send($$$('Enumerator'), 'new', [], function $$54(yielder){var self = $$54.$$s == null ? this : $$54.$$s; 10924 10925 10926 if (yielder == null) yielder = nil; 10927 10928 var slice = nil, last_after = nil; 10929 10930 self.$each_cons.$$p = function() { 10931 var params = $Opal.$destructure(arguments), 10932 before = params[0], 10933 after = params[1], 10934 match = $yieldX(block, [before, after]); 10935 10936 last_after = after; 10937 10938 if (slice === nil) { 10939 slice = []; 10940 } 10941 10942 if ($truthy(match)) { 10943 slice.push(before); 10944 yielder.$yield(slice); 10945 slice = []; 10946 } else { 10947 slice.push(before); 10948 } 10949 } 10950 10951 self.$each_cons(2); 10952 10953 if (slice !== nil) { 10954 slice.push(last_after); 10955 yielder.$yield(slice); 10956 } 10957 ;}, {$$s: self}); 10958 }); 10959 10960 $def(self, '$sort', function $$sort() { 10961 var block = $$sort.$$p || nil, self = this, ary = nil; 10962 10963 $$sort.$$p = null; 10964 10965 ; 10966 ary = self.$to_a(); 10967 if (!(block !== nil)) { 10968 block = $lambda(function $$55(a, b){ 10969 10970 if (a == null) a = nil; 10971 if (b == null) b = nil; 10972 return a['$<=>'](b);}) 10973 }; 10974 return $send(ary, 'sort', [], block.$to_proc()); 10975 }); 10976 10977 $def(self, '$sort_by', function $$sort_by() { 10978 var block = $$sort_by.$$p || nil, self = this, dup = nil; 10979 10980 $$sort_by.$$p = null; 10981 10982 ; 10983 if (!(block !== nil)) { 10984 return $send(self, 'enum_for', ["sort_by"], function $$56(){var self = $$56.$$s == null ? this : $$56.$$s; 10985 10986 return self.$enumerator_size()}, {$$s: self}) 10987 }; 10988 dup = $send(self, 'map', [], function $$57(){var arg = nil; 10989 10990 10991 arg = $Opal.$destructure(arguments); 10992 return [Opal.yield1(block, arg), arg];}); 10993 $send(dup, 'sort!', [], function $$58(a, b){ 10994 10995 if (a == null) a = nil; 10996 if (b == null) b = nil; 10997 return (a[0])['$<=>'](b[0]);}); 10998 return $send(dup, 'map!', [], function $$59(i){ 10999 11000 if (i == null) i = nil; 11001 return i[1];;}); 11002 }); 11003 11004 $def(self, '$sum', function $$sum(initial) { 11005 var $yield = $$sum.$$p || nil, self = this, result = nil, compensation = nil; 11006 11007 $$sum.$$p = null; 11008 11009 if (initial == null) initial = 0; 11010 result = initial; 11011 compensation = 0; 11012 $send(self, 'each', [], function $$60($a){var $post_args, args, item = nil, y = nil, t = nil; 11013 11014 11015 $post_args = $slice(arguments); 11016 args = $post_args; 11017 item = (($yield !== nil) ? (Opal.yieldX($yield, $to_a(args))) : ($Opal.$destructure(args))); 11018 if (($not([$$$($$$('Float'), 'INFINITY'), $$$($$$('Float'), 'INFINITY')['$-@']()]['$include?'](item)) && ($truthy(item['$respond_to?']("-"))))) { 11019 11020 y = $rb_minus(item, compensation); 11021 t = $rb_plus(result, y); 11022 compensation = $rb_minus($rb_minus(t, result), y); 11023 return (result = t); 11024 } else { 11025 return (result = $rb_plus(result, item)) 11026 };}, -1); 11027 return result; 11028 }, -1); 11029 11030 $def(self, '$take', function $$take(num) { 11031 var self = this; 11032 11033 return self.$first(num) 11034 }); 11035 11036 $def(self, '$take_while', function $$take_while() {try { var $t_return = $thrower('return'); 11037 var block = $$take_while.$$p || nil, self = this, result = nil; 11038 11039 $$take_while.$$p = null; 11040 11041 ; 11042 if (!$truthy(block)) { 11043 return self.$enum_for("take_while") 11044 }; 11045 result = []; 11046 return $send(self, 'each', [], function $$61($a){var $post_args, args, value = nil; 11047 11048 11049 $post_args = $slice(arguments); 11050 args = $post_args; 11051 value = $Opal.$destructure(args); 11052 if (!$truthy(Opal.yield1(block, value))) { 11053 $t_return.$throw(result) 11054 }; 11055 return result.push(value);;}, {$$arity: -1, $$ret: $t_return});} catch($e) { 11056 if ($e === $t_return) return $e.$v; 11057 throw $e; 11058 } 11059 }); 11060 11061 $def(self, '$uniq', function $$uniq() { 11062 var block = $$uniq.$$p || nil, self = this, hash = nil; 11063 11064 $$uniq.$$p = null; 11065 11066 ; 11067 hash = $hash2([], {}); 11068 $send(self, 'each', [], function $$62($a){var $post_args, args, $b, value = nil, produced = nil; 11069 11070 11071 $post_args = $slice(arguments); 11072 args = $post_args; 11073 value = $Opal.$destructure(args); 11074 produced = ((block !== nil) ? (Opal.yield1(block, value)) : (value)); 11075 if ($truthy(hash['$key?'](produced))) { 11076 return nil 11077 } else { 11078 return ($b = [produced, value], $send(hash, '[]=', $b), $b[$b.length - 1]) 11079 };}, -1); 11080 return hash.$values(); 11081 }); 11082 11083 $def(self, '$tally', function $$tally(hash) { 11084 var self = this, out = nil; 11085 11086 11087 ; 11088 if (hash && hash !== nil) { $deny_frozen_access(hash); }; 11089 out = $send($send(self, 'group_by', [], "itself".$to_proc()), 'transform_values', [], "count".$to_proc()); 11090 if ($truthy(hash)) { 11091 11092 $send(out, 'each', [], function $$63(k, v){var $a; 11093 11094 11095 if (k == null) k = nil; 11096 if (v == null) v = nil; 11097 return ($a = [k, $rb_plus(hash.$fetch(k, 0), v)], $send(hash, '[]=', $a), $a[$a.length - 1]);}); 11098 return hash; 11099 } else { 11100 return out 11101 }; 11102 }, -1); 11103 11104 $def(self, '$to_h', function $$to_h($a) { 11105 var block = $$to_h.$$p || nil, $post_args, args, self = this; 11106 11107 $$to_h.$$p = null; 11108 11109 ; 11110 $post_args = $slice(arguments); 11111 args = $post_args; 11112 if ((block !== nil)) { 11113 return $send($send(self, 'map', [], block.$to_proc()), 'to_h', $to_a(args)) 11114 }; 11115 11116 var hash = $hash2([], {}); 11117 11118 self.$each.$$p = function() { 11119 var param = $Opal.$destructure(arguments); 11120 var ary = $Opal['$coerce_to?'](param, $$$('Array'), "to_ary"), key, val; 11121 if (!ary.$$is_array) { 11122 $Kernel.$raise($$$('TypeError'), "wrong element type " + ((ary).$class()) + " (expected array)") 11123 } 11124 if (ary.length !== 2) { 11125 $Kernel.$raise($$$('ArgumentError'), "wrong array length (expected 2, was " + ((ary).$length()) + ")") 11126 } 11127 key = ary[0]; 11128 val = ary[1]; 11129 11130 Opal.hash_put(hash, key, val); 11131 }; 11132 11133 self.$each.apply(self, args); 11134 11135 return hash; 11136 ; 11137 }, -1); 11138 11139 $def(self, '$to_set', function $$to_set($a, $b) { 11140 var block = $$to_set.$$p || nil, $post_args, klass, args, self = this; 11141 11142 $$to_set.$$p = null; 11143 11144 ; 11145 $post_args = $slice(arguments); 11146 11147 if ($post_args.length > 0) klass = $post_args.shift();if (klass == null) klass = $$('Set'); 11148 args = $post_args; 11149 return $send(klass, 'new', [self].concat($to_a(args)), block.$to_proc()); 11150 }, -1); 11151 11152 $def(self, '$zip', function $$zip($a) { 11153 var block = $$zip.$$p || nil, $post_args, others, self = this; 11154 11155 $$zip.$$p = null; 11156 11157 ; 11158 $post_args = $slice(arguments); 11159 others = $post_args; 11160 return $send(self.$to_a(), 'zip', $to_a(others)); 11161 }, -1); 11162 $alias(self, "find", "detect"); 11163 $alias(self, "filter", "find_all"); 11164 $alias(self, "flat_map", "collect_concat"); 11165 $alias(self, "map", "collect"); 11166 $alias(self, "member?", "include?"); 11167 $alias(self, "reduce", "inject"); 11168 $alias(self, "select", "find_all"); 11169 return $alias(self, "to_a", "entries"); 11170 })('::', $nesting) 11171}; 11172 11173Opal.modules["corelib/enumerator/arithmetic_sequence"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11174 var $klass = Opal.klass, $truthy = Opal.truthy, $to_a = Opal.to_a, $eqeq = Opal.eqeq, $Kernel = Opal.Kernel, $def = Opal.def, $rb_gt = Opal.rb_gt, $rb_lt = Opal.rb_lt, $rb_le = Opal.rb_le, $rb_ge = Opal.rb_ge, $rb_plus = Opal.rb_plus, $rb_minus = Opal.rb_minus, $eqeqeq = Opal.eqeqeq, $not = Opal.not, $rb_times = Opal.rb_times, $rb_divide = Opal.rb_divide, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 11175 11176 Opal.add_stubs('is_a?,==,raise,respond_to?,class,attr_reader,begin,end,exclude_end?,>,step,<,<=,>=,-@,_lesser_than_end?,<<,+,-,===,%,_greater_than_begin?,reverse,!,include?,*,to_i,abs,/,hash,inspect'); 11177 return (function($base, $super, $parent_nesting) { 11178 var self = $klass($base, $super, 'Enumerator'); 11179 11180 var $nesting = [self].concat($parent_nesting); 11181 11182 return (function($base, $super, $parent_nesting) { 11183 var self = $klass($base, $super, 'ArithmeticSequence'); 11184 11185 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11186 11187 $proto.step_arg2 = $proto.receiver_num = $proto.step_arg1 = $proto.step = $proto.range = $proto.topfx = $proto.bypfx = $proto.creation_method = $proto.skipped_arg = nil; 11188 11189 Opal.prop(self.$$prototype, '$$is_arithmetic_seq', true); 11190 var inf = Infinity; 11191 11192 $def(self, '$initialize', function $$initialize(range, step, creation_method) { 11193 var $a, self = this, $ret_or_1 = nil; 11194 11195 11196 ; 11197 if (creation_method == null) creation_method = "step"; 11198 self.creation_method = creation_method; 11199 if ($truthy(range['$is_a?']($$$('Array')))) { 11200 11201 $a = [].concat($to_a(range)), (self.step_arg1 = ($a[0] == null ? nil : $a[0])), (self.step_arg2 = ($a[1] == null ? nil : $a[1])), (self.topfx = ($a[2] == null ? nil : $a[2])), (self.bypfx = ($a[3] == null ? nil : $a[3])), $a; 11202 self.receiver_num = step; 11203 self.step = 1; 11204 self.range = ($truthy(self.step_arg2) ? (((self.step = self.step_arg2), Opal.Range.$new(self.receiver_num, self.step_arg1, false))) : ($truthy(self.step_arg1) ? (Opal.Range.$new(self.receiver_num, self.step_arg1, false)) : (Opal.Range.$new(self.receiver_num, nil, false)))); 11205 } else { 11206 11207 if (!$truthy(step)) { 11208 self.skipped_arg = true 11209 }; 11210 $a = [range, ($truthy(($ret_or_1 = step)) ? ($ret_or_1) : (1))], (self.range = $a[0]), (self.step = $a[1]), $a; 11211 }; 11212 self.object = self; 11213 if ($eqeq(self.step, 0)) { 11214 $Kernel.$raise($$('ArgumentError'), "step can't be 0") 11215 }; 11216 if ($truthy(self.step['$respond_to?']("to_int"))) { 11217 return nil 11218 } else { 11219 return $Kernel.$raise($$('ArgumentError'), "" + ("no implicit conversion of " + (self.step.$class()) + " ") + "into Integer") 11220 }; 11221 }, -2); 11222 self.$attr_reader("step"); 11223 11224 $def(self, '$begin', function $$begin() { 11225 var self = this; 11226 11227 return self.range.$begin() 11228 }); 11229 11230 $def(self, '$end', function $$end() { 11231 var self = this; 11232 11233 return self.range.$end() 11234 }); 11235 11236 $def(self, '$exclude_end?', function $ArithmeticSequence_exclude_end$ques$1() { 11237 var self = this; 11238 11239 return self.range['$exclude_end?']() 11240 }); 11241 11242 $def(self, '$_lesser_than_end?', function $ArithmeticSequence__lesser_than_end$ques$2(val) { 11243 var self = this, end_ = nil, $ret_or_1 = nil; 11244 11245 11246 end_ = ($truthy(($ret_or_1 = self.$end())) ? ($ret_or_1) : (inf)); 11247 if ($truthy($rb_gt(self.$step(), 0))) { 11248 if ($truthy(self['$exclude_end?']())) { 11249 return $rb_lt(val, end_) 11250 } else { 11251 return $rb_le(val, end_) 11252 } 11253 } else if ($truthy(self['$exclude_end?']())) { 11254 return $rb_gt(val, end_) 11255 } else { 11256 return $rb_ge(val, end_) 11257 }; 11258 }); 11259 11260 $def(self, '$_greater_than_begin?', function $ArithmeticSequence__greater_than_begin$ques$3(val) { 11261 var self = this, begin_ = nil, $ret_or_1 = nil; 11262 11263 11264 begin_ = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 11265 if ($truthy($rb_gt(self.$step(), 0))) { 11266 return $rb_gt(val, begin_) 11267 } else { 11268 return $rb_lt(val, begin_) 11269 }; 11270 }); 11271 11272 $def(self, '$first', function $$first(count) { 11273 var self = this, iter = nil, $ret_or_1 = nil, out = nil; 11274 11275 11276 ; 11277 iter = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 11278 if (!$truthy(count)) { 11279 return ($truthy(self['$_lesser_than_end?'](iter)) ? (iter) : (nil)) 11280 }; 11281 out = []; 11282 while ($truthy(($truthy(($ret_or_1 = self['$_lesser_than_end?'](iter))) ? ($rb_gt(count, 0)) : ($ret_or_1)))) { 11283 11284 out['$<<'](iter); 11285 iter = $rb_plus(iter, self.$step()); 11286 count = $rb_minus(count, 1); 11287 }; 11288 return out; 11289 }, -1); 11290 11291 $def(self, '$each', function $$each() { 11292 var block = $$each.$$p || nil, self = this, $ret_or_1 = nil, iter = nil; 11293 11294 $$each.$$p = null; 11295 11296 ; 11297 if (!(block !== nil)) { 11298 return self 11299 }; 11300 if ($eqeqeq(nil, ($ret_or_1 = self.$begin()))) { 11301 $Kernel.$raise($$('TypeError'), "nil can't be coerced into Integer") 11302 } else { 11303 nil 11304 }; 11305 iter = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 11306 while ($truthy(self['$_lesser_than_end?'](iter))) { 11307 11308 Opal.yield1(block, iter); 11309 iter = $rb_plus(iter, self.$step()); 11310 }; 11311 return self; 11312 }); 11313 11314 $def(self, '$last', function $$last(count) { 11315 var self = this, $ret_or_1 = nil, iter = nil, out = nil; 11316 11317 11318 ; 11319 if (($eqeqeq(inf, ($ret_or_1 = self.$end())) || ($eqeqeq((inf)['$-@'](), $ret_or_1)))) { 11320 $Kernel.$raise($$$('FloatDomainError'), self.$end()) 11321 } else if ($eqeqeq(nil, $ret_or_1)) { 11322 $Kernel.$raise($$$('RangeError'), "cannot get the last element of endless arithmetic sequence") 11323 } else { 11324 nil 11325 }; 11326 iter = $rb_minus(self.$end(), $rb_minus(self.$end(), self.$begin())['$%'](self.$step())); 11327 if (!$truthy(self['$_lesser_than_end?'](iter))) { 11328 iter = $rb_minus(iter, self.$step()) 11329 }; 11330 if (!$truthy(count)) { 11331 return ($truthy(self['$_greater_than_begin?'](iter)) ? (iter) : (nil)) 11332 }; 11333 out = []; 11334 while ($truthy(($truthy(($ret_or_1 = self['$_greater_than_begin?'](iter))) ? ($rb_gt(count, 0)) : ($ret_or_1)))) { 11335 11336 out['$<<'](iter); 11337 iter = $rb_minus(iter, self.$step()); 11338 count = $rb_minus(count, 1); 11339 }; 11340 return out.$reverse(); 11341 }, -1); 11342 11343 $def(self, '$size', function $$size() { 11344 var self = this, step_sign = nil, iter = nil; 11345 11346 11347 step_sign = ($truthy($rb_gt(self.$step(), 0)) ? (1) : (-1)); 11348 if ($not(self['$_lesser_than_end?'](self.$begin()))) { 11349 return 0 11350 } else if ($truthy([(inf)['$-@'](), inf]['$include?'](self.$step()))) { 11351 return 1 11352 } else if (($truthy([$rb_times((inf)['$-@'](), step_sign), nil]['$include?'](self.$begin())) || ($truthy([$rb_times(inf, step_sign), nil]['$include?'](self.$end()))))) { 11353 return inf; 11354 } else { 11355 11356 iter = $rb_minus(self.$end(), $rb_minus(self.$end(), self.$begin())['$%'](self.$step())); 11357 if (!$truthy(self['$_lesser_than_end?'](iter))) { 11358 iter = $rb_minus(iter, self.$step()) 11359 }; 11360 return $rb_plus($rb_divide($rb_minus(iter, self.$begin()), self.$step()).$abs().$to_i(), 1); 11361 }; 11362 }); 11363 11364 $def(self, '$==', function $ArithmeticSequence_$eq_eq$4(other) { 11365 var self = this, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, $ret_or_4 = nil; 11366 11367 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = ($truthy(($ret_or_3 = ($truthy(($ret_or_4 = self.$class()['$=='](other.$class()))) ? (self.$begin()['$=='](other.$begin())) : ($ret_or_4)))) ? (self.$end()['$=='](other.$end())) : ($ret_or_3)))) ? (self.$step()['$=='](other.$step())) : ($ret_or_2))))) { 11368 return self['$exclude_end?']()['$=='](other['$exclude_end?']()) 11369 } else { 11370 return $ret_or_1 11371 } 11372 }); 11373 11374 $def(self, '$hash', function $$hash() { 11375 var self = this; 11376 11377 return [self.$begin(), self.$end(), self.$step(), self['$exclude_end?']()].$hash() 11378 }); 11379 11380 $def(self, '$inspect', function $$inspect() { 11381 var self = this, args = nil; 11382 11383 if ($truthy(self.receiver_num)) { 11384 11385 args = ($truthy(self.step_arg2) ? ("(" + (self.topfx) + (self.step_arg1.$inspect()) + ", " + (self.bypfx) + (self.step_arg2.$inspect()) + ")") : ($truthy(self.step_arg1) ? ("(" + (self.topfx) + (self.step_arg1.$inspect()) + ")") : nil)); 11386 return "(" + (self.receiver_num.$inspect()) + "." + (self.creation_method) + (args) + ")"; 11387 } else { 11388 11389 args = ($truthy(self.skipped_arg) ? (nil) : ("(" + (self.step) + ")")); 11390 return "((" + (self.range.$inspect()) + ")." + (self.creation_method) + (args) + ")"; 11391 } 11392 }); 11393 $alias(self, "===", "=="); 11394 return $alias(self, "eql?", "=="); 11395 })(self, self, $nesting) 11396 })('::', null, $nesting) 11397}; 11398 11399Opal.modules["corelib/enumerator/chain"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11400 var $deny_frozen_access = Opal.deny_frozen_access, $klass = Opal.klass, $slice = Opal.slice, $def = Opal.def, $send = Opal.send, $to_a = Opal.to_a, $truthy = Opal.truthy, $rb_plus = Opal.rb_plus, $thrower = Opal.thrower, nil = Opal.nil, $$$ = Opal.$$$; 11401 11402 Opal.add_stubs('to_enum,size,each,<<,to_proc,include?,+,reverse_each,respond_to?,rewind,inspect'); 11403 return (function($base, $super) { 11404 var self = $klass($base, $super, 'Enumerator'); 11405 11406 11407 return (function($base, $super) { 11408 var self = $klass($base, $super, 'Chain'); 11409 11410 var $proto = self.$$prototype; 11411 11412 $proto.enums = $proto.iterated = nil; 11413 11414 11415 $def(self, '$initialize', function $$initialize($a) { 11416 var $post_args, enums, self = this; 11417 11418 11419 $post_args = $slice(arguments); 11420 enums = $post_args; 11421 $deny_frozen_access(self); 11422 self.enums = enums; 11423 self.iterated = []; 11424 return (self.object = self); 11425 }, -1); 11426 11427 $def(self, '$each', function $$each($a) { 11428 var block = $$each.$$p || nil, $post_args, args, self = this; 11429 11430 $$each.$$p = null; 11431 11432 ; 11433 $post_args = $slice(arguments); 11434 args = $post_args; 11435 if (!(block !== nil)) { 11436 return $send(self, 'to_enum', ["each"].concat($to_a(args)), function $$1(){var self = $$1.$$s == null ? this : $$1.$$s; 11437 11438 return self.$size()}, {$$s: self}) 11439 }; 11440 $send(self.enums, 'each', [], function $$2(enum$){var self = $$2.$$s == null ? this : $$2.$$s; 11441 if (self.iterated == null) self.iterated = nil; 11442 11443 11444 if (enum$ == null) enum$ = nil; 11445 self.iterated['$<<'](enum$); 11446 return $send(enum$, 'each', $to_a(args), block.$to_proc());}, {$$s: self}); 11447 return self; 11448 }, -1); 11449 11450 $def(self, '$size', function $$size($a) {try { var $t_return = $thrower('return'); 11451 var $post_args, args, self = this, accum = nil; 11452 11453 11454 $post_args = $slice(arguments); 11455 args = $post_args; 11456 accum = 0; 11457 $send(self.enums, 'each', [], function $$3(enum$){var size = nil; 11458 11459 11460 if (enum$ == null) enum$ = nil; 11461 size = $send(enum$, 'size', $to_a(args)); 11462 if ($truthy([nil, $$$($$$('Float'), 'INFINITY')]['$include?'](size))) { 11463 $t_return.$throw(size) 11464 }; 11465 return (accum = $rb_plus(accum, size));}, {$$ret: $t_return}); 11466 return accum;} catch($e) { 11467 if ($e === $t_return) return $e.$v; 11468 throw $e; 11469 } 11470 }, -1); 11471 11472 $def(self, '$rewind', function $$rewind() { 11473 var self = this; 11474 11475 11476 $send(self.iterated, 'reverse_each', [], function $$4(enum$){ 11477 11478 if (enum$ == null) enum$ = nil; 11479 if ($truthy(enum$['$respond_to?']("rewind"))) { 11480 return enum$.$rewind() 11481 } else { 11482 return nil 11483 };}); 11484 self.iterated = []; 11485 return self; 11486 }); 11487 return $def(self, '$inspect', function $$inspect() { 11488 var self = this; 11489 11490 return "#<Enumerator::Chain: " + (self.enums.$inspect()) + ">" 11491 }); 11492 })(self, self) 11493 })('::', null) 11494}; 11495 11496Opal.modules["corelib/enumerator/generator"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11497 var $deny_frozen_access = Opal.deny_frozen_access, $klass = Opal.klass, $truthy = Opal.truthy, $Kernel = Opal.Kernel, $def = Opal.def, $slice = Opal.slice, $send = Opal.send, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 11498 11499 Opal.add_stubs('include,raise,new,to_proc'); 11500 return (function($base, $super, $parent_nesting) { 11501 var self = $klass($base, $super, 'Enumerator'); 11502 11503 var $nesting = [self].concat($parent_nesting); 11504 11505 return (function($base, $super, $parent_nesting) { 11506 var self = $klass($base, $super, 'Generator'); 11507 11508 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11509 11510 $proto.block = nil; 11511 11512 self.$include($$$('Enumerable')); 11513 11514 $def(self, '$initialize', function $$initialize() { 11515 var block = $$initialize.$$p || nil, self = this; 11516 11517 $$initialize.$$p = null; 11518 11519 ; 11520 $deny_frozen_access(self); 11521 if (!$truthy(block)) { 11522 $Kernel.$raise($$$('LocalJumpError'), "no block given") 11523 }; 11524 return (self.block = block); 11525 }); 11526 return $def(self, '$each', function $$each($a) { 11527 var block = $$each.$$p || nil, $post_args, args, self = this, yielder = nil; 11528 11529 $$each.$$p = null; 11530 11531 ; 11532 $post_args = $slice(arguments); 11533 args = $post_args; 11534 yielder = $send($$('Yielder'), 'new', [], block.$to_proc()); 11535 11536 try { 11537 args.unshift(yielder); 11538 11539 Opal.yieldX(self.block, args); 11540 } 11541 catch (e) { 11542 if (e && e.$thrower_type == "breaker") { 11543 return e.$v; 11544 } 11545 else { 11546 throw e; 11547 } 11548 } 11549 ; 11550 return self; 11551 }, -1); 11552 })($nesting[0], null, $nesting) 11553 })($nesting[0], null, $nesting) 11554}; 11555 11556Opal.modules["corelib/enumerator/lazy"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11557 var $truthy = Opal.truthy, $coerce_to = Opal.coerce_to, $yield1 = Opal.yield1, $yieldX = Opal.yieldX, $deny_frozen_access = Opal.deny_frozen_access, $klass = Opal.klass, $slice = Opal.slice, $send2 = Opal.send2, $find_super = Opal.find_super, $to_a = Opal.to_a, $defs = Opal.defs, $Kernel = Opal.Kernel, $send = Opal.send, $def = Opal.def, $return_self = Opal.return_self, $Opal = Opal.Opal, $rb_lt = Opal.rb_lt, $eqeqeq = Opal.eqeqeq, $rb_plus = Opal.rb_plus, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 11558 11559 Opal.add_stubs('raise,each,new,enumerator_size,yield,respond_to?,try_convert,<,===,+,for,class,to_proc,destructure,inspect,to_a,find_all,collect_concat,collect,enum_for'); 11560 return (function($base, $super, $parent_nesting) { 11561 var self = $klass($base, $super, 'Enumerator'); 11562 11563 var $nesting = [self].concat($parent_nesting); 11564 11565 return (function($base, $super, $parent_nesting) { 11566 var self = $klass($base, $super, 'Lazy'); 11567 11568 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11569 11570 $proto.enumerator = nil; 11571 11572 $klass(self, $$$('Exception'), 'StopLazyError'); 11573 $defs(self, '$for', function $Lazy_for$1(object, $a) { 11574 var $post_args, $fwd_rest, $yield = $Lazy_for$1.$$p || nil, self = this, lazy = nil; 11575 11576 $Lazy_for$1.$$p = null; 11577 11578 $post_args = $slice(arguments, 1); 11579 $fwd_rest = $post_args; 11580 lazy = $send2(self, $find_super(self, 'for', $Lazy_for$1, false, true), 'for', [object].concat($to_a($fwd_rest)), $yield); 11581 lazy.enumerator = object; 11582 return lazy; 11583 }, -2); 11584 11585 $def(self, '$initialize', function $$initialize(object, size) { 11586 var block = $$initialize.$$p || nil, self = this; 11587 11588 $$initialize.$$p = null; 11589 11590 ; 11591 if (size == null) size = nil; 11592 $deny_frozen_access(self); 11593 if (!(block !== nil)) { 11594 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy new without a block") 11595 }; 11596 self.enumerator = object; 11597 return $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [size], function $$2(yielder, $a){var $post_args, each_args; 11598 11599 11600 if (yielder == null) yielder = nil; 11601 $post_args = $slice(arguments, 1); 11602 each_args = $post_args; 11603 try { 11604 return $send(object, 'each', $to_a(each_args), function $$3($b){var $post_args, args; 11605 11606 11607 $post_args = $slice(arguments); 11608 args = $post_args; 11609 11610 args.unshift(yielder); 11611 11612 $yieldX(block, args); 11613 ;}, -1) 11614 } catch ($err) { 11615 if (Opal.rescue($err, [$$('StopLazyError')])) { 11616 try { 11617 return nil 11618 } finally { Opal.pop_exception(); } 11619 } else { throw $err; } 11620 };}, -2); 11621 }, -2); 11622 11623 $def(self, '$lazy', $return_self); 11624 11625 $def(self, '$collect', function $$collect() { 11626 var block = $$collect.$$p || nil, self = this; 11627 11628 $$collect.$$p = null; 11629 11630 ; 11631 if (!$truthy(block)) { 11632 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy map without a block") 11633 }; 11634 return $send($$('Lazy'), 'new', [self, self.$enumerator_size()], function $$4(enum$, $a){var $post_args, args; 11635 11636 11637 if (enum$ == null) enum$ = nil; 11638 $post_args = $slice(arguments, 1); 11639 args = $post_args; 11640 11641 var value = $yieldX(block, args); 11642 11643 enum$.$yield(value); 11644 ;}, -2); 11645 }); 11646 11647 $def(self, '$collect_concat', function $$collect_concat() { 11648 var block = $$collect_concat.$$p || nil, self = this; 11649 11650 $$collect_concat.$$p = null; 11651 11652 ; 11653 if (!$truthy(block)) { 11654 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy map without a block") 11655 }; 11656 return $send($$('Lazy'), 'new', [self, nil], function $$5(enum$, $a){var $post_args, args; 11657 11658 11659 if (enum$ == null) enum$ = nil; 11660 $post_args = $slice(arguments, 1); 11661 args = $post_args; 11662 11663 var value = $yieldX(block, args); 11664 11665 if ((value)['$respond_to?']("force") && (value)['$respond_to?']("each")) { 11666 $send((value), 'each', [], function $$6(v){ 11667 11668 if (v == null) v = nil; 11669 return enum$.$yield(v);}) 11670 } 11671 else { 11672 var array = $Opal.$try_convert(value, $$$('Array'), "to_ary"); 11673 11674 if (array === nil) { 11675 enum$.$yield(value); 11676 } 11677 else { 11678 $send((value), 'each', [], function $$7(v){ 11679 11680 if (v == null) v = nil; 11681 return enum$.$yield(v);}); 11682 } 11683 } 11684 ;}, -2); 11685 }); 11686 11687 $def(self, '$drop', function $$drop(n) { 11688 var self = this, current_size = nil, set_size = nil, dropped = nil; 11689 11690 11691 n = $coerce_to(n, $$$('Integer'), 'to_int'); 11692 if ($truthy($rb_lt(n, 0))) { 11693 $Kernel.$raise($$$('ArgumentError'), "attempt to drop negative size") 11694 }; 11695 current_size = self.$enumerator_size(); 11696 set_size = ($eqeqeq($$$('Integer'), current_size) ? (($truthy($rb_lt(n, current_size)) ? (n) : (current_size))) : (current_size)); 11697 dropped = 0; 11698 return $send($$('Lazy'), 'new', [self, set_size], function $$8(enum$, $a){var $post_args, args; 11699 11700 11701 if (enum$ == null) enum$ = nil; 11702 $post_args = $slice(arguments, 1); 11703 args = $post_args; 11704 if ($truthy($rb_lt(dropped, n))) { 11705 return (dropped = $rb_plus(dropped, 1)) 11706 } else { 11707 return $send(enum$, 'yield', $to_a(args)) 11708 };}, -2); 11709 }); 11710 11711 $def(self, '$drop_while', function $$drop_while() { 11712 var block = $$drop_while.$$p || nil, self = this, succeeding = nil; 11713 11714 $$drop_while.$$p = null; 11715 11716 ; 11717 if (!$truthy(block)) { 11718 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy drop_while without a block") 11719 }; 11720 succeeding = true; 11721 return $send($$('Lazy'), 'new', [self, nil], function $$9(enum$, $a){var $post_args, args; 11722 11723 11724 if (enum$ == null) enum$ = nil; 11725 $post_args = $slice(arguments, 1); 11726 args = $post_args; 11727 if ($truthy(succeeding)) { 11728 11729 var value = $yieldX(block, args); 11730 11731 if (!$truthy(value)) { 11732 succeeding = false; 11733 11734 $send(enum$, 'yield', $to_a(args)); 11735 } 11736 11737 } else { 11738 return $send(enum$, 'yield', $to_a(args)) 11739 };}, -2); 11740 }); 11741 11742 $def(self, '$enum_for', function $$enum_for($a, $b) { 11743 var block = $$enum_for.$$p || nil, $post_args, method, args, self = this; 11744 11745 $$enum_for.$$p = null; 11746 11747 ; 11748 $post_args = $slice(arguments); 11749 11750 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 11751 args = $post_args; 11752 return $send(self.$class(), 'for', [self, method].concat($to_a(args)), block.$to_proc()); 11753 }, -1); 11754 11755 $def(self, '$find_all', function $$find_all() { 11756 var block = $$find_all.$$p || nil, self = this; 11757 11758 $$find_all.$$p = null; 11759 11760 ; 11761 if (!$truthy(block)) { 11762 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy select without a block") 11763 }; 11764 return $send($$('Lazy'), 'new', [self, nil], function $$10(enum$, $a){var $post_args, args; 11765 11766 11767 if (enum$ == null) enum$ = nil; 11768 $post_args = $slice(arguments, 1); 11769 args = $post_args; 11770 11771 var value = $yieldX(block, args); 11772 11773 if ($truthy(value)) { 11774 $send(enum$, 'yield', $to_a(args)); 11775 } 11776 ;}, -2); 11777 }); 11778 11779 $def(self, '$grep', function $$grep(pattern) { 11780 var block = $$grep.$$p || nil, self = this; 11781 11782 $$grep.$$p = null; 11783 11784 ; 11785 if ($truthy(block)) { 11786 return $send($$('Lazy'), 'new', [self, nil], function $$11(enum$, $a){var $post_args, args; 11787 11788 11789 if (enum$ == null) enum$ = nil; 11790 $post_args = $slice(arguments, 1); 11791 args = $post_args; 11792 11793 var param = $Opal.$destructure(args), 11794 value = pattern['$==='](param); 11795 11796 if ($truthy(value)) { 11797 value = $yield1(block, param); 11798 11799 enum$.$yield($yield1(block, param)); 11800 } 11801 ;}, -2) 11802 } else { 11803 return $send($$('Lazy'), 'new', [self, nil], function $$12(enum$, $a){var $post_args, args; 11804 11805 11806 if (enum$ == null) enum$ = nil; 11807 $post_args = $slice(arguments, 1); 11808 args = $post_args; 11809 11810 var param = $Opal.$destructure(args), 11811 value = pattern['$==='](param); 11812 11813 if ($truthy(value)) { 11814 enum$.$yield(param); 11815 } 11816 ;}, -2) 11817 }; 11818 }); 11819 11820 $def(self, '$reject', function $$reject() { 11821 var block = $$reject.$$p || nil, self = this; 11822 11823 $$reject.$$p = null; 11824 11825 ; 11826 if (!$truthy(block)) { 11827 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy reject without a block") 11828 }; 11829 return $send($$('Lazy'), 'new', [self, nil], function $$13(enum$, $a){var $post_args, args; 11830 11831 11832 if (enum$ == null) enum$ = nil; 11833 $post_args = $slice(arguments, 1); 11834 args = $post_args; 11835 11836 var value = $yieldX(block, args); 11837 11838 if (!$truthy(value)) { 11839 $send(enum$, 'yield', $to_a(args)); 11840 } 11841 ;}, -2); 11842 }); 11843 11844 $def(self, '$take', function $$take(n) { 11845 var self = this, current_size = nil, set_size = nil, taken = nil; 11846 11847 11848 n = $coerce_to(n, $$$('Integer'), 'to_int'); 11849 if ($truthy($rb_lt(n, 0))) { 11850 $Kernel.$raise($$$('ArgumentError'), "attempt to take negative size") 11851 }; 11852 current_size = self.$enumerator_size(); 11853 set_size = ($eqeqeq($$$('Integer'), current_size) ? (($truthy($rb_lt(n, current_size)) ? (n) : (current_size))) : (current_size)); 11854 taken = 0; 11855 return $send($$('Lazy'), 'new', [self, set_size], function $$14(enum$, $a){var $post_args, args; 11856 11857 11858 if (enum$ == null) enum$ = nil; 11859 $post_args = $slice(arguments, 1); 11860 args = $post_args; 11861 if ($truthy($rb_lt(taken, n))) { 11862 11863 $send(enum$, 'yield', $to_a(args)); 11864 return (taken = $rb_plus(taken, 1)); 11865 } else { 11866 return $Kernel.$raise($$('StopLazyError')) 11867 };}, -2); 11868 }); 11869 11870 $def(self, '$take_while', function $$take_while() { 11871 var block = $$take_while.$$p || nil, self = this; 11872 11873 $$take_while.$$p = null; 11874 11875 ; 11876 if (!$truthy(block)) { 11877 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy take_while without a block") 11878 }; 11879 return $send($$('Lazy'), 'new', [self, nil], function $$15(enum$, $a){var $post_args, args; 11880 11881 11882 if (enum$ == null) enum$ = nil; 11883 $post_args = $slice(arguments, 1); 11884 args = $post_args; 11885 11886 var value = $yieldX(block, args); 11887 11888 if ($truthy(value)) { 11889 $send(enum$, 'yield', $to_a(args)); 11890 } 11891 else { 11892 $Kernel.$raise($$('StopLazyError')); 11893 } 11894 ;}, -2); 11895 }); 11896 11897 $def(self, '$inspect', function $$inspect() { 11898 var self = this; 11899 11900 return "#<" + (self.$class()) + ": " + (self.enumerator.$inspect()) + ">" 11901 }); 11902 $alias(self, "force", "to_a"); 11903 $alias(self, "filter", "find_all"); 11904 $alias(self, "flat_map", "collect_concat"); 11905 $alias(self, "map", "collect"); 11906 $alias(self, "select", "find_all"); 11907 return $alias(self, "to_enum", "enum_for"); 11908 })(self, self, $nesting) 11909 })('::', null, $nesting) 11910}; 11911 11912Opal.modules["corelib/enumerator/yielder"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11913 var $klass = Opal.klass, $def = Opal.def, $slice = Opal.slice, $send = Opal.send, $to_a = Opal.to_a, $nesting = [], nil = Opal.nil; 11914 11915 Opal.add_stubs('yield,proc'); 11916 return (function($base, $super, $parent_nesting) { 11917 var self = $klass($base, $super, 'Enumerator'); 11918 11919 var $nesting = [self].concat($parent_nesting); 11920 11921 return (function($base, $super) { 11922 var self = $klass($base, $super, 'Yielder'); 11923 11924 var $proto = self.$$prototype; 11925 11926 $proto.block = nil; 11927 11928 11929 $def(self, '$initialize', function $$initialize() { 11930 var block = $$initialize.$$p || nil, self = this; 11931 11932 $$initialize.$$p = null; 11933 11934 ; 11935 self.block = block; 11936 return self; 11937 }); 11938 11939 $def(self, '$yield', function $Yielder_yield$1($a) { 11940 var $post_args, values, self = this; 11941 11942 11943 $post_args = $slice(arguments); 11944 values = $post_args; 11945 11946 var value = Opal.yieldX(self.block, values); 11947 11948 if (value && value.$thrower_type == "break") { 11949 throw value; 11950 } 11951 11952 return value; 11953 ; 11954 }, -1); 11955 11956 $def(self, '$<<', function $Yielder_$lt$lt$2(value) { 11957 var self = this; 11958 11959 11960 self.$yield(value); 11961 return self; 11962 }); 11963 return $def(self, '$to_proc', function $$to_proc() { 11964 var self = this; 11965 11966 return $send(self, 'proc', [], function $$3($a){var $post_args, values, self = $$3.$$s == null ? this : $$3.$$s; 11967 11968 11969 $post_args = $slice(arguments); 11970 values = $post_args; 11971 return $send(self, 'yield', $to_a(values));}, {$$arity: -1, $$s: self}) 11972 }); 11973 })($nesting[0], null) 11974 })($nesting[0], null, $nesting) 11975}; 11976 11977Opal.modules["corelib/enumerator"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11978 var $slice = Opal.slice, $coerce_to = Opal.coerce_to, $deny_frozen_access = Opal.deny_frozen_access, $klass = Opal.klass, $defs = Opal.defs, $truthy = Opal.truthy, $send = Opal.send, $not = Opal.not, $def = Opal.def, $rb_plus = Opal.rb_plus, $to_a = Opal.to_a, $Opal = Opal.Opal, $send2 = Opal.send2, $find_super = Opal.find_super, $rb_ge = Opal.rb_ge, $Kernel = Opal.Kernel, $rb_le = Opal.rb_le, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 11979 11980 Opal.add_stubs('require,include,allocate,new,to_proc,!,respond_to?,empty?,nil?,+,class,__send__,call,enum_for,size,destructure,map,>=,length,raise,[],peek_values,<=,next_values,inspect,any?,each_with_object,autoload'); 11981 11982 self.$require("corelib/enumerable"); 11983 return (function($base, $super, $parent_nesting) { 11984 var self = $klass($base, $super, 'Enumerator'); 11985 11986 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11987 11988 $proto.size = $proto.args = $proto.object = $proto.method = $proto.values = $proto.cursor = nil; 11989 11990 self.$include($$$('Enumerable')); 11991 self.$$prototype.$$is_enumerator = true; 11992 $defs(self, '$for', function $Enumerator_for$1(object, $a, $b) { 11993 var block = $Enumerator_for$1.$$p || nil, $post_args, method, args, self = this; 11994 11995 $Enumerator_for$1.$$p = null; 11996 11997 ; 11998 $post_args = $slice(arguments, 1); 11999 12000 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 12001 args = $post_args; 12002 12003 var obj = self.$allocate(); 12004 12005 obj.object = object; 12006 obj.size = block; 12007 obj.method = method; 12008 obj.args = args; 12009 obj.cursor = 0; 12010 12011 return obj; 12012 ; 12013 }, -2); 12014 12015 $def(self, '$initialize', function $$initialize($a) { 12016 var block = $$initialize.$$p || nil, $post_args, $fwd_rest, self = this; 12017 12018 $$initialize.$$p = null; 12019 12020 ; 12021 $post_args = $slice(arguments); 12022 $fwd_rest = $post_args; 12023 $deny_frozen_access(self); 12024 self.cursor = 0; 12025 if ($truthy(block)) { 12026 12027 self.object = $send($$('Generator'), 'new', [], block.$to_proc()); 12028 self.method = "each"; 12029 self.args = []; 12030 self.size = arguments[0] || nil; 12031 if (($truthy(self.size) && ($not(self.size['$respond_to?']("call"))))) { 12032 return (self.size = $coerce_to(self.size, $$$('Integer'), 'to_int')) 12033 } else { 12034 return nil 12035 }; 12036 } else { 12037 12038 self.object = arguments[0]; 12039 self.method = arguments[1] || "each"; 12040 self.args = $slice(arguments, 2); 12041 return (self.size = nil); 12042 }; 12043 }, -1); 12044 12045 $def(self, '$each', function $$each($a) { 12046 var block = $$each.$$p || nil, $post_args, args, self = this; 12047 12048 $$each.$$p = null; 12049 12050 ; 12051 $post_args = $slice(arguments); 12052 args = $post_args; 12053 if (($truthy(block['$nil?']()) && ($truthy(args['$empty?']())))) { 12054 return self 12055 }; 12056 args = $rb_plus(self.args, args); 12057 if ($truthy(block['$nil?']())) { 12058 return $send(self.$class(), 'new', [self.object, self.method].concat($to_a(args))) 12059 }; 12060 return $send(self.object, '__send__', [self.method].concat($to_a(args)), block.$to_proc()); 12061 }, -1); 12062 12063 $def(self, '$size', function $$size() { 12064 var self = this; 12065 12066 if ($truthy(self.size['$respond_to?']("call"))) { 12067 return $send(self.size, 'call', $to_a(self.args)) 12068 } else { 12069 return self.size 12070 } 12071 }); 12072 12073 $def(self, '$with_index', function $$with_index(offset) { 12074 var block = $$with_index.$$p || nil, self = this; 12075 12076 $$with_index.$$p = null; 12077 12078 ; 12079 if (offset == null) offset = 0; 12080 offset = ($truthy(offset) ? ($coerce_to(offset, $$$('Integer'), 'to_int')) : (0)); 12081 if (!$truthy(block)) { 12082 return $send(self, 'enum_for', ["with_index", offset], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 12083 12084 return self.$size()}, {$$s: self}) 12085 }; 12086 12087 var result, index = offset; 12088 12089 self.$each.$$p = function() { 12090 var param = $Opal.$destructure(arguments), 12091 value = block(param, index); 12092 12093 index++; 12094 12095 return value; 12096 } 12097 12098 return self.$each(); 12099 ; 12100 }, -1); 12101 12102 $def(self, '$each_with_index', function $$each_with_index() { 12103 var block = $$each_with_index.$$p || nil, self = this; 12104 12105 $$each_with_index.$$p = null; 12106 12107 ; 12108 if (!(block !== nil)) { 12109 return $send(self, 'enum_for', ["each_with_index"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 12110 12111 return self.$size()}, {$$s: self}) 12112 }; 12113 $send2(self, $find_super(self, 'each_with_index', $$each_with_index, false, true), 'each_with_index', [], block); 12114 return self.object; 12115 }); 12116 12117 $def(self, '$rewind', function $$rewind() { 12118 var self = this; 12119 12120 12121 self.cursor = 0; 12122 return self; 12123 }); 12124 12125 $def(self, '$peek_values', function $$peek_values() { 12126 var self = this, $ret_or_1 = nil; 12127 12128 12129 self.values = ($truthy(($ret_or_1 = self.values)) ? ($ret_or_1) : ($send(self, 'map', [], function $$4($a){var $post_args, i; 12130 12131 12132 $post_args = $slice(arguments); 12133 i = $post_args; 12134 return i;}, -1))); 12135 if ($truthy($rb_ge(self.cursor, self.values.$length()))) { 12136 $Kernel.$raise($$$('StopIteration'), "iteration reached an end") 12137 }; 12138 return self.values['$[]'](self.cursor); 12139 }); 12140 12141 $def(self, '$peek', function $$peek() { 12142 var self = this, values = nil; 12143 12144 12145 values = self.$peek_values(); 12146 if ($truthy($rb_le(values.$length(), 1))) { 12147 return values['$[]'](0) 12148 } else { 12149 return values 12150 }; 12151 }); 12152 12153 $def(self, '$next_values', function $$next_values() { 12154 var self = this, out = nil; 12155 12156 12157 out = self.$peek_values(); 12158 self.cursor = $rb_plus(self.cursor, 1); 12159 return out; 12160 }); 12161 12162 $def(self, '$next', function $$next() { 12163 var self = this, values = nil; 12164 12165 12166 values = self.$next_values(); 12167 if ($truthy($rb_le(values.$length(), 1))) { 12168 return values['$[]'](0) 12169 } else { 12170 return values 12171 }; 12172 }); 12173 12174 $def(self, '$feed', function $$feed(arg) { 12175 var self = this; 12176 12177 return self.$raise($$('NotImplementedError'), "Opal doesn't support Enumerator#feed") 12178 }); 12179 12180 $def(self, '$+', function $Enumerator_$plus$5(other) { 12181 var self = this; 12182 12183 return $$$($$$('Enumerator'), 'Chain').$new(self, other) 12184 }); 12185 12186 $def(self, '$inspect', function $$inspect() { 12187 var self = this, result = nil; 12188 12189 12190 result = "#<" + (self.$class()) + ": " + (self.object.$inspect()) + ":" + (self.method); 12191 if ($truthy(self.args['$any?']())) { 12192 result = $rb_plus(result, "(" + (self.args.$inspect()['$[]']($$$('Range').$new(1, -2))) + ")") 12193 }; 12194 return $rb_plus(result, ">"); 12195 }); 12196 $alias(self, "with_object", "each_with_object"); 12197 self.$autoload("ArithmeticSequence", "corelib/enumerator/arithmetic_sequence"); 12198 self.$autoload("Chain", "corelib/enumerator/chain"); 12199 self.$autoload("Generator", "corelib/enumerator/generator"); 12200 self.$autoload("Lazy", "corelib/enumerator/lazy"); 12201 return self.$autoload("Yielder", "corelib/enumerator/yielder"); 12202 })('::', null, $nesting); 12203}; 12204 12205Opal.modules["corelib/numeric"] = function(Opal) {/* Generated by Opal 1.7.3 */ 12206 var $klass = Opal.klass, $truthy = Opal.truthy, $Kernel = Opal.Kernel, $def = Opal.def, $to_ary = Opal.to_ary, $return_self = Opal.return_self, $rb_minus = Opal.rb_minus, $rb_times = Opal.rb_times, $rb_lt = Opal.rb_lt, $eqeq = Opal.eqeq, $rb_divide = Opal.rb_divide, $return_val = Opal.return_val, $Opal = Opal.Opal, $slice = Opal.slice, $extract_kwargs = Opal.extract_kwargs, $ensure_kwargs = Opal.ensure_kwargs, $not = Opal.not, $send = Opal.send, $rb_ge = Opal.rb_ge, $rb_le = Opal.rb_le, $rb_plus = Opal.rb_plus, $rb_gt = Opal.rb_gt, $alias = Opal.alias, self = Opal.top, nil = Opal.nil, $$$ = Opal.$$$; 12207 12208 Opal.add_stubs('require,include,instance_of?,class,Float,respond_to?,coerce,__send__,raise,equal?,-,*,div,<,-@,ceil,to_f,denominator,to_r,==,floor,/,%,Complex,zero?,numerator,abs,arg,coerce_to!,round,<=>,compare,is_a?,!,new,enum_for,to_proc,negative?,>=,<=,+,to_i,truncate,>,angle,conj,imag,rect'); 12209 12210 self.$require("corelib/comparable"); 12211 return (function($base, $super) { 12212 var self = $klass($base, $super, 'Numeric'); 12213 12214 12215 12216 self.$include($$$('Comparable')); 12217 12218 $def(self, '$coerce', function $$coerce(other) { 12219 var self = this; 12220 12221 12222 if ($truthy(other['$instance_of?'](self.$class()))) { 12223 return [other, self] 12224 }; 12225 return [$Kernel.$Float(other), $Kernel.$Float(self)]; 12226 }); 12227 12228 $def(self, '$__coerced__', function $$__coerced__(method, other) { 12229 var $a, $b, self = this, a = nil, b = nil; 12230 12231 if ($truthy(other['$respond_to?']("coerce"))) { 12232 12233 $b = other.$coerce(self), $a = $to_ary($b), (a = ($a[0] == null ? nil : $a[0])), (b = ($a[1] == null ? nil : $a[1])), $b; 12234 return a.$__send__(method, b); 12235 } else 12236 switch (method) { 12237 case "+": 12238 case "-": 12239 case "*": 12240 case "/": 12241 case "%": 12242 case "&": 12243 case "|": 12244 case "^": 12245 case "**": 12246 return $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Numeric") 12247 case ">": 12248 case ">=": 12249 case "<": 12250 case "<=": 12251 case "<=>": 12252 return $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (other.$class()) + " failed") 12253 default: 12254 return nil 12255 } 12256 }); 12257 12258 $def(self, '$<=>', function $Numeric_$lt_eq_gt$1(other) { 12259 var self = this; 12260 12261 12262 if ($truthy(self['$equal?'](other))) { 12263 return 0 12264 }; 12265 return nil; 12266 }); 12267 12268 $def(self, '$+@', $return_self); 12269 12270 $def(self, '$-@', function $Numeric_$minus$$2() { 12271 var self = this; 12272 12273 return $rb_minus(0, self) 12274 }); 12275 12276 $def(self, '$%', function $Numeric_$percent$3(other) { 12277 var self = this; 12278 12279 return $rb_minus(self, $rb_times(other, self.$div(other))) 12280 }); 12281 12282 $def(self, '$abs', function $$abs() { 12283 var self = this; 12284 12285 if ($rb_lt(self, 0)) { 12286 return self['$-@']() 12287 } else { 12288 return self 12289 } 12290 }); 12291 12292 $def(self, '$abs2', function $$abs2() { 12293 var self = this; 12294 12295 return $rb_times(self, self) 12296 }); 12297 12298 $def(self, '$angle', function $$angle() { 12299 var self = this; 12300 12301 if ($rb_lt(self, 0)) { 12302 return $$$($$$('Math'), 'PI') 12303 } else { 12304 return 0 12305 } 12306 }); 12307 12308 $def(self, '$ceil', function $$ceil(ndigits) { 12309 var self = this; 12310 12311 12312 if (ndigits == null) ndigits = 0; 12313 return self.$to_f().$ceil(ndigits); 12314 }, -1); 12315 12316 $def(self, '$conj', $return_self); 12317 12318 $def(self, '$denominator', function $$denominator() { 12319 var self = this; 12320 12321 return self.$to_r().$denominator() 12322 }); 12323 12324 $def(self, '$div', function $$div(other) { 12325 var self = this; 12326 12327 12328 if ($eqeq(other, 0)) { 12329 $Kernel.$raise($$$('ZeroDivisionError'), "divided by o") 12330 }; 12331 return $rb_divide(self, other).$floor(); 12332 }); 12333 12334 $def(self, '$divmod', function $$divmod(other) { 12335 var self = this; 12336 12337 return [self.$div(other), self['$%'](other)] 12338 }); 12339 12340 $def(self, '$fdiv', function $$fdiv(other) { 12341 var self = this; 12342 12343 return $rb_divide(self.$to_f(), other) 12344 }); 12345 12346 $def(self, '$floor', function $$floor(ndigits) { 12347 var self = this; 12348 12349 12350 if (ndigits == null) ndigits = 0; 12351 return self.$to_f().$floor(ndigits); 12352 }, -1); 12353 12354 $def(self, '$i', function $$i() { 12355 var self = this; 12356 12357 return $Kernel.$Complex(0, self) 12358 }); 12359 12360 $def(self, '$imag', $return_val(0)); 12361 12362 $def(self, '$integer?', $return_val(false)); 12363 12364 $def(self, '$nonzero?', function $Numeric_nonzero$ques$4() { 12365 var self = this; 12366 12367 if ($truthy(self['$zero?']())) { 12368 return nil 12369 } else { 12370 return self 12371 } 12372 }); 12373 12374 $def(self, '$numerator', function $$numerator() { 12375 var self = this; 12376 12377 return self.$to_r().$numerator() 12378 }); 12379 12380 $def(self, '$polar', function $$polar() { 12381 var self = this; 12382 12383 return [self.$abs(), self.$arg()] 12384 }); 12385 12386 $def(self, '$quo', function $$quo(other) { 12387 var self = this; 12388 12389 return $rb_divide($Opal['$coerce_to!'](self, $$$('Rational'), "to_r"), other) 12390 }); 12391 12392 $def(self, '$real', $return_self); 12393 12394 $def(self, '$real?', $return_val(true)); 12395 12396 $def(self, '$rect', function $$rect() { 12397 var self = this; 12398 12399 return [self, 0] 12400 }); 12401 12402 $def(self, '$round', function $$round(digits) { 12403 var self = this; 12404 12405 12406 ; 12407 return self.$to_f().$round(digits); 12408 }, -1); 12409 12410 $def(self, '$step', function $$step($a, $b, $c) { 12411 var block = $$step.$$p || nil, $post_args, $kwargs, limit, step, to, by, self = this, counter = nil; 12412 12413 $$step.$$p = null; 12414 12415 ; 12416 $post_args = $slice(arguments); 12417 $kwargs = $extract_kwargs($post_args); 12418 $kwargs = $ensure_kwargs($kwargs); 12419 12420 if ($post_args.length > 0) limit = $post_args.shift();; 12421 12422 if ($post_args.length > 0) step = $post_args.shift();; 12423 12424 to = $kwargs.$$smap["to"];; 12425 12426 by = $kwargs.$$smap["by"];; 12427 12428 if (limit !== undefined && to !== undefined) { 12429 $Kernel.$raise($$$('ArgumentError'), "to is given twice") 12430 } 12431 12432 if (step !== undefined && by !== undefined) { 12433 $Kernel.$raise($$$('ArgumentError'), "step is given twice") 12434 } 12435 12436 if (to !== undefined) { 12437 limit = to; 12438 } 12439 12440 if (by !== undefined) { 12441 step = by; 12442 } 12443 12444 if (limit === undefined) { 12445 limit = nil; 12446 } 12447 12448 function validateParameters() { 12449 if (step === nil) { 12450 $Kernel.$raise($$$('TypeError'), "step must be numeric") 12451 } 12452 12453 if (step != null && step['$=='](0)) { 12454 $Kernel.$raise($$$('ArgumentError'), "step can't be 0") 12455 } 12456 12457 if (step === nil || step == null) { 12458 step = 1; 12459 } 12460 12461 var sign = step['$<=>'](0); 12462 12463 if (sign === nil) { 12464 $Kernel.$raise($$$('ArgumentError'), "0 can't be coerced into " + (step.$class())) 12465 } 12466 12467 if (limit === nil || limit == null) { 12468 limit = sign > 0 ? $$$($$$('Float'), 'INFINITY') : $$$($$$('Float'), 'INFINITY')['$-@'](); 12469 } 12470 12471 $Opal.$compare(self, limit) 12472 } 12473 12474 function stepFloatSize() { 12475 if ((step > 0 && self > limit) || (step < 0 && self < limit)) { 12476 return 0; 12477 } else if (step === Infinity || step === -Infinity) { 12478 return 1; 12479 } else { 12480 var abs = Math.abs, floor = Math.floor, 12481 err = (abs(self) + abs(limit) + abs(limit - self)) / abs(step) * $$$($$$('Float'), 'EPSILON'); 12482 12483 if (err === Infinity || err === -Infinity) { 12484 return 0; 12485 } else { 12486 if (err > 0.5) { 12487 err = 0.5; 12488 } 12489 12490 return floor((limit - self) / step + err) + 1 12491 } 12492 } 12493 } 12494 12495 function stepSize() { 12496 validateParameters(); 12497 12498 if (step === 0) { 12499 return Infinity; 12500 } 12501 12502 if (step % 1 !== 0) { 12503 return stepFloatSize(); 12504 } else if ((step > 0 && self > limit) || (step < 0 && self < limit)) { 12505 return 0; 12506 } else { 12507 var ceil = Math.ceil, abs = Math.abs, 12508 lhs = abs(self - limit) + 1, 12509 rhs = abs(step); 12510 12511 return ceil(lhs / rhs); 12512 } 12513 } 12514 12515 ; 12516 if (!(block !== nil)) { 12517 if ((($not(limit) || ($truthy(limit['$is_a?']($$$('Numeric'))))) && (($not(step) || ($truthy(step['$is_a?']($$$('Numeric')))))))) { 12518 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new([limit, step, ($truthy(to) ? ("to: ") : nil), ($truthy(by) ? ("by: ") : nil)], self) 12519 } else { 12520 return $send(self, 'enum_for', ["step", limit, step], (stepSize).$to_proc()) 12521 } 12522 }; 12523 12524 validateParameters(); 12525 12526 var isDesc = step['$negative?'](), 12527 isInf = step['$=='](0) || 12528 (limit === Infinity && !isDesc) || 12529 (limit === -Infinity && isDesc); 12530 12531 if (self.$$is_number && step.$$is_number && limit.$$is_number) { 12532 if (self % 1 === 0 && (isInf || limit % 1 === 0) && step % 1 === 0) { 12533 var value = self; 12534 12535 if (isInf) { 12536 for (;; value += step) { 12537 block(value); 12538 } 12539 } else if (isDesc) { 12540 for (; value >= limit; value += step) { 12541 block(value); 12542 } 12543 } else { 12544 for (; value <= limit; value += step) { 12545 block(value); 12546 } 12547 } 12548 12549 return self; 12550 } else { 12551 var begin = self.$to_f().valueOf(); 12552 step = step.$to_f().valueOf(); 12553 limit = limit.$to_f().valueOf(); 12554 12555 var n = stepFloatSize(); 12556 12557 if (!isFinite(step)) { 12558 if (n !== 0) block(begin); 12559 } else if (step === 0) { 12560 while (true) { 12561 block(begin); 12562 } 12563 } else { 12564 for (var i = 0; i < n; i++) { 12565 var d = i * step + self; 12566 if (step >= 0 ? limit < d : limit > d) { 12567 d = limit; 12568 } 12569 block(d); 12570 } 12571 } 12572 12573 return self; 12574 } 12575 } 12576 ; 12577 counter = self; 12578 while ($truthy(isDesc ? $rb_ge(counter, limit) : $rb_le(counter, limit))) { 12579 12580 Opal.yield1(block, counter); 12581 counter = $rb_plus(counter, step); 12582 }; 12583 }, -1); 12584 12585 $def(self, '$to_c', function $$to_c() { 12586 var self = this; 12587 12588 return $Kernel.$Complex(self, 0) 12589 }); 12590 12591 $def(self, '$to_int', function $$to_int() { 12592 var self = this; 12593 12594 return self.$to_i() 12595 }); 12596 12597 $def(self, '$truncate', function $$truncate(ndigits) { 12598 var self = this; 12599 12600 12601 if (ndigits == null) ndigits = 0; 12602 return self.$to_f().$truncate(ndigits); 12603 }, -1); 12604 12605 $def(self, '$zero?', function $Numeric_zero$ques$5() { 12606 var self = this; 12607 12608 return self['$=='](0) 12609 }); 12610 12611 $def(self, '$positive?', function $Numeric_positive$ques$6() { 12612 var self = this; 12613 12614 return $rb_gt(self, 0) 12615 }); 12616 12617 $def(self, '$negative?', function $Numeric_negative$ques$7() { 12618 var self = this; 12619 12620 return $rb_lt(self, 0) 12621 }); 12622 12623 $def(self, '$dup', $return_self); 12624 12625 $def(self, '$clone', function $$clone($kwargs) { 12626 var freeze, self = this; 12627 12628 12629 $kwargs = $ensure_kwargs($kwargs); 12630 12631 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = true; 12632 return self; 12633 }, -1); 12634 12635 $def(self, '$finite?', $return_val(true)); 12636 12637 $def(self, '$infinite?', $return_val(nil)); 12638 $alias(self, "arg", "angle"); 12639 $alias(self, "conjugate", "conj"); 12640 $alias(self, "imaginary", "imag"); 12641 $alias(self, "magnitude", "abs"); 12642 $alias(self, "modulo", "%"); 12643 $alias(self, "phase", "arg"); 12644 return $alias(self, "rectangular", "rect"); 12645 })('::', null); 12646}; 12647 12648Opal.modules["corelib/array"] = function(Opal) {/* Generated by Opal 1.7.3 */ 12649 var $truthy = Opal.truthy, $falsy = Opal.falsy, $hash_ids = Opal.hash_ids, $yield1 = Opal.yield1, $hash_get = Opal.hash_get, $hash_put = Opal.hash_put, $hash_delete = Opal.hash_delete, $coerce_to = Opal.coerce_to, $respond_to = Opal.respond_to, $deny_frozen_access = Opal.deny_frozen_access, $freeze = Opal.freeze, $klass = Opal.klass, $slice = Opal.slice, $defs = Opal.defs, $Kernel = Opal.Kernel, $def = Opal.def, $Opal = Opal.Opal, $eqeqeq = Opal.eqeqeq, $hash2 = Opal.hash2, $send2 = Opal.send2, $find_super = Opal.find_super, $send = Opal.send, $rb_gt = Opal.rb_gt, $rb_times = Opal.rb_times, $eqeq = Opal.eqeq, $rb_minus = Opal.rb_minus, $to_a = Opal.to_a, $to_ary = Opal.to_ary, $gvars = Opal.gvars, $rb_ge = Opal.rb_ge, $assign_ivar = Opal.assign_ivar, $rb_lt = Opal.rb_lt, $return_self = Opal.return_self, $neqeq = Opal.neqeq, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 12650 12651 Opal.add_stubs('require,include,to_a,warn,raise,replace,respond_to?,to_ary,coerce_to?,===,join,to_str,hash,<=>,==,object_id,inspect,enum_for,class,bsearch_index,to_proc,nil?,coerce_to!,>,*,enumerator_size,empty?,size,map,equal?,dup,each,reduce,-,[],dig,eql?,length,exclude_end?,flatten,frozen?,__id__,&,!,intersection,to_s,new,item,max,min,>=,**,delete_if,reverse,rotate,rand,at,keep_if,shuffle!,<,sort,sort_by,!=,times,[]=,<<,uniq,|,values,is_a?,end,begin,upto,reject,push,select,select!,collect,collect!,unshift,pristine,singleton_class'); 12652 12653 self.$require("corelib/enumerable"); 12654 self.$require("corelib/numeric"); 12655 return (function($base, $super, $parent_nesting) { 12656 var self = $klass($base, $super, 'Array'); 12657 12658 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 12659 12660 12661 self.$include($$$('Enumerable')); 12662 Opal.prop(self.$$prototype, '$$is_array', true); 12663 12664 // Recent versions of V8 (> 7.1) only use an optimized implementation when Array.prototype is unmodified. 12665 // For instance, "array-splice.tq" has a "fast path" (ExtractFastJSArray, defined in "src/codegen/code-stub-assembler.cc") 12666 // but it's only enabled when "IsPrototypeInitialArrayPrototype()" is true. 12667 // 12668 // Older versions of V8 were using relatively fast JS-with-extensions code even when Array.prototype is modified: 12669 // https://github.com/v8/v8/blob/7.0.1/src/js/array.js#L599-L642 12670 // 12671 // In short, Array operations are slow in recent versions of V8 when the Array.prototype has been tampered. 12672 // So, when possible, we are using faster open-coded version to boost the performance. 12673 12674 // As of V8 8.4, depending on the size of the array, this is up to ~25x times faster than Array#shift() 12675 // Implementation is heavily inspired by: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L341-L347 12676 function shiftNoArg(list) { 12677 var r = list[0]; 12678 var index = 1; 12679 var length = list.length; 12680 for (; index < length; index++) { 12681 list[index - 1] = list[index]; 12682 } 12683 list.pop(); 12684 return r; 12685 } 12686 12687 function toArraySubclass(obj, klass) { 12688 if (klass.$$name === Opal.Array) { 12689 return obj; 12690 } else { 12691 return klass.$allocate().$replace((obj).$to_a()); 12692 } 12693 } 12694 12695 // A helper for keep_if and delete_if, filter is either Opal.truthy 12696 // or Opal.falsy. 12697 function filterIf(self, filter, block) { 12698 var value, raised = null, updated = new Array(self.length); 12699 12700 for (var i = 0, i2 = 0, length = self.length; i < length; i++) { 12701 if (!raised) { 12702 try { 12703 value = $yield1(block, self[i]) 12704 } catch(error) { 12705 raised = error; 12706 } 12707 } 12708 12709 if (raised || filter(value)) { 12710 updated[i2] = self[i] 12711 i2 += 1; 12712 } 12713 } 12714 12715 if (i2 !== i) { 12716 self.splice.apply(self, [0, updated.length].concat(updated)); 12717 self.splice(i2, updated.length); 12718 } 12719 12720 if (raised) throw raised; 12721 } 12722 ; 12723 $defs(self, '$[]', function $Array_$$$1($a) { 12724 var $post_args, objects, self = this; 12725 12726 12727 $post_args = $slice(arguments); 12728 objects = $post_args; 12729 return toArraySubclass(objects, self);; 12730 }, -1); 12731 12732 $def(self, '$initialize', function $$initialize(size, obj) { 12733 var block = $$initialize.$$p || nil, self = this; 12734 12735 $$initialize.$$p = null; 12736 12737 ; 12738 if (size == null) size = nil; 12739 if (obj == null) obj = nil; 12740 12741 $deny_frozen_access(self); 12742 12743 if (obj !== nil && block !== nil) { 12744 $Kernel.$warn("warning: block supersedes default value argument") 12745 } 12746 12747 if (size > $$$($$$('Integer'), 'MAX')) { 12748 $Kernel.$raise($$$('ArgumentError'), "array size too big") 12749 } 12750 12751 if (arguments.length > 2) { 12752 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..2)") 12753 } 12754 12755 if (arguments.length === 0) { 12756 self.splice(0, self.length); 12757 return self; 12758 } 12759 12760 if (arguments.length === 1) { 12761 if (size.$$is_array) { 12762 self.$replace(size.$to_a()) 12763 return self; 12764 } else if (size['$respond_to?']("to_ary")) { 12765 self.$replace(size.$to_ary()) 12766 return self; 12767 } 12768 } 12769 12770 size = $coerce_to(size, $$$('Integer'), 'to_int'); 12771 12772 if (size < 0) { 12773 $Kernel.$raise($$$('ArgumentError'), "negative array size") 12774 } 12775 12776 self.splice(0, self.length); 12777 var i, value; 12778 12779 if (block === nil) { 12780 for (i = 0; i < size; i++) { 12781 self.push(obj); 12782 } 12783 } 12784 else { 12785 for (i = 0, value; i < size; i++) { 12786 value = block(i); 12787 self[i] = value; 12788 } 12789 } 12790 12791 return self; 12792 ; 12793 }, -1); 12794 $defs(self, '$try_convert', function $$try_convert(obj) { 12795 12796 return $Opal['$coerce_to?'](obj, $$$('Array'), "to_ary") 12797 }); 12798 12799 $def(self, '$&', function $Array_$$2(other) { 12800 var self = this; 12801 12802 12803 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12804 12805 var result = [], hash = $hash2([], {}), i, length, item; 12806 12807 for (i = 0, length = other.length; i < length; i++) { 12808 $hash_put(hash, other[i], true); 12809 } 12810 12811 for (i = 0, length = self.length; i < length; i++) { 12812 item = self[i]; 12813 if ($hash_delete(hash, item) !== undefined) { 12814 result.push(item); 12815 } 12816 } 12817 12818 return result; 12819 ; 12820 }); 12821 12822 $def(self, '$|', function $Array_$$3(other) { 12823 var self = this; 12824 12825 12826 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12827 12828 var hash = $hash2([], {}), i, length, item; 12829 12830 for (i = 0, length = self.length; i < length; i++) { 12831 $hash_put(hash, self[i], true); 12832 } 12833 12834 for (i = 0, length = other.length; i < length; i++) { 12835 $hash_put(hash, other[i], true); 12836 } 12837 12838 return hash.$keys(); 12839 ; 12840 }); 12841 12842 $def(self, '$*', function $Array_$$4(other) { 12843 var self = this; 12844 12845 12846 if ($truthy(other['$respond_to?']("to_str"))) { 12847 return self.$join(other.$to_str()) 12848 }; 12849 other = $coerce_to(other, $$$('Integer'), 'to_int'); 12850 if ($truthy(other < 0)) { 12851 $Kernel.$raise($$$('ArgumentError'), "negative argument") 12852 }; 12853 12854 var result = [], 12855 converted = self.$to_a(); 12856 12857 for (var i = 0; i < other; i++) { 12858 result = result.concat(converted); 12859 } 12860 12861 return result; 12862 ; 12863 }); 12864 12865 $def(self, '$+', function $Array_$plus$5(other) { 12866 var self = this; 12867 12868 12869 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12870 return self.concat(other);; 12871 }); 12872 12873 $def(self, '$-', function $Array_$minus$6(other) { 12874 var self = this; 12875 12876 12877 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12878 if ($truthy(self.length === 0)) { 12879 return [] 12880 }; 12881 if ($truthy(other.length === 0)) { 12882 return self.slice() 12883 }; 12884 12885 var result = [], hash = $hash2([], {}), i, length, item; 12886 12887 for (i = 0, length = other.length; i < length; i++) { 12888 $hash_put(hash, other[i], true); 12889 } 12890 12891 for (i = 0, length = self.length; i < length; i++) { 12892 item = self[i]; 12893 if ($hash_get(hash, item) === undefined) { 12894 result.push(item); 12895 } 12896 } 12897 12898 return result; 12899 ; 12900 }); 12901 12902 $def(self, '$<<', function $Array_$lt$lt$7(object) { 12903 var self = this; 12904 12905 12906 $deny_frozen_access(self); 12907 self.push(object); 12908 return self; 12909 }); 12910 12911 $def(self, '$<=>', function $Array_$lt_eq_gt$8(other) { 12912 var self = this; 12913 12914 12915 if ($eqeqeq($$$('Array'), other)) { 12916 other = other.$to_a() 12917 } else if ($truthy(other['$respond_to?']("to_ary"))) { 12918 other = other.$to_ary().$to_a() 12919 } else { 12920 return nil 12921 }; 12922 12923 if (self.$hash() === other.$hash()) { 12924 return 0; 12925 } 12926 12927 var count = Math.min(self.length, other.length); 12928 12929 for (var i = 0; i < count; i++) { 12930 var tmp = (self[i])['$<=>'](other[i]); 12931 12932 if (tmp !== 0) { 12933 return tmp; 12934 } 12935 } 12936 12937 return (self.length)['$<=>'](other.length); 12938 ; 12939 }); 12940 12941 $def(self, '$==', function $Array_$eq_eq$9(other) { 12942 var self = this; 12943 12944 12945 var recursed = {}; 12946 12947 function _eqeq(array, other) { 12948 var i, length, a, b; 12949 12950 if (array === other) 12951 return true; 12952 12953 if (!other.$$is_array) { 12954 if ($respond_to(other, '$to_ary')) { 12955 return (other)['$=='](array); 12956 } else { 12957 return false; 12958 } 12959 } 12960 12961 if (array.$$constructor !== Array) 12962 array = (array).$to_a(); 12963 if (other.$$constructor !== Array) 12964 other = (other).$to_a(); 12965 12966 if (array.length !== other.length) { 12967 return false; 12968 } 12969 12970 recursed[(array).$object_id()] = true; 12971 12972 for (i = 0, length = array.length; i < length; i++) { 12973 a = array[i]; 12974 b = other[i]; 12975 if (a.$$is_array) { 12976 if (b.$$is_array && b.length !== a.length) { 12977 return false; 12978 } 12979 if (!recursed.hasOwnProperty((a).$object_id())) { 12980 if (!_eqeq(a, b)) { 12981 return false; 12982 } 12983 } 12984 } else { 12985 if (!(a)['$=='](b)) { 12986 return false; 12987 } 12988 } 12989 } 12990 12991 return true; 12992 } 12993 12994 return _eqeq(self, other); 12995 12996 }); 12997 12998 function $array_slice_range(self, index) { 12999 var size = self.length, 13000 exclude, from, to, result; 13001 13002 exclude = index.excl; 13003 from = index.begin === nil ? 0 : $coerce_to(index.begin, Opal.Integer, 'to_int'); 13004 to = index.end === nil ? -1 : $coerce_to(index.end, Opal.Integer, 'to_int'); 13005 13006 if (from < 0) { 13007 from += size; 13008 13009 if (from < 0) { 13010 return nil; 13011 } 13012 } 13013 13014 if (index.excl_rev && index.begin !== nil) { 13015 from += 1; 13016 } 13017 13018 if (from > size) { 13019 return nil; 13020 } 13021 13022 if (to < 0) { 13023 to += size; 13024 13025 if (to < 0) { 13026 return []; 13027 } 13028 } 13029 13030 if (!exclude || index.end === nil) { 13031 to += 1; 13032 } 13033 13034 result = self.slice(from, to); 13035 return result; 13036 } 13037 13038 function $array_slice_arithmetic_seq(self, index) { 13039 var array, out = [], i = 0, pseudorange; 13040 13041 if (index.step < 0) { 13042 pseudorange = { 13043 begin: index.range.end, 13044 end: index.range.begin, 13045 excl: false, 13046 excl_rev: index.range.excl 13047 }; 13048 array = $array_slice_range(self, pseudorange).$reverse(); 13049 } 13050 else { 13051 array = $array_slice_range(self, index.range); 13052 } 13053 13054 while (i < array.length) { 13055 out.push(array[i]); 13056 i += Math.abs(index.step); 13057 } 13058 13059 return out; 13060 } 13061 13062 function $array_slice_index_length(self, index, length) { 13063 var size = self.length, 13064 exclude, from, to, result; 13065 13066 index = $coerce_to(index, Opal.Integer, 'to_int'); 13067 13068 if (index < 0) { 13069 index += size; 13070 13071 if (index < 0) { 13072 return nil; 13073 } 13074 } 13075 13076 if (length === undefined) { 13077 if (index >= size || index < 0) { 13078 return nil; 13079 } 13080 13081 return self[index]; 13082 } 13083 else { 13084 length = $coerce_to(length, Opal.Integer, 'to_int'); 13085 13086 if (length < 0 || index > size || index < 0) { 13087 return nil; 13088 } 13089 13090 result = self.slice(index, index + length); 13091 } 13092 return result; 13093 } 13094 ; 13095 13096 $def(self, '$[]', function $Array_$$$10(index, length) { 13097 var self = this; 13098 13099 13100 ; 13101 13102 if (index.$$is_range) { 13103 return $array_slice_range(self, index); 13104 } 13105 else if (index.$$is_arithmetic_seq) { 13106 return $array_slice_arithmetic_seq(self, index); 13107 } 13108 else { 13109 return $array_slice_index_length(self, index, length); 13110 } 13111 ; 13112 }, -2); 13113 13114 $def(self, '$[]=', function $Array_$$$eq$11(index, value, extra) { 13115 var self = this, data = nil, length = nil; 13116 13117 13118 ; 13119 $deny_frozen_access(self); 13120 data = nil; 13121 13122 var i, size = self.length; 13123 13124 if (index.$$is_range) { 13125 if (value.$$is_array) 13126 data = value.$to_a(); 13127 else if (value['$respond_to?']("to_ary")) 13128 data = value.$to_ary().$to_a(); 13129 else 13130 data = [value]; 13131 13132 var exclude = index.excl, 13133 from = index.begin === nil ? 0 : $coerce_to(index.begin, Opal.Integer, 'to_int'), 13134 to = index.end === nil ? -1 : $coerce_to(index.end, Opal.Integer, 'to_int'); 13135 13136 if (from < 0) { 13137 from += size; 13138 13139 if (from < 0) { 13140 $Kernel.$raise($$$('RangeError'), "" + (index.$inspect()) + " out of range"); 13141 } 13142 } 13143 13144 if (to < 0) { 13145 to += size; 13146 } 13147 13148 if (!exclude || index.end === nil) { 13149 to += 1; 13150 } 13151 13152 if (from > size) { 13153 for (i = size; i < from; i++) { 13154 self[i] = nil; 13155 } 13156 } 13157 13158 if (to < 0) { 13159 self.splice.apply(self, [from, 0].concat(data)); 13160 } 13161 else { 13162 self.splice.apply(self, [from, to - from].concat(data)); 13163 } 13164 13165 return value; 13166 } else { 13167 if (extra === undefined) { 13168 (length = 1) 13169 } else { 13170 length = value; 13171 value = extra; 13172 13173 if (value.$$is_array) 13174 data = value.$to_a(); 13175 else if (value['$respond_to?']("to_ary")) 13176 data = value.$to_ary().$to_a(); 13177 else 13178 data = [value]; 13179 } 13180 13181 var old; 13182 13183 index = $coerce_to(index, $$$('Integer'), 'to_int'); 13184 length = $coerce_to(length, $$$('Integer'), 'to_int'); 13185 13186 if (index < 0) { 13187 old = index; 13188 index += size; 13189 13190 if (index < 0) { 13191 $Kernel.$raise($$$('IndexError'), "index " + (old) + " too small for array; minimum " + (-self.length)); 13192 } 13193 } 13194 13195 if (length < 0) { 13196 $Kernel.$raise($$$('IndexError'), "negative length (" + (length) + ")") 13197 } 13198 13199 if (index > size) { 13200 for (i = size; i < index; i++) { 13201 self[i] = nil; 13202 } 13203 } 13204 13205 if (extra === undefined) { 13206 self[index] = value; 13207 } 13208 else { 13209 self.splice.apply(self, [index, length].concat(data)); 13210 } 13211 13212 return value; 13213 } 13214 ; 13215 }, -3); 13216 13217 $def(self, '$any?', function $Array_any$ques$12(pattern) { 13218 var block = $Array_any$ques$12.$$p || nil, self = this; 13219 13220 $Array_any$ques$12.$$p = null; 13221 13222 ; 13223 ; 13224 if (self.length === 0) return false; 13225 return $send2(self, $find_super(self, 'any?', $Array_any$ques$12, false, true), 'any?', [pattern], block); 13226 }, -1); 13227 13228 $def(self, '$assoc', function $$assoc(object) { 13229 var self = this; 13230 13231 13232 for (var i = 0, length = self.length, item; i < length; i++) { 13233 if (item = self[i], item.length && (item[0])['$=='](object)) { 13234 return item; 13235 } 13236 } 13237 13238 return nil; 13239 13240 }); 13241 13242 $def(self, '$at', function $$at(index) { 13243 var self = this; 13244 13245 13246 index = $coerce_to(index, $$$('Integer'), 'to_int') 13247 13248 if (index < 0) { 13249 index += self.length; 13250 } 13251 13252 if (index < 0 || index >= self.length) { 13253 return nil; 13254 } 13255 13256 return self[index]; 13257 13258 }); 13259 13260 $def(self, '$bsearch_index', function $$bsearch_index() { 13261 var block = $$bsearch_index.$$p || nil, self = this; 13262 13263 $$bsearch_index.$$p = null; 13264 13265 ; 13266 if (!(block !== nil)) { 13267 return self.$enum_for("bsearch_index") 13268 }; 13269 13270 var min = 0, 13271 max = self.length, 13272 mid, 13273 val, 13274 ret, 13275 smaller = false, 13276 satisfied = nil; 13277 13278 while (min < max) { 13279 mid = min + Math.floor((max - min) / 2); 13280 val = self[mid]; 13281 ret = $yield1(block, val); 13282 13283 if (ret === true) { 13284 satisfied = mid; 13285 smaller = true; 13286 } 13287 else if (ret === false || ret === nil) { 13288 smaller = false; 13289 } 13290 else if (ret.$$is_number) { 13291 if (ret === 0) { return mid; } 13292 smaller = (ret < 0); 13293 } 13294 else { 13295 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((ret).$class()) + " (must be numeric, true, false or nil)") 13296 } 13297 13298 if (smaller) { max = mid; } else { min = mid + 1; } 13299 } 13300 13301 return satisfied; 13302 ; 13303 }); 13304 13305 $def(self, '$bsearch', function $$bsearch() { 13306 var block = $$bsearch.$$p || nil, self = this, index = nil; 13307 13308 $$bsearch.$$p = null; 13309 13310 ; 13311 if (!(block !== nil)) { 13312 return self.$enum_for("bsearch") 13313 }; 13314 index = $send(self, 'bsearch_index', [], block.$to_proc()); 13315 13316 if (index != null && index.$$is_number) { 13317 return self[index]; 13318 } else { 13319 return index; 13320 } 13321 ; 13322 }); 13323 13324 $def(self, '$cycle', function $$cycle(n) { 13325 var block = $$cycle.$$p || nil, self = this; 13326 13327 $$cycle.$$p = null; 13328 13329 ; 13330 if (n == null) n = nil; 13331 if (!(block !== nil)) { 13332 return $send(self, 'enum_for', ["cycle", n], function $$13(){var self = $$13.$$s == null ? this : $$13.$$s; 13333 13334 if ($truthy(n['$nil?']())) { 13335 return $$$($$$('Float'), 'INFINITY') 13336 } else { 13337 13338 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 13339 if ($truthy($rb_gt(n, 0))) { 13340 return $rb_times(self.$enumerator_size(), n) 13341 } else { 13342 return 0 13343 }; 13344 }}, {$$s: self}) 13345 }; 13346 if (($truthy(self['$empty?']()) || ($eqeq(n, 0)))) { 13347 return nil 13348 }; 13349 13350 var i, length, value; 13351 13352 if (n === nil) { 13353 while (true) { 13354 for (i = 0, length = self.length; i < length; i++) { 13355 value = $yield1(block, self[i]); 13356 } 13357 } 13358 } 13359 else { 13360 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 13361 if (n <= 0) { 13362 return self; 13363 } 13364 13365 while (n > 0) { 13366 for (i = 0, length = self.length; i < length; i++) { 13367 value = $yield1(block, self[i]); 13368 } 13369 13370 n--; 13371 } 13372 } 13373 ; 13374 return self; 13375 }, -1); 13376 13377 $def(self, '$clear', function $$clear() { 13378 var self = this; 13379 13380 13381 $deny_frozen_access(self); 13382 self.splice(0, self.length); 13383 return self; 13384 }); 13385 13386 $def(self, '$count', function $$count(object) { 13387 var block = $$count.$$p || nil, self = this; 13388 13389 $$count.$$p = null; 13390 13391 ; 13392 ; 13393 if (($truthy(object !== undefined) || ($truthy(block)))) { 13394 return $send2(self, $find_super(self, 'count', $$count, false, true), 'count', [object], block) 13395 } else { 13396 return self.$size() 13397 }; 13398 }, -1); 13399 13400 $def(self, '$initialize_copy', function $$initialize_copy(other) { 13401 var self = this; 13402 13403 return self.$replace(other) 13404 }); 13405 13406 $def(self, '$collect', function $$collect() { 13407 var block = $$collect.$$p || nil, self = this; 13408 13409 $$collect.$$p = null; 13410 13411 ; 13412 if (!(block !== nil)) { 13413 return $send(self, 'enum_for', ["collect"], function $$14(){var self = $$14.$$s == null ? this : $$14.$$s; 13414 13415 return self.$size()}, {$$s: self}) 13416 }; 13417 13418 var result = []; 13419 13420 for (var i = 0, length = self.length; i < length; i++) { 13421 var value = $yield1(block, self[i]); 13422 result.push(value); 13423 } 13424 13425 return result; 13426 ; 13427 }); 13428 13429 $def(self, '$collect!', function $Array_collect$excl$15() { 13430 var block = $Array_collect$excl$15.$$p || nil, self = this; 13431 13432 $Array_collect$excl$15.$$p = null; 13433 13434 ; 13435 if (!(block !== nil)) { 13436 return $send(self, 'enum_for', ["collect!"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 13437 13438 return self.$size()}, {$$s: self}) 13439 }; 13440 13441 $deny_frozen_access(self); 13442 13443 for (var i = 0, length = self.length; i < length; i++) { 13444 var value = $yield1(block, self[i]); 13445 self[i] = value; 13446 } 13447 ; 13448 return self; 13449 }); 13450 13451 function binomial_coefficient(n, k) { 13452 if (n === k || k === 0) { 13453 return 1; 13454 } 13455 13456 if (k > 0 && n > k) { 13457 return binomial_coefficient(n - 1, k - 1) + binomial_coefficient(n - 1, k); 13458 } 13459 13460 return 0; 13461 } 13462 ; 13463 13464 $def(self, '$combination', function $$combination(n) { 13465 var $yield = $$combination.$$p || nil, self = this, num = nil; 13466 13467 $$combination.$$p = null; 13468 13469 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 13470 if (!($yield !== nil)) { 13471 return $send(self, 'enum_for', ["combination", num], function $$17(){var self = $$17.$$s == null ? this : $$17.$$s; 13472 13473 return binomial_coefficient(self.length, num)}, {$$s: self}) 13474 }; 13475 13476 var i, length, stack, chosen, lev, done, next; 13477 13478 if (num === 0) { 13479 Opal.yield1($yield, []) 13480 } else if (num === 1) { 13481 for (i = 0, length = self.length; i < length; i++) { 13482 Opal.yield1($yield, [self[i]]) 13483 } 13484 } 13485 else if (num === self.length) { 13486 Opal.yield1($yield, self.slice()) 13487 } 13488 else if (num >= 0 && num < self.length) { 13489 stack = []; 13490 for (i = 0; i <= num + 1; i++) { 13491 stack.push(0); 13492 } 13493 13494 chosen = []; 13495 lev = 0; 13496 done = false; 13497 stack[0] = -1; 13498 13499 while (!done) { 13500 chosen[lev] = self[stack[lev+1]]; 13501 while (lev < num - 1) { 13502 lev++; 13503 next = stack[lev+1] = stack[lev] + 1; 13504 chosen[lev] = self[next]; 13505 } 13506 Opal.yield1($yield, chosen.slice()) 13507 lev++; 13508 do { 13509 done = (lev === 0); 13510 stack[lev]++; 13511 lev--; 13512 } while ( stack[lev+1] + num === self.length + lev + 1 ); 13513 } 13514 } 13515 ; 13516 return self; 13517 }); 13518 13519 $def(self, '$repeated_combination', function $$repeated_combination(n) { 13520 var $yield = $$repeated_combination.$$p || nil, self = this, num = nil; 13521 13522 $$repeated_combination.$$p = null; 13523 13524 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 13525 if (!($yield !== nil)) { 13526 return $send(self, 'enum_for', ["repeated_combination", num], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 13527 13528 return binomial_coefficient(self.length + num - 1, num);}, {$$s: self}) 13529 }; 13530 13531 function iterate(max, from, buffer, self) { 13532 if (buffer.length == max) { 13533 var copy = buffer.slice(); 13534 Opal.yield1($yield, copy) 13535 return; 13536 } 13537 for (var i = from; i < self.length; i++) { 13538 buffer.push(self[i]); 13539 iterate(max, i, buffer, self); 13540 buffer.pop(); 13541 } 13542 } 13543 13544 if (num >= 0) { 13545 iterate(num, 0, [], self); 13546 } 13547 ; 13548 return self; 13549 }); 13550 13551 $def(self, '$compact', function $$compact() { 13552 var self = this; 13553 13554 13555 var result = []; 13556 13557 for (var i = 0, length = self.length, item; i < length; i++) { 13558 if ((item = self[i]) !== nil) { 13559 result.push(item); 13560 } 13561 } 13562 13563 return result; 13564 13565 }); 13566 13567 $def(self, '$compact!', function $Array_compact$excl$19() { 13568 var self = this; 13569 13570 13571 $deny_frozen_access(self); 13572 13573 var original = self.length; 13574 13575 for (var i = 0, length = self.length; i < length; i++) { 13576 if (self[i] === nil) { 13577 self.splice(i, 1); 13578 13579 length--; 13580 i--; 13581 } 13582 } 13583 13584 return self.length === original ? nil : self; 13585 13586 }); 13587 13588 $def(self, '$concat', function $$concat($a) { 13589 var $post_args, others, self = this; 13590 13591 13592 $post_args = $slice(arguments); 13593 others = $post_args; 13594 $deny_frozen_access(self); 13595 others = $send(others, 'map', [], function $$20(other){var self = $$20.$$s == null ? this : $$20.$$s; 13596 13597 13598 if (other == null) other = nil; 13599 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 13600 if ($truthy(other['$equal?'](self))) { 13601 other = other.$dup() 13602 }; 13603 return other;}, {$$s: self}); 13604 $send(others, 'each', [], function $$21(other){var self = $$21.$$s == null ? this : $$21.$$s; 13605 13606 13607 if (other == null) other = nil; 13608 13609 for (var i = 0, length = other.length; i < length; i++) { 13610 self.push(other[i]); 13611 } 13612 ;}, {$$s: self}); 13613 return self; 13614 }, -1); 13615 13616 $def(self, '$delete', function $Array_delete$22(object) { 13617 var $yield = $Array_delete$22.$$p || nil, self = this; 13618 13619 $Array_delete$22.$$p = null; 13620 13621 var original = self.length; 13622 13623 for (var i = 0, length = original; i < length; i++) { 13624 if ((self[i])['$=='](object)) { 13625 $deny_frozen_access(self); 13626 13627 self.splice(i, 1); 13628 13629 length--; 13630 i--; 13631 } 13632 } 13633 13634 if (self.length === original) { 13635 if (($yield !== nil)) { 13636 return Opal.yieldX($yield, []); 13637 } 13638 return nil; 13639 } 13640 return object; 13641 13642 }); 13643 13644 $def(self, '$delete_at', function $$delete_at(index) { 13645 var self = this; 13646 13647 13648 $deny_frozen_access(self); 13649 13650 index = $coerce_to(index, $$$('Integer'), 'to_int'); 13651 13652 if (index < 0) { 13653 index += self.length; 13654 } 13655 13656 if (index < 0 || index >= self.length) { 13657 return nil; 13658 } 13659 13660 var result = self[index]; 13661 13662 self.splice(index, 1); 13663 13664 return result; 13665 13666 }); 13667 13668 $def(self, '$delete_if', function $$delete_if() { 13669 var block = $$delete_if.$$p || nil, self = this; 13670 13671 $$delete_if.$$p = null; 13672 13673 ; 13674 if (!(block !== nil)) { 13675 return $send(self, 'enum_for', ["delete_if"], function $$23(){var self = $$23.$$s == null ? this : $$23.$$s; 13676 13677 return self.$size()}, {$$s: self}) 13678 }; 13679 13680 $deny_frozen_access(self); 13681 13682 filterIf(self, $falsy, block) 13683 ; 13684 return self; 13685 }); 13686 13687 $def(self, '$difference', function $$difference($a) { 13688 var $post_args, arrays, self = this; 13689 13690 13691 $post_args = $slice(arguments); 13692 arrays = $post_args; 13693 return $send(arrays, 'reduce', [self.$to_a().$dup()], function $$24(a, b){ 13694 13695 if (a == null) a = nil; 13696 if (b == null) b = nil; 13697 return $rb_minus(a, b);}); 13698 }, -1); 13699 13700 $def(self, '$dig', function $$dig(idx, $a) { 13701 var $post_args, idxs, self = this, item = nil; 13702 13703 13704 $post_args = $slice(arguments, 1); 13705 idxs = $post_args; 13706 item = self['$[]'](idx); 13707 13708 if (item === nil || idxs.length === 0) { 13709 return item; 13710 } 13711 ; 13712 if (!$truthy(item['$respond_to?']("dig"))) { 13713 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method") 13714 }; 13715 return $send(item, 'dig', $to_a(idxs)); 13716 }, -2); 13717 13718 $def(self, '$drop', function $$drop(number) { 13719 var self = this; 13720 13721 13722 number = $coerce_to(number, $$$('Integer'), 'to_int'); 13723 13724 if (number < 0) { 13725 $Kernel.$raise($$$('ArgumentError')) 13726 } 13727 13728 return self.slice(number); 13729 13730 }); 13731 13732 $def(self, '$dup', function $$dup() { 13733 var $yield = $$dup.$$p || nil, self = this; 13734 13735 $$dup.$$p = null; 13736 13737 13738 if (self.$$class === Opal.Array && 13739 self.$$class.$allocate.$$pristine && 13740 self.$copy_instance_variables.$$pristine && 13741 self.$initialize_dup.$$pristine) { 13742 return self.slice(0); 13743 } 13744 ; 13745 return $send2(self, $find_super(self, 'dup', $$dup, false, true), 'dup', [], $yield); 13746 }); 13747 13748 $def(self, '$each', function $$each() { 13749 var block = $$each.$$p || nil, self = this; 13750 13751 $$each.$$p = null; 13752 13753 ; 13754 if (!(block !== nil)) { 13755 return $send(self, 'enum_for', ["each"], function $$25(){var self = $$25.$$s == null ? this : $$25.$$s; 13756 13757 return self.$size()}, {$$s: self}) 13758 }; 13759 13760 for (var i = 0, length = self.length; i < length; i++) { 13761 var value = $yield1(block, self[i]); 13762 } 13763 ; 13764 return self; 13765 }); 13766 13767 $def(self, '$each_index', function $$each_index() { 13768 var block = $$each_index.$$p || nil, self = this; 13769 13770 $$each_index.$$p = null; 13771 13772 ; 13773 if (!(block !== nil)) { 13774 return $send(self, 'enum_for', ["each_index"], function $$26(){var self = $$26.$$s == null ? this : $$26.$$s; 13775 13776 return self.$size()}, {$$s: self}) 13777 }; 13778 13779 for (var i = 0, length = self.length; i < length; i++) { 13780 var value = $yield1(block, i); 13781 } 13782 ; 13783 return self; 13784 }); 13785 13786 $def(self, '$empty?', function $Array_empty$ques$27() { 13787 var self = this; 13788 13789 return self.length === 0; 13790 }); 13791 13792 $def(self, '$eql?', function $Array_eql$ques$28(other) { 13793 var self = this; 13794 13795 13796 var recursed = {}; 13797 13798 function _eql(array, other) { 13799 var i, length, a, b; 13800 13801 if (!other.$$is_array) { 13802 return false; 13803 } 13804 13805 other = other.$to_a(); 13806 13807 if (array.length !== other.length) { 13808 return false; 13809 } 13810 13811 recursed[(array).$object_id()] = true; 13812 13813 for (i = 0, length = array.length; i < length; i++) { 13814 a = array[i]; 13815 b = other[i]; 13816 if (a.$$is_array) { 13817 if (b.$$is_array && b.length !== a.length) { 13818 return false; 13819 } 13820 if (!recursed.hasOwnProperty((a).$object_id())) { 13821 if (!_eql(a, b)) { 13822 return false; 13823 } 13824 } 13825 } else { 13826 if (!(a)['$eql?'](b)) { 13827 return false; 13828 } 13829 } 13830 } 13831 13832 return true; 13833 } 13834 13835 return _eql(self, other); 13836 13837 }); 13838 13839 $def(self, '$fetch', function $$fetch(index, defaults) { 13840 var block = $$fetch.$$p || nil, self = this; 13841 13842 $$fetch.$$p = null; 13843 13844 ; 13845 ; 13846 13847 var original = index; 13848 13849 index = $coerce_to(index, $$$('Integer'), 'to_int'); 13850 13851 if (index < 0) { 13852 index += self.length; 13853 } 13854 13855 if (index >= 0 && index < self.length) { 13856 return self[index]; 13857 } 13858 13859 if (block !== nil && defaults != null) { 13860 self.$warn("warning: block supersedes default value argument") 13861 } 13862 13863 if (block !== nil) { 13864 return block(original); 13865 } 13866 13867 if (defaults != null) { 13868 return defaults; 13869 } 13870 13871 if (self.length === 0) { 13872 $Kernel.$raise($$$('IndexError'), "index " + (original) + " outside of array bounds: 0...0") 13873 } 13874 else { 13875 $Kernel.$raise($$$('IndexError'), "index " + (original) + " outside of array bounds: -" + (self.length) + "..." + (self.length)); 13876 } 13877 ; 13878 }, -2); 13879 13880 $def(self, '$fill', function $$fill($a) { 13881 var block = $$fill.$$p || nil, $post_args, args, $b, $c, self = this, one = nil, two = nil, obj = nil, left = nil, right = nil; 13882 13883 $$fill.$$p = null; 13884 13885 ; 13886 $post_args = $slice(arguments); 13887 args = $post_args; 13888 13889 $deny_frozen_access(self); 13890 13891 var i, length, value; 13892 ; 13893 if ($truthy(block)) { 13894 13895 if ($truthy(args.length > 2)) { 13896 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$length()) + " for 0..2)") 13897 }; 13898 $c = args, $b = $to_ary($c), (one = ($b[0] == null ? nil : $b[0])), (two = ($b[1] == null ? nil : $b[1])), $c; 13899 } else { 13900 13901 if ($truthy(args.length == 0)) { 13902 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)") 13903 } else if ($truthy(args.length > 3)) { 13904 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$length()) + " for 1..3)") 13905 }; 13906 $c = args, $b = $to_ary($c), (obj = ($b[0] == null ? nil : $b[0])), (one = ($b[1] == null ? nil : $b[1])), (two = ($b[2] == null ? nil : $b[2])), $c; 13907 }; 13908 if ($eqeqeq($$$('Range'), one)) { 13909 13910 if ($truthy(two)) { 13911 $Kernel.$raise($$$('TypeError'), "length invalid with range") 13912 }; 13913 left = one.begin === nil ? 0 : $coerce_to(one.begin, $$$('Integer'), 'to_int'); 13914 if ($truthy(left < 0)) { 13915 left += this.length 13916 }; 13917 if ($truthy(left < 0)) { 13918 $Kernel.$raise($$$('RangeError'), "" + (one.$inspect()) + " out of range") 13919 }; 13920 right = one.end === nil ? -1 : $coerce_to(one.end, $$$('Integer'), 'to_int'); 13921 if ($truthy(right < 0)) { 13922 right += this.length 13923 }; 13924 if (!$truthy(one['$exclude_end?']())) { 13925 right += 1 13926 }; 13927 if ($truthy(right <= left)) { 13928 return self 13929 }; 13930 } else if ($truthy(one)) { 13931 13932 left = $coerce_to(one, $$$('Integer'), 'to_int'); 13933 if ($truthy(left < 0)) { 13934 left += this.length 13935 }; 13936 if ($truthy(left < 0)) { 13937 left = 0 13938 }; 13939 if ($truthy(two)) { 13940 13941 right = $coerce_to(two, $$$('Integer'), 'to_int'); 13942 if ($truthy(right == 0)) { 13943 return self 13944 }; 13945 right += left; 13946 } else { 13947 right = this.length 13948 }; 13949 } else { 13950 13951 left = 0; 13952 right = this.length; 13953 }; 13954 if ($truthy(left > this.length)) { 13955 13956 for (i = this.length; i < right; i++) { 13957 self[i] = nil; 13958 } 13959 13960 }; 13961 if ($truthy(right > this.length)) { 13962 this.length = right 13963 }; 13964 if ($truthy(block)) { 13965 13966 for (length = this.length; left < right; left++) { 13967 value = block(left); 13968 self[left] = value; 13969 } 13970 13971 } else { 13972 13973 for (length = this.length; left < right; left++) { 13974 self[left] = obj; 13975 } 13976 13977 }; 13978 return self; 13979 }, -1); 13980 13981 $def(self, '$first', function $$first(count) { 13982 var self = this; 13983 13984 13985 ; 13986 13987 if (count == null) { 13988 return self.length === 0 ? nil : self[0]; 13989 } 13990 13991 count = $coerce_to(count, $$$('Integer'), 'to_int'); 13992 13993 if (count < 0) { 13994 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 13995 } 13996 13997 return self.slice(0, count); 13998 ; 13999 }, -1); 14000 14001 $def(self, '$flatten', function $$flatten(level) { 14002 var self = this; 14003 14004 14005 ; 14006 14007 function _flatten(array, level) { 14008 var result = [], 14009 i, length, 14010 item, ary; 14011 14012 array = (array).$to_a(); 14013 14014 for (i = 0, length = array.length; i < length; i++) { 14015 item = array[i]; 14016 14017 if (!$respond_to(item, '$to_ary', true)) { 14018 result.push(item); 14019 continue; 14020 } 14021 14022 ary = (item).$to_ary(); 14023 14024 if (ary === nil) { 14025 result.push(item); 14026 continue; 14027 } 14028 14029 if (!ary.$$is_array) { 14030 $Kernel.$raise($$$('TypeError')); 14031 } 14032 14033 if (ary === self) { 14034 $Kernel.$raise($$$('ArgumentError')); 14035 } 14036 14037 switch (level) { 14038 case undefined: 14039 result = result.concat(_flatten(ary)); 14040 break; 14041 case 0: 14042 result.push(ary); 14043 break; 14044 default: 14045 result.push.apply(result, _flatten(ary, level - 1)); 14046 } 14047 } 14048 return result; 14049 } 14050 14051 if (level !== undefined) { 14052 level = $coerce_to(level, $$$('Integer'), 'to_int'); 14053 } 14054 14055 return _flatten(self, level); 14056 ; 14057 }, -1); 14058 14059 $def(self, '$flatten!', function $Array_flatten$excl$29(level) { 14060 var self = this; 14061 14062 14063 ; 14064 14065 $deny_frozen_access(self); 14066 14067 var flattened = self.$flatten(level); 14068 14069 if (self.length == flattened.length) { 14070 for (var i = 0, length = self.length; i < length; i++) { 14071 if (self[i] !== flattened[i]) { 14072 break; 14073 } 14074 } 14075 14076 if (i == length) { 14077 return nil; 14078 } 14079 } 14080 14081 self.$replace(flattened); 14082 ; 14083 return self; 14084 }, -1); 14085 14086 $def(self, '$freeze', function $$freeze() { 14087 var self = this; 14088 14089 14090 if ($truthy(self['$frozen?']())) { 14091 return self 14092 }; 14093 return $freeze(self);; 14094 }); 14095 14096 $def(self, '$hash', function $$hash() { 14097 var self = this; 14098 14099 14100 var top = ($hash_ids === undefined), 14101 result = ['A'], 14102 hash_id = self.$object_id(), 14103 item, i, key; 14104 14105 try { 14106 if (top) { 14107 $hash_ids = Object.create(null); 14108 } 14109 14110 // return early for recursive structures 14111 if ($hash_ids[hash_id]) { 14112 return 'self'; 14113 } 14114 14115 for (key in $hash_ids) { 14116 item = $hash_ids[key]; 14117 if (self['$eql?'](item)) { 14118 return 'self'; 14119 } 14120 } 14121 14122 $hash_ids[hash_id] = self; 14123 14124 for (i = 0; i < self.length; i++) { 14125 item = self[i]; 14126 result.push(item.$hash()); 14127 } 14128 14129 return result.join(','); 14130 } finally { 14131 if (top) { 14132 $hash_ids = undefined; 14133 } 14134 } 14135 14136 }); 14137 14138 $def(self, '$include?', function $Array_include$ques$30(member) { 14139 var self = this; 14140 14141 14142 for (var i = 0, length = self.length; i < length; i++) { 14143 if ((self[i])['$=='](member)) { 14144 return true; 14145 } 14146 } 14147 14148 return false; 14149 14150 }); 14151 14152 $def(self, '$index', function $$index(object) { 14153 var block = $$index.$$p || nil, self = this; 14154 14155 $$index.$$p = null; 14156 14157 ; 14158 ; 14159 14160 var i, length, value; 14161 14162 if (object != null && block !== nil) { 14163 self.$warn("warning: given block not used") 14164 } 14165 14166 if (object != null) { 14167 for (i = 0, length = self.length; i < length; i++) { 14168 if ((self[i])['$=='](object)) { 14169 return i; 14170 } 14171 } 14172 } 14173 else if (block !== nil) { 14174 for (i = 0, length = self.length; i < length; i++) { 14175 value = block(self[i]); 14176 14177 if (value !== false && value !== nil) { 14178 return i; 14179 } 14180 } 14181 } 14182 else { 14183 return self.$enum_for("index"); 14184 } 14185 14186 return nil; 14187 ; 14188 }, -1); 14189 14190 $def(self, '$insert', function $$insert(index, $a) { 14191 var $post_args, objects, self = this; 14192 14193 14194 $post_args = $slice(arguments, 1); 14195 objects = $post_args; 14196 14197 $deny_frozen_access(self); 14198 14199 index = $coerce_to(index, $$$('Integer'), 'to_int'); 14200 14201 if (objects.length > 0) { 14202 if (index < 0) { 14203 index += self.length + 1; 14204 14205 if (index < 0) { 14206 $Kernel.$raise($$$('IndexError'), "" + (index) + " is out of bounds"); 14207 } 14208 } 14209 if (index > self.length) { 14210 for (var i = self.length; i < index; i++) { 14211 self.push(nil); 14212 } 14213 } 14214 14215 self.splice.apply(self, [index, 0].concat(objects)); 14216 } 14217 ; 14218 return self; 14219 }, -2); 14220 var inspect_stack = []; 14221 14222 $def(self, '$inspect', function $$inspect() { 14223 var self = this; 14224 14225 14226 14227 var result = [], 14228 id = self.$__id__(), 14229 pushed = true; 14230 ; 14231 14232 return (function() { try { 14233 14234 14235 if (inspect_stack.indexOf(id) !== -1) { 14236 pushed = false; 14237 return '[...]'; 14238 } 14239 inspect_stack.push(id) 14240 14241 for (var i = 0, length = self.length; i < length; i++) { 14242 var item = self['$[]'](i); 14243 14244 result.push($$('Opal').$inspect(item)); 14245 } 14246 14247 return '[' + result.join(', ') + ']'; 14248 ; 14249 return nil; 14250 } finally { 14251 if (pushed) inspect_stack.pop() 14252 }; })();; 14253 }); 14254 14255 $def(self, '$intersection', function $$intersection($a) { 14256 var $post_args, arrays, self = this; 14257 14258 14259 $post_args = $slice(arguments); 14260 arrays = $post_args; 14261 return $send(arrays, 'reduce', [self.$to_a().$dup()], function $$31(a, b){ 14262 14263 if (a == null) a = nil; 14264 if (b == null) b = nil; 14265 return a['$&'](b);}); 14266 }, -1); 14267 14268 $def(self, '$intersect?', function $Array_intersect$ques$32(other) { 14269 var self = this; 14270 14271 return self.$intersection(other)['$empty?']()['$!']() 14272 }); 14273 14274 $def(self, '$join', function $$join(sep) { 14275 var self = this; 14276 if ($gvars[","] == null) $gvars[","] = nil; 14277 14278 14279 if (sep == null) sep = nil; 14280 if ($truthy(self.length === 0)) { 14281 return "" 14282 }; 14283 if ($truthy(sep === nil)) { 14284 sep = $gvars[","] 14285 }; 14286 14287 var result = []; 14288 var i, length, item, tmp; 14289 14290 for (i = 0, length = self.length; i < length; i++) { 14291 item = self[i]; 14292 14293 if ($respond_to(item, '$to_str')) { 14294 tmp = (item).$to_str(); 14295 14296 if (tmp !== nil) { 14297 result.push((tmp).$to_s()); 14298 14299 continue; 14300 } 14301 } 14302 14303 if ($respond_to(item, '$to_ary')) { 14304 tmp = (item).$to_ary(); 14305 14306 if (tmp === self) { 14307 $Kernel.$raise($$$('ArgumentError')); 14308 } 14309 14310 if (tmp !== nil) { 14311 result.push((tmp).$join(sep)); 14312 14313 continue; 14314 } 14315 } 14316 14317 if ($respond_to(item, '$to_s')) { 14318 tmp = (item).$to_s(); 14319 14320 if (tmp !== nil) { 14321 result.push(tmp); 14322 14323 continue; 14324 } 14325 } 14326 14327 $Kernel.$raise($$$('NoMethodError').$new("" + ($$('Opal').$inspect(self.$item())) + " doesn't respond to #to_str, #to_ary or #to_s", "to_str")); 14328 } 14329 14330 if (sep === nil) { 14331 return result.join(''); 14332 } 14333 else { 14334 return result.join($Opal['$coerce_to!'](sep, $$$('String'), "to_str").$to_s()); 14335 } 14336 ; 14337 }, -1); 14338 14339 $def(self, '$keep_if', function $$keep_if() { 14340 var block = $$keep_if.$$p || nil, self = this; 14341 14342 $$keep_if.$$p = null; 14343 14344 ; 14345 if (!(block !== nil)) { 14346 return $send(self, 'enum_for', ["keep_if"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 14347 14348 return self.$size()}, {$$s: self}) 14349 }; 14350 14351 $deny_frozen_access(self); 14352 14353 filterIf(self, $truthy, block) 14354 ; 14355 return self; 14356 }); 14357 14358 $def(self, '$last', function $$last(count) { 14359 var self = this; 14360 14361 14362 ; 14363 14364 if (count == null) { 14365 return self.length === 0 ? nil : self[self.length - 1]; 14366 } 14367 14368 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14369 14370 if (count < 0) { 14371 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 14372 } 14373 14374 if (count > self.length) { 14375 count = self.length; 14376 } 14377 14378 return self.slice(self.length - count, self.length); 14379 ; 14380 }, -1); 14381 14382 $def(self, '$length', function $$length() { 14383 var self = this; 14384 14385 return self.length; 14386 }); 14387 14388 $def(self, '$max', function $$max(n) { 14389 var block = $$max.$$p || nil, self = this; 14390 14391 $$max.$$p = null; 14392 14393 ; 14394 ; 14395 return $send(self.$each(), 'max', [n], block.$to_proc()); 14396 }, -1); 14397 14398 $def(self, '$min', function $$min() { 14399 var block = $$min.$$p || nil, self = this; 14400 14401 $$min.$$p = null; 14402 14403 ; 14404 return $send(self.$each(), 'min', [], block.$to_proc()); 14405 }); 14406 14407 // Returns the product of from, from-1, ..., from - how_many + 1. 14408 function descending_factorial(from, how_many) { 14409 var count = how_many >= 0 ? 1 : 0; 14410 while (how_many) { 14411 count *= from; 14412 from--; 14413 how_many--; 14414 } 14415 return count; 14416 } 14417 ; 14418 14419 $def(self, '$permutation', function $$permutation(num) { 14420 var block = $$permutation.$$p || nil, self = this, perm = nil, used = nil; 14421 14422 $$permutation.$$p = null; 14423 14424 ; 14425 ; 14426 if (!(block !== nil)) { 14427 return $send(self, 'enum_for', ["permutation", num], function $$34(){var self = $$34.$$s == null ? this : $$34.$$s; 14428 14429 return descending_factorial(self.length, num === undefined ? self.length : num);}, {$$s: self}) 14430 }; 14431 14432 var permute, offensive, output; 14433 14434 if (num === undefined) { 14435 num = self.length; 14436 } 14437 else { 14438 num = $coerce_to(num, $$$('Integer'), 'to_int'); 14439 } 14440 14441 if (num < 0 || self.length < num) { 14442 // no permutations, yield nothing 14443 } 14444 else if (num === 0) { 14445 // exactly one permutation: the zero-length array 14446 Opal.yield1(block, []) 14447 } 14448 else if (num === 1) { 14449 // this is a special, easy case 14450 for (var i = 0; i < self.length; i++) { 14451 Opal.yield1(block, [self[i]]) 14452 } 14453 } 14454 else { 14455 // this is the general case 14456 (perm = $$('Array').$new(num)); 14457 (used = $$('Array').$new(self.length, false)); 14458 14459 permute = function(num, perm, index, used, blk) { 14460 self = this; 14461 for(var i = 0; i < self.length; i++){ 14462 if(used['$[]'](i)['$!']()) { 14463 perm[index] = i; 14464 if(index < num - 1) { 14465 used[i] = true; 14466 permute.call(self, num, perm, index + 1, used, blk); 14467 used[i] = false; 14468 } 14469 else { 14470 output = []; 14471 for (var j = 0; j < perm.length; j++) { 14472 output.push(self[perm[j]]); 14473 } 14474 $yield1(blk, output); 14475 } 14476 } 14477 } 14478 } 14479 14480 if ((block !== nil)) { 14481 // offensive (both definitions) copy. 14482 offensive = self.slice(); 14483 permute.call(offensive, num, perm, 0, used, block); 14484 } 14485 else { 14486 permute.call(self, num, perm, 0, used, block); 14487 } 14488 } 14489 ; 14490 return self; 14491 }, -1); 14492 14493 $def(self, '$repeated_permutation', function $$repeated_permutation(n) { 14494 var $yield = $$repeated_permutation.$$p || nil, self = this, num = nil; 14495 14496 $$repeated_permutation.$$p = null; 14497 14498 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 14499 if (!($yield !== nil)) { 14500 return $send(self, 'enum_for', ["repeated_permutation", num], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 14501 14502 if ($truthy($rb_ge(num, 0))) { 14503 return self.$size()['$**'](num) 14504 } else { 14505 return 0 14506 }}, {$$s: self}) 14507 }; 14508 14509 function iterate(max, buffer, self) { 14510 if (buffer.length == max) { 14511 var copy = buffer.slice(); 14512 Opal.yield1($yield, copy) 14513 return; 14514 } 14515 for (var i = 0; i < self.length; i++) { 14516 buffer.push(self[i]); 14517 iterate(max, buffer, self); 14518 buffer.pop(); 14519 } 14520 } 14521 14522 iterate(num, [], self.slice()); 14523 ; 14524 return self; 14525 }); 14526 14527 $def(self, '$pop', function $$pop(count) { 14528 var self = this; 14529 14530 14531 ; 14532 $deny_frozen_access(self); 14533 if ($truthy(count === undefined)) { 14534 14535 if ($truthy(self.length === 0)) { 14536 return nil 14537 }; 14538 return self.pop(); 14539 }; 14540 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14541 if ($truthy(count < 0)) { 14542 $Kernel.$raise($$$('ArgumentError'), "negative array size") 14543 }; 14544 if ($truthy(self.length === 0)) { 14545 return [] 14546 }; 14547 if ($truthy(count === 1)) { 14548 return [self.pop()]; 14549 } else if ($truthy(count > self.length)) { 14550 return self.splice(0, self.length); 14551 } else { 14552 return self.splice(self.length - count, self.length); 14553 }; 14554 }, -1); 14555 14556 $def(self, '$product', function $$product($a) { 14557 var block = $$product.$$p || nil, $post_args, args, self = this; 14558 14559 $$product.$$p = null; 14560 14561 ; 14562 $post_args = $slice(arguments); 14563 args = $post_args; 14564 14565 var result = (block !== nil) ? null : [], 14566 n = args.length + 1, 14567 counters = new Array(n), 14568 lengths = new Array(n), 14569 arrays = new Array(n), 14570 i, m, subarray, len, resultlen = 1; 14571 14572 arrays[0] = self; 14573 for (i = 1; i < n; i++) { 14574 arrays[i] = $coerce_to(args[i - 1], $$$('Array'), 'to_ary'); 14575 } 14576 14577 for (i = 0; i < n; i++) { 14578 len = arrays[i].length; 14579 if (len === 0) { 14580 return result || self; 14581 } 14582 resultlen *= len; 14583 if (resultlen > 2147483647) { 14584 $Kernel.$raise($$$('RangeError'), "too big to product") 14585 } 14586 lengths[i] = len; 14587 counters[i] = 0; 14588 } 14589 14590 outer_loop: for (;;) { 14591 subarray = []; 14592 for (i = 0; i < n; i++) { 14593 subarray.push(arrays[i][counters[i]]); 14594 } 14595 if (result) { 14596 result.push(subarray); 14597 } else { 14598 Opal.yield1(block, subarray) 14599 } 14600 m = n - 1; 14601 counters[m]++; 14602 while (counters[m] === lengths[m]) { 14603 counters[m] = 0; 14604 if (--m < 0) break outer_loop; 14605 counters[m]++; 14606 } 14607 } 14608 14609 return result || self; 14610 ; 14611 }, -1); 14612 14613 $def(self, '$push', function $$push($a) { 14614 var $post_args, objects, self = this; 14615 14616 14617 $post_args = $slice(arguments); 14618 objects = $post_args; 14619 14620 $deny_frozen_access(self); 14621 14622 for (var i = 0, length = objects.length; i < length; i++) { 14623 self.push(objects[i]); 14624 } 14625 ; 14626 return self; 14627 }, -1); 14628 14629 $def(self, '$rassoc', function $$rassoc(object) { 14630 var self = this; 14631 14632 14633 for (var i = 0, length = self.length, item; i < length; i++) { 14634 item = self[i]; 14635 14636 if (item.length && item[1] !== undefined) { 14637 if ((item[1])['$=='](object)) { 14638 return item; 14639 } 14640 } 14641 } 14642 14643 return nil; 14644 14645 }); 14646 14647 $def(self, '$reject', function $$reject() { 14648 var block = $$reject.$$p || nil, self = this; 14649 14650 $$reject.$$p = null; 14651 14652 ; 14653 if (!(block !== nil)) { 14654 return $send(self, 'enum_for', ["reject"], function $$36(){var self = $$36.$$s == null ? this : $$36.$$s; 14655 14656 return self.$size()}, {$$s: self}) 14657 }; 14658 14659 var result = []; 14660 14661 for (var i = 0, length = self.length, value; i < length; i++) { 14662 value = block(self[i]); 14663 14664 if (value === false || value === nil) { 14665 result.push(self[i]); 14666 } 14667 } 14668 return result; 14669 ; 14670 }); 14671 14672 $def(self, '$reject!', function $Array_reject$excl$37() { 14673 var block = $Array_reject$excl$37.$$p || nil, self = this, original = nil; 14674 14675 $Array_reject$excl$37.$$p = null; 14676 14677 ; 14678 if (!(block !== nil)) { 14679 return $send(self, 'enum_for', ["reject!"], function $$38(){var self = $$38.$$s == null ? this : $$38.$$s; 14680 14681 return self.$size()}, {$$s: self}) 14682 }; 14683 $deny_frozen_access(self); 14684 original = self.$length(); 14685 $send(self, 'delete_if', [], block.$to_proc()); 14686 if ($eqeq(self.$length(), original)) { 14687 return nil 14688 } else { 14689 return self 14690 }; 14691 }); 14692 14693 $def(self, '$replace', function $$replace(other) { 14694 var self = this; 14695 14696 14697 $deny_frozen_access(self); 14698 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 14699 14700 self.splice(0, self.length); 14701 self.push.apply(self, other); 14702 ; 14703 return self; 14704 }); 14705 14706 $def(self, '$reverse', function $$reverse() { 14707 var self = this; 14708 14709 return self.slice(0).reverse(); 14710 }); 14711 14712 $def(self, '$reverse!', function $Array_reverse$excl$39() { 14713 var self = this; 14714 14715 14716 $deny_frozen_access(self); 14717 return self.reverse();; 14718 }); 14719 14720 $def(self, '$reverse_each', function $$reverse_each() { 14721 var block = $$reverse_each.$$p || nil, self = this; 14722 14723 $$reverse_each.$$p = null; 14724 14725 ; 14726 if (!(block !== nil)) { 14727 return $send(self, 'enum_for', ["reverse_each"], function $$40(){var self = $$40.$$s == null ? this : $$40.$$s; 14728 14729 return self.$size()}, {$$s: self}) 14730 }; 14731 $send(self.$reverse(), 'each', [], block.$to_proc()); 14732 return self; 14733 }); 14734 14735 $def(self, '$rindex', function $$rindex(object) { 14736 var block = $$rindex.$$p || nil, self = this; 14737 14738 $$rindex.$$p = null; 14739 14740 ; 14741 ; 14742 14743 var i, value; 14744 14745 if (object != null && block !== nil) { 14746 self.$warn("warning: given block not used") 14747 } 14748 14749 if (object != null) { 14750 for (i = self.length - 1; i >= 0; i--) { 14751 if (i >= self.length) { 14752 break; 14753 } 14754 if ((self[i])['$=='](object)) { 14755 return i; 14756 } 14757 } 14758 } 14759 else if (block !== nil) { 14760 for (i = self.length - 1; i >= 0; i--) { 14761 if (i >= self.length) { 14762 break; 14763 } 14764 14765 value = block(self[i]); 14766 14767 if (value !== false && value !== nil) { 14768 return i; 14769 } 14770 } 14771 } 14772 else if (object == null) { 14773 return self.$enum_for("rindex"); 14774 } 14775 14776 return nil; 14777 ; 14778 }, -1); 14779 14780 $def(self, '$rotate', function $$rotate(n) { 14781 var self = this; 14782 14783 14784 if (n == null) n = 1; 14785 14786 var ary, idx, firstPart, lastPart; 14787 14788 n = $coerce_to(n, $$$('Integer'), 'to_int') 14789 14790 if (self.length === 1) { 14791 return self.slice(); 14792 } 14793 if (self.length === 0) { 14794 return []; 14795 } 14796 14797 ary = self.slice(); 14798 idx = n % ary.length; 14799 14800 firstPart = ary.slice(idx); 14801 lastPart = ary.slice(0, idx); 14802 return firstPart.concat(lastPart); 14803 ; 14804 }, -1); 14805 14806 $def(self, '$rotate!', function $Array_rotate$excl$41(cnt) { 14807 var self = this, ary = nil; 14808 14809 14810 if (cnt == null) cnt = 1; 14811 14812 $deny_frozen_access(self); 14813 14814 if (self.length === 0 || self.length === 1) { 14815 return self; 14816 } 14817 cnt = $coerce_to(cnt, $$$('Integer'), 'to_int'); 14818 ; 14819 ary = self.$rotate(cnt); 14820 return self.$replace(ary); 14821 }, -1); 14822 (function($base, $super) { 14823 var self = $klass($base, $super, 'SampleRandom'); 14824 14825 var $proto = self.$$prototype; 14826 14827 $proto.rng = nil; 14828 14829 14830 $def(self, '$initialize', $assign_ivar("rng")); 14831 return $def(self, '$rand', function $$rand(size) { 14832 var self = this, random = nil; 14833 14834 14835 random = $coerce_to(self.rng.$rand(size), $$$('Integer'), 'to_int'); 14836 if ($truthy(random < 0)) { 14837 $Kernel.$raise($$$('RangeError'), "random value must be >= 0") 14838 }; 14839 if (!$truthy(random < size)) { 14840 $Kernel.$raise($$$('RangeError'), "random value must be less than Array size") 14841 }; 14842 return random; 14843 }); 14844 })(self, null); 14845 14846 $def(self, '$sample', function $$sample(count, options) { 14847 var self = this, o = nil, rng = nil; 14848 14849 14850 ; 14851 ; 14852 if ($truthy(count === undefined)) { 14853 return self.$at($Kernel.$rand(self.length)) 14854 }; 14855 if ($truthy(options === undefined)) { 14856 if ($truthy((o = $Opal['$coerce_to?'](count, $$$('Hash'), "to_hash")))) { 14857 14858 options = o; 14859 count = nil; 14860 } else { 14861 14862 options = nil; 14863 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14864 } 14865 } else { 14866 14867 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14868 options = $coerce_to(options, $$$('Hash'), 'to_hash'); 14869 }; 14870 if (($truthy(count) && ($truthy(count < 0)))) { 14871 $Kernel.$raise($$$('ArgumentError'), "count must be greater than 0") 14872 }; 14873 if ($truthy(options)) { 14874 rng = options['$[]']("random") 14875 }; 14876 rng = (($truthy(rng) && ($truthy(rng['$respond_to?']("rand")))) ? ($$('SampleRandom').$new(rng)) : ($Kernel)); 14877 if (!$truthy(count)) { 14878 return self[rng.$rand(self.length)] 14879 }; 14880 14881 14882 var abandon, spin, result, i, j, k, targetIndex, oldValue; 14883 14884 if (count > self.length) { 14885 count = self.length; 14886 } 14887 14888 switch (count) { 14889 case 0: 14890 return []; 14891 break; 14892 case 1: 14893 return [self[rng.$rand(self.length)]]; 14894 break; 14895 case 2: 14896 i = rng.$rand(self.length); 14897 j = rng.$rand(self.length - 1); 14898 if (i <= j) { 14899 j++; 14900 } 14901 return [self[i], self[j]]; 14902 break; 14903 default: 14904 if (self.length / count > 3) { 14905 abandon = false; 14906 spin = 0; 14907 14908 result = $$('Array').$new(count); 14909 i = 1; 14910 14911 result[0] = rng.$rand(self.length); 14912 while (i < count) { 14913 k = rng.$rand(self.length); 14914 j = 0; 14915 14916 while (j < i) { 14917 while (k === result[j]) { 14918 spin++; 14919 if (spin > 100) { 14920 abandon = true; 14921 break; 14922 } 14923 k = rng.$rand(self.length); 14924 } 14925 if (abandon) { break; } 14926 14927 j++; 14928 } 14929 14930 if (abandon) { break; } 14931 14932 result[i] = k; 14933 14934 i++; 14935 } 14936 14937 if (!abandon) { 14938 i = 0; 14939 while (i < count) { 14940 result[i] = self[result[i]]; 14941 i++; 14942 } 14943 14944 return result; 14945 } 14946 } 14947 14948 result = self.slice(); 14949 14950 for (var c = 0; c < count; c++) { 14951 targetIndex = rng.$rand(self.length - c) + c; 14952 oldValue = result[c]; 14953 result[c] = result[targetIndex]; 14954 result[targetIndex] = oldValue; 14955 } 14956 14957 return count === self.length ? result : (result)['$[]'](0, count); 14958 } 14959 ; 14960 }, -1); 14961 14962 $def(self, '$select', function $$select() { 14963 var block = $$select.$$p || nil, self = this; 14964 14965 $$select.$$p = null; 14966 14967 ; 14968 if (!(block !== nil)) { 14969 return $send(self, 'enum_for', ["select"], function $$42(){var self = $$42.$$s == null ? this : $$42.$$s; 14970 14971 return self.$size()}, {$$s: self}) 14972 }; 14973 14974 var result = []; 14975 14976 for (var i = 0, length = self.length, item, value; i < length; i++) { 14977 item = self[i]; 14978 14979 value = $yield1(block, item); 14980 14981 if ($truthy(value)) { 14982 result.push(item); 14983 } 14984 } 14985 14986 return result; 14987 ; 14988 }); 14989 14990 $def(self, '$select!', function $Array_select$excl$43() { 14991 var block = $Array_select$excl$43.$$p || nil, self = this; 14992 14993 $Array_select$excl$43.$$p = null; 14994 14995 ; 14996 if (!(block !== nil)) { 14997 return $send(self, 'enum_for', ["select!"], function $$44(){var self = $$44.$$s == null ? this : $$44.$$s; 14998 14999 return self.$size()}, {$$s: self}) 15000 }; 15001 15002 $deny_frozen_access(self) 15003 15004 var original = self.length; 15005 $send(self, 'keep_if', [], block.$to_proc()); 15006 return self.length === original ? nil : self; 15007 ; 15008 }); 15009 15010 $def(self, '$shift', function $$shift(count) { 15011 var self = this; 15012 15013 15014 ; 15015 $deny_frozen_access(self); 15016 if ($truthy(count === undefined)) { 15017 15018 if ($truthy(self.length === 0)) { 15019 return nil 15020 }; 15021 return shiftNoArg(self); 15022 }; 15023 count = $coerce_to(count, $$$('Integer'), 'to_int'); 15024 if ($truthy(count < 0)) { 15025 $Kernel.$raise($$$('ArgumentError'), "negative array size") 15026 }; 15027 if ($truthy(self.length === 0)) { 15028 return [] 15029 }; 15030 return self.splice(0, count);; 15031 }, -1); 15032 15033 $def(self, '$shuffle', function $$shuffle(rng) { 15034 var self = this; 15035 15036 15037 ; 15038 return self.$dup().$to_a()['$shuffle!'](rng); 15039 }, -1); 15040 15041 $def(self, '$shuffle!', function $Array_shuffle$excl$45(rng) { 15042 var self = this; 15043 15044 15045 ; 15046 15047 $deny_frozen_access(self); 15048 15049 var randgen, i = self.length, j, tmp; 15050 15051 if (rng !== undefined) { 15052 rng = $Opal['$coerce_to?'](rng, $$$('Hash'), "to_hash"); 15053 15054 if (rng !== nil) { 15055 rng = rng['$[]']("random"); 15056 15057 if (rng !== nil && rng['$respond_to?']("rand")) { 15058 randgen = rng; 15059 } 15060 } 15061 } 15062 15063 while (i) { 15064 if (randgen) { 15065 j = randgen.$rand(i).$to_int(); 15066 15067 if (j < 0) { 15068 $Kernel.$raise($$$('RangeError'), "random number too small " + (j)) 15069 } 15070 15071 if (j >= i) { 15072 $Kernel.$raise($$$('RangeError'), "random number too big " + (j)) 15073 } 15074 } 15075 else { 15076 j = self.$rand(i); 15077 } 15078 15079 tmp = self[--i]; 15080 self[i] = self[j]; 15081 self[j] = tmp; 15082 } 15083 15084 return self; 15085 ; 15086 }, -1); 15087 15088 $def(self, '$slice!', function $Array_slice$excl$46(index, length) { 15089 var self = this, result = nil, range = nil, range_start = nil, range_end = nil, start = nil; 15090 15091 15092 ; 15093 $deny_frozen_access(self); 15094 result = nil; 15095 if ($truthy(length === undefined)) { 15096 if ($eqeqeq($$$('Range'), index)) { 15097 15098 range = index; 15099 result = self['$[]'](range); 15100 range_start = range.begin === nil ? 0 : $coerce_to(range.begin, $$$('Integer'), 'to_int'); 15101 range_end = range.end === nil ? -1 : $coerce_to(range.end, $$$('Integer'), 'to_int'); 15102 15103 if (range_start < 0) { 15104 range_start += self.length; 15105 } 15106 15107 if (range_end < 0) { 15108 range_end += self.length; 15109 } else if (range_end >= self.length) { 15110 range_end = self.length - 1; 15111 if (range.excl) { 15112 range_end += 1; 15113 } 15114 } 15115 15116 var range_length = range_end - range_start; 15117 if (range.excl && range.end !== nil) { 15118 range_end -= 1; 15119 } else { 15120 range_length += 1; 15121 } 15122 15123 if (range_start < self.length && range_start >= 0 && range_end < self.length && range_end >= 0 && range_length > 0) { 15124 self.splice(range_start, range_length); 15125 } 15126 ; 15127 } else { 15128 15129 start = $coerce_to(index, $$$('Integer'), 'to_int'); 15130 15131 if (start < 0) { 15132 start += self.length; 15133 } 15134 15135 if (start < 0 || start >= self.length) { 15136 return nil; 15137 } 15138 15139 result = self[start]; 15140 15141 if (start === 0) { 15142 self.shift(); 15143 } else { 15144 self.splice(start, 1); 15145 } 15146 ; 15147 } 15148 } else { 15149 15150 start = $coerce_to(index, $$$('Integer'), 'to_int'); 15151 length = $coerce_to(length, $$$('Integer'), 'to_int'); 15152 15153 if (length < 0) { 15154 return nil; 15155 } 15156 15157 var end = start + length; 15158 15159 result = self['$[]'](start, length); 15160 15161 if (start < 0) { 15162 start += self.length; 15163 } 15164 15165 if (start + length > self.length) { 15166 length = self.length - start; 15167 } 15168 15169 if (start < self.length && start >= 0) { 15170 self.splice(start, length); 15171 } 15172 ; 15173 }; 15174 return result; 15175 }, -2); 15176 15177 $def(self, '$sort', function $$sort() { 15178 var block = $$sort.$$p || nil, self = this; 15179 15180 $$sort.$$p = null; 15181 15182 ; 15183 if (!$truthy(self.length > 1)) { 15184 return self 15185 }; 15186 15187 if (block === nil) { 15188 block = function(a, b) { 15189 return (a)['$<=>'](b); 15190 }; 15191 } 15192 15193 return self.slice().sort(function(x, y) { 15194 var ret = block(x, y); 15195 15196 if (ret === nil) { 15197 $Kernel.$raise($$$('ArgumentError'), "comparison of " + ((x).$inspect()) + " with " + ((y).$inspect()) + " failed"); 15198 } 15199 15200 return $rb_gt(ret, 0) ? 1 : ($rb_lt(ret, 0) ? -1 : 0); 15201 }); 15202 ; 15203 }); 15204 15205 $def(self, '$sort!', function $Array_sort$excl$47() { 15206 var block = $Array_sort$excl$47.$$p || nil, self = this; 15207 15208 $Array_sort$excl$47.$$p = null; 15209 15210 ; 15211 15212 $deny_frozen_access(self) 15213 15214 var result; 15215 15216 if ((block !== nil)) { 15217 result = $send((self.slice()), 'sort', [], block.$to_proc()); 15218 } 15219 else { 15220 result = (self.slice()).$sort(); 15221 } 15222 15223 self.length = 0; 15224 for(var i = 0, length = result.length; i < length; i++) { 15225 self.push(result[i]); 15226 } 15227 15228 return self; 15229 ; 15230 }); 15231 15232 $def(self, '$sort_by!', function $Array_sort_by$excl$48() { 15233 var block = $Array_sort_by$excl$48.$$p || nil, self = this; 15234 15235 $Array_sort_by$excl$48.$$p = null; 15236 15237 ; 15238 if (!(block !== nil)) { 15239 return $send(self, 'enum_for', ["sort_by!"], function $$49(){var self = $$49.$$s == null ? this : $$49.$$s; 15240 15241 return self.$size()}, {$$s: self}) 15242 }; 15243 $deny_frozen_access(self); 15244 return self.$replace($send(self, 'sort_by', [], block.$to_proc())); 15245 }); 15246 15247 $def(self, '$take', function $$take(count) { 15248 var self = this; 15249 15250 15251 if (count < 0) { 15252 $Kernel.$raise($$$('ArgumentError')); 15253 } 15254 15255 return self.slice(0, count); 15256 15257 }); 15258 15259 $def(self, '$take_while', function $$take_while() { 15260 var block = $$take_while.$$p || nil, self = this; 15261 15262 $$take_while.$$p = null; 15263 15264 ; 15265 15266 var result = []; 15267 15268 for (var i = 0, length = self.length, item, value; i < length; i++) { 15269 item = self[i]; 15270 15271 value = block(item); 15272 15273 if (value === false || value === nil) { 15274 return result; 15275 } 15276 15277 result.push(item); 15278 } 15279 15280 return result; 15281 ; 15282 }); 15283 15284 $def(self, '$to_a', function $$to_a() { 15285 var self = this; 15286 15287 15288 if (self.$$class === Opal.Array) { 15289 return self; 15290 } 15291 else { 15292 return Opal.Array.$new(self); 15293 } 15294 15295 }); 15296 15297 $def(self, '$to_ary', $return_self); 15298 15299 $def(self, '$to_h', function $$to_h() { 15300 var block = $$to_h.$$p || nil, self = this, array = nil; 15301 15302 $$to_h.$$p = null; 15303 15304 ; 15305 array = self; 15306 if ((block !== nil)) { 15307 array = $send(array, 'map', [], block.$to_proc()) 15308 }; 15309 15310 var i, len = array.length, ary, key, val, hash = $hash2([], {}); 15311 15312 for (i = 0; i < len; i++) { 15313 ary = $Opal['$coerce_to?'](array[i], $$$('Array'), "to_ary"); 15314 if (!ary.$$is_array) { 15315 $Kernel.$raise($$$('TypeError'), "wrong element type " + ((ary).$class()) + " at " + (i) + " (expected array)") 15316 } 15317 if (ary.length !== 2) { 15318 $Kernel.$raise($$$('ArgumentError'), "wrong array length at " + (i) + " (expected 2, was " + ((ary).$length()) + ")") 15319 } 15320 key = ary[0]; 15321 val = ary[1]; 15322 $hash_put(hash, key, val); 15323 } 15324 15325 return hash; 15326 ; 15327 }); 15328 15329 $def(self, '$transpose', function $$transpose() { 15330 var self = this, result = nil, max = nil; 15331 15332 15333 if ($truthy(self['$empty?']())) { 15334 return [] 15335 }; 15336 result = []; 15337 max = nil; 15338 $send(self, 'each', [], function $$50(row){var $ret_or_1 = nil; 15339 15340 15341 if (row == null) row = nil; 15342 row = ($eqeqeq($$$('Array'), row) ? (row.$to_a()) : (($coerce_to(row, $$$('Array'), 'to_ary')).$to_a())); 15343 max = ($truthy(($ret_or_1 = max)) ? ($ret_or_1) : (row.length)); 15344 if ($neqeq(row.length, max)) { 15345 $Kernel.$raise($$$('IndexError'), "element size differs (" + (row.length) + " should be " + (max) + ")") 15346 }; 15347 return $send((row.length), 'times', [], function $$51(i){var $a, entry = nil; 15348 15349 15350 if (i == null) i = nil; 15351 entry = ($truthy(($ret_or_1 = result['$[]'](i))) ? ($ret_or_1) : (($a = [i, []], $send(result, '[]=', $a), $a[$a.length - 1]))); 15352 return entry['$<<'](row.$at(i));});}); 15353 return result; 15354 }); 15355 15356 $def(self, '$union', function $$union($a) { 15357 var $post_args, arrays, self = this; 15358 15359 15360 $post_args = $slice(arguments); 15361 arrays = $post_args; 15362 return $send(arrays, 'reduce', [self.$uniq()], function $$52(a, b){ 15363 15364 if (a == null) a = nil; 15365 if (b == null) b = nil; 15366 return a['$|'](b);}); 15367 }, -1); 15368 15369 $def(self, '$uniq', function $$uniq() { 15370 var block = $$uniq.$$p || nil, self = this; 15371 15372 $$uniq.$$p = null; 15373 15374 ; 15375 15376 var hash = $hash2([], {}), i, length, item, key; 15377 15378 if (block === nil) { 15379 for (i = 0, length = self.length; i < length; i++) { 15380 item = self[i]; 15381 if ($hash_get(hash, item) === undefined) { 15382 $hash_put(hash, item, item); 15383 } 15384 } 15385 } 15386 else { 15387 for (i = 0, length = self.length; i < length; i++) { 15388 item = self[i]; 15389 key = $yield1(block, item); 15390 if ($hash_get(hash, key) === undefined) { 15391 $hash_put(hash, key, item); 15392 } 15393 } 15394 } 15395 15396 return (hash).$values(); 15397 ; 15398 }); 15399 15400 $def(self, '$uniq!', function $Array_uniq$excl$53() { 15401 var block = $Array_uniq$excl$53.$$p || nil, self = this; 15402 15403 $Array_uniq$excl$53.$$p = null; 15404 15405 ; 15406 15407 $deny_frozen_access(self); 15408 15409 var original_length = self.length, hash = $hash2([], {}), i, length, item, key; 15410 15411 for (i = 0, length = original_length; i < length; i++) { 15412 item = self[i]; 15413 key = (block === nil ? item : $yield1(block, item)); 15414 15415 if ($hash_get(hash, key) === undefined) { 15416 $hash_put(hash, key, item); 15417 continue; 15418 } 15419 15420 self.splice(i, 1); 15421 length--; 15422 i--; 15423 } 15424 15425 return self.length === original_length ? nil : self; 15426 ; 15427 }); 15428 15429 $def(self, '$unshift', function $$unshift($a) { 15430 var $post_args, objects, self = this; 15431 15432 15433 $post_args = $slice(arguments); 15434 objects = $post_args; 15435 15436 $deny_frozen_access(self); 15437 15438 var selfLength = self.length 15439 var objectsLength = objects.length 15440 if (objectsLength == 0) return self; 15441 var index = selfLength - objectsLength 15442 for (var i = 0; i < objectsLength; i++) { 15443 self.push(self[index + i]) 15444 } 15445 var len = selfLength - 1 15446 while (len - objectsLength >= 0) { 15447 self[len] = self[len - objectsLength] 15448 len-- 15449 } 15450 for (var j = 0; j < objectsLength; j++) { 15451 self[j] = objects[j] 15452 } 15453 return self; 15454 ; 15455 }, -1); 15456 15457 $def(self, '$values_at', function $$values_at($a) { 15458 var $post_args, args, self = this, out = nil; 15459 15460 15461 $post_args = $slice(arguments); 15462 args = $post_args; 15463 out = []; 15464 $send(args, 'each', [], function $$54(elem){var self = $$54.$$s == null ? this : $$54.$$s, finish = nil, start = nil, i = nil; 15465 15466 15467 if (elem == null) elem = nil; 15468 if ($truthy(elem['$is_a?']($$$('Range')))) { 15469 15470 finish = elem.$end() === nil ? -1 : $coerce_to(elem.$end(), $$$('Integer'), 'to_int'); 15471 start = elem.$begin() === nil ? 0 : $coerce_to(elem.$begin(), $$$('Integer'), 'to_int'); 15472 15473 if (start < 0) { 15474 start = start + self.length; 15475 return nil; 15476 } 15477 ; 15478 15479 if (finish < 0) { 15480 finish = finish + self.length; 15481 } 15482 if (elem['$exclude_end?']() && elem.$end() !== nil) { 15483 finish--; 15484 } 15485 if (finish < start) { 15486 return nil; 15487 } 15488 ; 15489 return $send(start, 'upto', [finish], function $$55(i){var self = $$55.$$s == null ? this : $$55.$$s; 15490 15491 15492 if (i == null) i = nil; 15493 return out['$<<'](self.$at(i));}, {$$s: self}); 15494 } else { 15495 15496 i = $coerce_to(elem, $$$('Integer'), 'to_int'); 15497 return out['$<<'](self.$at(i)); 15498 };}, {$$s: self}); 15499 return out; 15500 }, -1); 15501 15502 $def(self, '$zip', function $$zip($a) { 15503 var block = $$zip.$$p || nil, $post_args, others, self = this, $ret_or_1 = nil; 15504 15505 $$zip.$$p = null; 15506 15507 ; 15508 $post_args = $slice(arguments); 15509 others = $post_args; 15510 15511 var result = [], size = self.length, part, o, i, j, jj; 15512 15513 for (j = 0, jj = others.length; j < jj; j++) { 15514 o = others[j]; 15515 if (o.$$is_array) { 15516 continue; 15517 } 15518 if (o.$$is_range || o.$$is_enumerator) { 15519 others[j] = o.$take(size); 15520 continue; 15521 } 15522 others[j] = ($truthy(($ret_or_1 = $Opal['$coerce_to?'](o, $$$('Array'), "to_ary"))) ? ($ret_or_1) : ($Opal['$coerce_to!'](o, $$$('Enumerator'), "to_enum", "each"))).$to_a(); 15523 } 15524 15525 for (i = 0; i < size; i++) { 15526 part = [self[i]]; 15527 15528 for (j = 0, jj = others.length; j < jj; j++) { 15529 o = others[j][i]; 15530 15531 if (o == null) { 15532 o = nil; 15533 } 15534 15535 part[j + 1] = o; 15536 } 15537 15538 result[i] = part; 15539 } 15540 15541 if (block !== nil) { 15542 for (i = 0; i < size; i++) { 15543 Opal.yield1(block, result[i]); 15544 } 15545 15546 return nil; 15547 } 15548 15549 return result; 15550 ; 15551 }, -1); 15552 $defs(self, '$inherited', function $$inherited(klass) { 15553 15554 15555 klass.$$prototype.$to_a = function() { 15556 return this.slice(0, this.length); 15557 } 15558 15559 }); 15560 15561 $def(self, '$instance_variables', function $$instance_variables() { 15562 var $yield = $$instance_variables.$$p || nil, self = this; 15563 15564 $$instance_variables.$$p = null; 15565 return $send($send2(self, $find_super(self, 'instance_variables', $$instance_variables, false, true), 'instance_variables', [], $yield), 'reject', [], function $$56(ivar){var $ret_or_1 = nil; 15566 15567 15568 if (ivar == null) ivar = nil; 15569 if ($truthy(($ret_or_1 = /^@\d+$/.test(ivar)))) { 15570 return $ret_or_1 15571 } else { 15572 return ivar['$==']("@length") 15573 };}) 15574 }); 15575 15576 $def(self, '$pack', function $$pack($a) { 15577 var $post_args, args; 15578 15579 15580 $post_args = $slice(arguments); 15581 args = $post_args; 15582 return $Kernel.$raise("To use Array#pack, you must first require 'corelib/array/pack'."); 15583 }, -1); 15584 $alias(self, "append", "push"); 15585 $alias(self, "filter", "select"); 15586 $alias(self, "filter!", "select!"); 15587 $alias(self, "map", "collect"); 15588 $alias(self, "map!", "collect!"); 15589 $alias(self, "prepend", "unshift"); 15590 $alias(self, "size", "length"); 15591 $alias(self, "slice", "[]"); 15592 $alias(self, "to_s", "inspect"); 15593 $Opal.$pristine(self.$singleton_class(), "allocate"); 15594 return $Opal.$pristine(self, "copy_instance_variables", "initialize_dup"); 15595 })('::', Array, $nesting); 15596}; 15597 15598Opal.modules["corelib/hash"] = function(Opal) {/* Generated by Opal 1.7.3 */ 15599 var $yield1 = Opal.yield1, $hash = Opal.hash, $hash_init = Opal.hash_init, $hash_get = Opal.hash_get, $hash_put = Opal.hash_put, $hash_delete = Opal.hash_delete, $deny_frozen_access = Opal.deny_frozen_access, $freeze = Opal.freeze, $klass = Opal.klass, $slice = Opal.slice, $Opal = Opal.Opal, $Kernel = Opal.Kernel, $defs = Opal.defs, $def = Opal.def, $send = Opal.send, $rb_ge = Opal.rb_ge, $rb_gt = Opal.rb_gt, $hash2 = Opal.hash2, $truthy = Opal.truthy, $to_a = Opal.to_a, $return_self = Opal.return_self, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 15600 15601 Opal.add_stubs('require,include,coerce_to?,[],merge!,allocate,raise,coerce_to!,each,fetch,>=,>,==,compare_by_identity,lambda?,abs,arity,enum_for,size,respond_to?,class,dig,except!,dup,delete,new,inspect,map,to_proc,flatten,frozen?,eql?,default,default_proc,default_proc=,default=,to_h,proc,clone,select,select!,has_key?,indexes,index,length,[]=,has_value?'); 15602 15603 self.$require("corelib/enumerable"); 15604 return (function($base, $super, $parent_nesting) { 15605 var self = $klass($base, $super, 'Hash'); 15606 15607 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 15608 15609 15610 self.$include($$$('Enumerable')); 15611 self.$$prototype.$$is_hash = true; 15612 $defs(self, '$[]', function $Hash_$$$1($a) { 15613 var $post_args, argv, self = this; 15614 15615 15616 $post_args = $slice(arguments); 15617 argv = $post_args; 15618 15619 var hash, argc = argv.length, i; 15620 15621 if (argc === 1) { 15622 hash = $Opal['$coerce_to?'](argv['$[]'](0), $$$('Hash'), "to_hash"); 15623 if (hash !== nil) { 15624 return self.$allocate()['$merge!'](hash); 15625 } 15626 15627 argv = $Opal['$coerce_to?'](argv['$[]'](0), $$$('Array'), "to_ary"); 15628 if (argv === nil) { 15629 $Kernel.$raise($$$('ArgumentError'), "odd number of arguments for Hash") 15630 } 15631 15632 argc = argv.length; 15633 hash = self.$allocate(); 15634 15635 for (i = 0; i < argc; i++) { 15636 if (!argv[i].$$is_array) continue; 15637 switch(argv[i].length) { 15638 case 1: 15639 hash.$store(argv[i][0], nil); 15640 break; 15641 case 2: 15642 hash.$store(argv[i][0], argv[i][1]); 15643 break; 15644 default: 15645 $Kernel.$raise($$$('ArgumentError'), "invalid number of elements (" + (argv[i].length) + " for 1..2)") 15646 } 15647 } 15648 15649 return hash; 15650 } 15651 15652 if (argc % 2 !== 0) { 15653 $Kernel.$raise($$$('ArgumentError'), "odd number of arguments for Hash") 15654 } 15655 15656 hash = self.$allocate(); 15657 15658 for (i = 0; i < argc; i += 2) { 15659 hash.$store(argv[i], argv[i + 1]); 15660 } 15661 15662 return hash; 15663 ; 15664 }, -1); 15665 $defs(self, '$allocate', function $$allocate() { 15666 var self = this; 15667 15668 15669 var hash = new self.$$constructor(); 15670 15671 $hash_init(hash); 15672 15673 hash.$$none = nil; 15674 hash.$$proc = nil; 15675 15676 return hash; 15677 15678 }); 15679 $defs(self, '$try_convert', function $$try_convert(obj) { 15680 15681 return $Opal['$coerce_to?'](obj, $$$('Hash'), "to_hash") 15682 }); 15683 15684 $def(self, '$initialize', function $$initialize(defaults) { 15685 var block = $$initialize.$$p || nil, self = this; 15686 15687 $$initialize.$$p = null; 15688 15689 ; 15690 ; 15691 15692 $deny_frozen_access(self); 15693 15694 if (defaults !== undefined && block !== nil) { 15695 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (1 for 0)") 15696 } 15697 self.$$none = (defaults === undefined ? nil : defaults); 15698 self.$$proc = block; 15699 15700 return self; 15701 ; 15702 }, -1); 15703 15704 $def(self, '$==', function $Hash_$eq_eq$2(other) { 15705 var self = this; 15706 15707 15708 if (self === other) { 15709 return true; 15710 } 15711 15712 if (!other.$$is_hash) { 15713 return false; 15714 } 15715 15716 if (self.$$keys.length !== other.$$keys.length) { 15717 return false; 15718 } 15719 15720 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, other_value; i < length; i++) { 15721 key = keys[i]; 15722 15723 if (key.$$is_string) { 15724 value = self.$$smap[key]; 15725 other_value = other.$$smap[key]; 15726 } else { 15727 value = key.value; 15728 other_value = $hash_get(other, key.key); 15729 } 15730 15731 if (other_value === undefined || !value['$eql?'](other_value)) { 15732 return false; 15733 } 15734 } 15735 15736 return true; 15737 15738 }); 15739 15740 $def(self, '$>=', function $Hash_$gt_eq$3(other) { 15741 var self = this, result = nil; 15742 15743 15744 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 15745 15746 if (self.$$keys.length < other.$$keys.length) { 15747 return false 15748 } 15749 ; 15750 result = true; 15751 $send(other, 'each', [], function $$4(other_key, other_val){var self = $$4.$$s == null ? this : $$4.$$s, val = nil; 15752 15753 15754 if (other_key == null) other_key = nil; 15755 if (other_val == null) other_val = nil; 15756 val = self.$fetch(other_key, null); 15757 15758 if (val == null || val !== other_val) { 15759 result = false; 15760 return; 15761 } 15762 ;}, {$$s: self}); 15763 return result; 15764 }); 15765 15766 $def(self, '$>', function $Hash_$gt$5(other) { 15767 var self = this; 15768 15769 15770 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 15771 15772 if (self.$$keys.length <= other.$$keys.length) { 15773 return false 15774 } 15775 ; 15776 return $rb_ge(self, other); 15777 }); 15778 15779 $def(self, '$<', function $Hash_$lt$6(other) { 15780 var self = this; 15781 15782 15783 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 15784 return $rb_gt(other, self); 15785 }); 15786 15787 $def(self, '$<=', function $Hash_$lt_eq$7(other) { 15788 var self = this; 15789 15790 15791 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 15792 return $rb_ge(other, self); 15793 }); 15794 15795 $def(self, '$[]', function $Hash_$$$8(key) { 15796 var self = this; 15797 15798 15799 var value = $hash_get(self, key); 15800 15801 if (value !== undefined) { 15802 return value; 15803 } 15804 15805 return self.$default(key); 15806 15807 }); 15808 15809 $def(self, '$[]=', function $Hash_$$$eq$9(key, value) { 15810 var self = this; 15811 15812 15813 $deny_frozen_access(self); 15814 15815 $hash_put(self, key, value); 15816 return value; 15817 15818 }); 15819 15820 $def(self, '$assoc', function $$assoc(object) { 15821 var self = this; 15822 15823 15824 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 15825 key = keys[i]; 15826 15827 if (key.$$is_string) { 15828 if ((key)['$=='](object)) { 15829 return [key, self.$$smap[key]]; 15830 } 15831 } else { 15832 if ((key.key)['$=='](object)) { 15833 return [key.key, key.value]; 15834 } 15835 } 15836 } 15837 15838 return nil; 15839 15840 }); 15841 15842 $def(self, '$clear', function $$clear() { 15843 var self = this; 15844 15845 15846 $deny_frozen_access(self); 15847 15848 $hash_init(self); 15849 return self; 15850 15851 }); 15852 15853 $def(self, '$clone', function $$clone() { 15854 var self = this; 15855 15856 15857 var hash = new self.$$class(); 15858 15859 $hash_init(hash); 15860 Opal.hash_clone(self, hash); 15861 15862 return hash; 15863 15864 }); 15865 15866 $def(self, '$compact', function $$compact() { 15867 var self = this; 15868 15869 15870 var hash = $hash(); 15871 15872 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15873 key = keys[i]; 15874 15875 if (key.$$is_string) { 15876 value = self.$$smap[key]; 15877 } else { 15878 value = key.value; 15879 key = key.key; 15880 } 15881 15882 if (value !== nil) { 15883 $hash_put(hash, key, value); 15884 } 15885 } 15886 15887 return hash; 15888 15889 }); 15890 15891 $def(self, '$compact!', function $Hash_compact$excl$10() { 15892 var self = this; 15893 15894 15895 $deny_frozen_access(self); 15896 15897 var changes_were_made = false; 15898 15899 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15900 key = keys[i]; 15901 15902 if (key.$$is_string) { 15903 value = self.$$smap[key]; 15904 } else { 15905 value = key.value; 15906 key = key.key; 15907 } 15908 15909 if (value === nil) { 15910 if ($hash_delete(self, key) !== undefined) { 15911 changes_were_made = true; 15912 length--; 15913 i--; 15914 } 15915 } 15916 } 15917 15918 return changes_were_made ? self : nil; 15919 15920 }); 15921 15922 $def(self, '$compare_by_identity', function $$compare_by_identity() { 15923 var self = this; 15924 15925 15926 $deny_frozen_access(self); 15927 15928 var i, ii, key, keys = self.$$keys, identity_hash; 15929 15930 if (self.$$by_identity) return self; 15931 if (self.$$keys.length === 0) { 15932 self.$$by_identity = true 15933 return self; 15934 } 15935 15936 identity_hash = $hash2([], {}).$compare_by_identity(); 15937 for(i = 0, ii = keys.length; i < ii; i++) { 15938 key = keys[i]; 15939 if (!key.$$is_string) key = key.key; 15940 $hash_put(identity_hash, key, $hash_get(self, key)); 15941 } 15942 15943 self.$$by_identity = true; 15944 self.$$map = identity_hash.$$map; 15945 self.$$smap = identity_hash.$$smap; 15946 return self; 15947 15948 }); 15949 15950 $def(self, '$compare_by_identity?', function $Hash_compare_by_identity$ques$11() { 15951 var self = this; 15952 15953 return self.$$by_identity === true; 15954 }); 15955 15956 $def(self, '$default', function $Hash_default$12(key) { 15957 var self = this; 15958 15959 15960 ; 15961 15962 if (key !== undefined && self.$$proc !== nil && self.$$proc !== undefined) { 15963 return self.$$proc.$call(self, key); 15964 } 15965 if (self.$$none === undefined) { 15966 return nil; 15967 } 15968 return self.$$none; 15969 ; 15970 }, -1); 15971 15972 $def(self, '$default=', function $Hash_default$eq$13(object) { 15973 var self = this; 15974 15975 15976 $deny_frozen_access(self); 15977 15978 self.$$proc = nil; 15979 self.$$none = object; 15980 15981 return object; 15982 15983 }); 15984 15985 $def(self, '$default_proc', function $$default_proc() { 15986 var self = this; 15987 15988 15989 if (self.$$proc !== undefined) { 15990 return self.$$proc; 15991 } 15992 return nil; 15993 15994 }); 15995 15996 $def(self, '$default_proc=', function $Hash_default_proc$eq$14(default_proc) { 15997 var self = this; 15998 15999 16000 $deny_frozen_access(self); 16001 16002 var proc = default_proc; 16003 16004 if (proc !== nil) { 16005 proc = $Opal['$coerce_to!'](proc, $$$('Proc'), "to_proc"); 16006 16007 if ((proc)['$lambda?']() && (proc).$arity().$abs() !== 2) { 16008 $Kernel.$raise($$$('TypeError'), "default_proc takes two arguments"); 16009 } 16010 } 16011 16012 self.$$none = nil; 16013 self.$$proc = proc; 16014 16015 return default_proc; 16016 16017 }); 16018 16019 $def(self, '$delete', function $Hash_delete$15(key) { 16020 var block = $Hash_delete$15.$$p || nil, self = this; 16021 16022 $Hash_delete$15.$$p = null; 16023 16024 ; 16025 16026 $deny_frozen_access(self); 16027 var value = $hash_delete(self, key); 16028 16029 if (value !== undefined) { 16030 return value; 16031 } 16032 16033 if (block !== nil) { 16034 return Opal.yield1(block, key); 16035 } 16036 16037 return nil; 16038 ; 16039 }); 16040 16041 $def(self, '$delete_if', function $$delete_if() { 16042 var block = $$delete_if.$$p || nil, self = this; 16043 16044 $$delete_if.$$p = null; 16045 16046 ; 16047 if (!$truthy(block)) { 16048 return $send(self, 'enum_for', ["delete_if"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 16049 16050 return self.$size()}, {$$s: self}) 16051 }; 16052 16053 $deny_frozen_access(self); 16054 16055 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16056 key = keys[i]; 16057 16058 if (key.$$is_string) { 16059 value = self.$$smap[key]; 16060 } else { 16061 value = key.value; 16062 key = key.key; 16063 } 16064 16065 obj = block(key, value); 16066 16067 if (obj !== false && obj !== nil) { 16068 if ($hash_delete(self, key) !== undefined) { 16069 length--; 16070 i--; 16071 } 16072 } 16073 } 16074 16075 return self; 16076 ; 16077 }); 16078 16079 $def(self, '$dig', function $$dig(key, $a) { 16080 var $post_args, keys, self = this, item = nil; 16081 16082 16083 $post_args = $slice(arguments, 1); 16084 keys = $post_args; 16085 item = self['$[]'](key); 16086 16087 if (item === nil || keys.length === 0) { 16088 return item; 16089 } 16090 ; 16091 if (!$truthy(item['$respond_to?']("dig"))) { 16092 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method") 16093 }; 16094 return $send(item, 'dig', $to_a(keys)); 16095 }, -2); 16096 16097 $def(self, '$each', function $$each() { 16098 var block = $$each.$$p || nil, self = this; 16099 16100 $$each.$$p = null; 16101 16102 ; 16103 if (!$truthy(block)) { 16104 return $send(self, 'enum_for', ["each"], function $$17(){var self = $$17.$$s == null ? this : $$17.$$s; 16105 16106 return self.$size()}, {$$s: self}) 16107 }; 16108 16109 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key, value; i < length; i++) { 16110 key = keys[i]; 16111 16112 if (key.$$is_string) { 16113 value = self.$$smap[key]; 16114 } else { 16115 value = key.value; 16116 key = key.key; 16117 } 16118 16119 $yield1(block, [key, value]); 16120 } 16121 16122 return self; 16123 ; 16124 }); 16125 16126 $def(self, '$each_key', function $$each_key() { 16127 var block = $$each_key.$$p || nil, self = this; 16128 16129 $$each_key.$$p = null; 16130 16131 ; 16132 if (!$truthy(block)) { 16133 return $send(self, 'enum_for', ["each_key"], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 16134 16135 return self.$size()}, {$$s: self}) 16136 }; 16137 16138 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) { 16139 key = keys[i]; 16140 16141 block(key.$$is_string ? key : key.key); 16142 } 16143 16144 return self; 16145 ; 16146 }); 16147 16148 $def(self, '$each_value', function $$each_value() { 16149 var block = $$each_value.$$p || nil, self = this; 16150 16151 $$each_value.$$p = null; 16152 16153 ; 16154 if (!$truthy(block)) { 16155 return $send(self, 'enum_for', ["each_value"], function $$19(){var self = $$19.$$s == null ? this : $$19.$$s; 16156 16157 return self.$size()}, {$$s: self}) 16158 }; 16159 16160 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) { 16161 key = keys[i]; 16162 16163 block(key.$$is_string ? self.$$smap[key] : key.value); 16164 } 16165 16166 return self; 16167 ; 16168 }); 16169 16170 $def(self, '$empty?', function $Hash_empty$ques$20() { 16171 var self = this; 16172 16173 return self.$$keys.length === 0; 16174 }); 16175 16176 $def(self, '$except', function $$except($a) { 16177 var $post_args, keys, self = this; 16178 16179 16180 $post_args = $slice(arguments); 16181 keys = $post_args; 16182 return $send(self.$dup(), 'except!', $to_a(keys)); 16183 }, -1); 16184 16185 $def(self, '$except!', function $Hash_except$excl$21($a) { 16186 var $post_args, keys, self = this; 16187 16188 16189 $post_args = $slice(arguments); 16190 keys = $post_args; 16191 $send(keys, 'each', [], function $$22(key){var self = $$22.$$s == null ? this : $$22.$$s; 16192 16193 16194 if (key == null) key = nil; 16195 return self.$delete(key);}, {$$s: self}); 16196 return self; 16197 }, -1); 16198 16199 $def(self, '$fetch', function $$fetch(key, defaults) { 16200 var block = $$fetch.$$p || nil, self = this; 16201 16202 $$fetch.$$p = null; 16203 16204 ; 16205 ; 16206 16207 var value = $hash_get(self, key); 16208 16209 if (value !== undefined) { 16210 return value; 16211 } 16212 16213 if (block !== nil) { 16214 return block(key); 16215 } 16216 16217 if (defaults !== undefined) { 16218 return defaults; 16219 } 16220 ; 16221 return $Kernel.$raise($$$('KeyError').$new("key not found: " + (key.$inspect()), $hash2(["key", "receiver"], {"key": key, "receiver": self}))); 16222 }, -2); 16223 16224 $def(self, '$fetch_values', function $$fetch_values($a) { 16225 var block = $$fetch_values.$$p || nil, $post_args, keys, self = this; 16226 16227 $$fetch_values.$$p = null; 16228 16229 ; 16230 $post_args = $slice(arguments); 16231 keys = $post_args; 16232 return $send(keys, 'map', [], function $$23(key){var self = $$23.$$s == null ? this : $$23.$$s; 16233 16234 16235 if (key == null) key = nil; 16236 return $send(self, 'fetch', [key], block.$to_proc());}, {$$s: self}); 16237 }, -1); 16238 16239 $def(self, '$flatten', function $$flatten(level) { 16240 var self = this; 16241 16242 16243 if (level == null) level = 1; 16244 level = $Opal['$coerce_to!'](level, $$$('Integer'), "to_int"); 16245 16246 var result = []; 16247 16248 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16249 key = keys[i]; 16250 16251 if (key.$$is_string) { 16252 value = self.$$smap[key]; 16253 } else { 16254 value = key.value; 16255 key = key.key; 16256 } 16257 16258 result.push(key); 16259 16260 if (value.$$is_array) { 16261 if (level === 1) { 16262 result.push(value); 16263 continue; 16264 } 16265 16266 result = result.concat((value).$flatten(level - 2)); 16267 continue; 16268 } 16269 16270 result.push(value); 16271 } 16272 16273 return result; 16274 ; 16275 }, -1); 16276 16277 $def(self, '$freeze', function $$freeze() { 16278 var self = this; 16279 16280 16281 if ($truthy(self['$frozen?']())) { 16282 return self 16283 }; 16284 return $freeze(self);; 16285 }); 16286 16287 $def(self, '$has_key?', function $Hash_has_key$ques$24(key) { 16288 var self = this; 16289 16290 return $hash_get(self, key) !== undefined; 16291 }); 16292 16293 $def(self, '$has_value?', function $Hash_has_value$ques$25(value) { 16294 var self = this; 16295 16296 16297 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 16298 key = keys[i]; 16299 16300 if (((key.$$is_string ? self.$$smap[key] : key.value))['$=='](value)) { 16301 return true; 16302 } 16303 } 16304 16305 return false; 16306 16307 }); 16308 16309 $def(self, '$hash', function $$hash() { 16310 var self = this; 16311 16312 16313 var top = (Opal.hash_ids === undefined), 16314 hash_id = self.$object_id(), 16315 result = ['Hash'], 16316 key, item; 16317 16318 try { 16319 if (top) { 16320 Opal.hash_ids = Object.create(null); 16321 } 16322 16323 if (Opal[hash_id]) { 16324 return 'self'; 16325 } 16326 16327 for (key in Opal.hash_ids) { 16328 item = Opal.hash_ids[key]; 16329 if (self['$eql?'](item)) { 16330 return 'self'; 16331 } 16332 } 16333 16334 Opal.hash_ids[hash_id] = self; 16335 16336 for (var i = 0, keys = self.$$keys, length = keys.length; i < length; i++) { 16337 key = keys[i]; 16338 16339 if (key.$$is_string) { 16340 result.push([key, self.$$smap[key].$hash()]); 16341 } else { 16342 result.push([key.key_hash, key.value.$hash()]); 16343 } 16344 } 16345 16346 return result.sort().join(); 16347 16348 } finally { 16349 if (top) { 16350 Opal.hash_ids = undefined; 16351 } 16352 } 16353 16354 }); 16355 16356 $def(self, '$index', function $$index(object) { 16357 var self = this; 16358 16359 16360 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16361 key = keys[i]; 16362 16363 if (key.$$is_string) { 16364 value = self.$$smap[key]; 16365 } else { 16366 value = key.value; 16367 key = key.key; 16368 } 16369 16370 if ((value)['$=='](object)) { 16371 return key; 16372 } 16373 } 16374 16375 return nil; 16376 16377 }); 16378 16379 $def(self, '$indexes', function $$indexes($a) { 16380 var $post_args, args, self = this; 16381 16382 16383 $post_args = $slice(arguments); 16384 args = $post_args; 16385 16386 var result = []; 16387 16388 for (var i = 0, length = args.length, key, value; i < length; i++) { 16389 key = args[i]; 16390 value = $hash_get(self, key); 16391 16392 if (value === undefined) { 16393 result.push(self.$default()); 16394 continue; 16395 } 16396 16397 result.push(value); 16398 } 16399 16400 return result; 16401 ; 16402 }, -1); 16403 var inspect_ids; 16404 16405 $def(self, '$inspect', function $$inspect() { 16406 var self = this; 16407 16408 16409 16410 var top = (inspect_ids === undefined), 16411 hash_id = self.$object_id(), 16412 result = []; 16413 ; 16414 16415 return (function() { try { 16416 16417 16418 if (top) { 16419 inspect_ids = {}; 16420 } 16421 16422 if (inspect_ids.hasOwnProperty(hash_id)) { 16423 return '{...}'; 16424 } 16425 16426 inspect_ids[hash_id] = true; 16427 16428 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16429 key = keys[i]; 16430 16431 if (key.$$is_string) { 16432 value = self.$$smap[key]; 16433 } else { 16434 value = key.value; 16435 key = key.key; 16436 } 16437 16438 key = $$('Opal').$inspect(key) 16439 value = $$('Opal').$inspect(value) 16440 16441 result.push(key + '=>' + value); 16442 } 16443 16444 return '{' + result.join(', ') + '}'; 16445 ; 16446 return nil; 16447 } finally { 16448 if (top) inspect_ids = undefined 16449 }; })();; 16450 }); 16451 16452 $def(self, '$invert', function $$invert() { 16453 var self = this; 16454 16455 16456 var hash = $hash(); 16457 16458 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16459 key = keys[i]; 16460 16461 if (key.$$is_string) { 16462 value = self.$$smap[key]; 16463 } else { 16464 value = key.value; 16465 key = key.key; 16466 } 16467 16468 $hash_put(hash, value, key); 16469 } 16470 16471 return hash; 16472 16473 }); 16474 16475 $def(self, '$keep_if', function $$keep_if() { 16476 var block = $$keep_if.$$p || nil, self = this; 16477 16478 $$keep_if.$$p = null; 16479 16480 ; 16481 if (!$truthy(block)) { 16482 return $send(self, 'enum_for', ["keep_if"], function $$26(){var self = $$26.$$s == null ? this : $$26.$$s; 16483 16484 return self.$size()}, {$$s: self}) 16485 }; 16486 16487 $deny_frozen_access(self); 16488 16489 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16490 key = keys[i]; 16491 16492 if (key.$$is_string) { 16493 value = self.$$smap[key]; 16494 } else { 16495 value = key.value; 16496 key = key.key; 16497 } 16498 16499 obj = block(key, value); 16500 16501 if (obj === false || obj === nil) { 16502 if ($hash_delete(self, key) !== undefined) { 16503 length--; 16504 i--; 16505 } 16506 } 16507 } 16508 16509 return self; 16510 ; 16511 }); 16512 16513 $def(self, '$keys', function $$keys() { 16514 var self = this; 16515 16516 16517 var result = []; 16518 16519 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 16520 key = keys[i]; 16521 16522 if (key.$$is_string) { 16523 result.push(key); 16524 } else { 16525 result.push(key.key); 16526 } 16527 } 16528 16529 return result; 16530 16531 }); 16532 16533 $def(self, '$length', function $$length() { 16534 var self = this; 16535 16536 return self.$$keys.length; 16537 }); 16538 16539 $def(self, '$merge', function $$merge($a) { 16540 var block = $$merge.$$p || nil, $post_args, others, self = this; 16541 16542 $$merge.$$p = null; 16543 16544 ; 16545 $post_args = $slice(arguments); 16546 others = $post_args; 16547 return $send(self.$dup(), 'merge!', $to_a(others), block.$to_proc()); 16548 }, -1); 16549 16550 $def(self, '$merge!', function $Hash_merge$excl$27($a) { 16551 var block = $Hash_merge$excl$27.$$p || nil, $post_args, others, self = this; 16552 16553 $Hash_merge$excl$27.$$p = null; 16554 16555 ; 16556 $post_args = $slice(arguments); 16557 others = $post_args; 16558 16559 $deny_frozen_access(self); 16560 var i, j, other, other_keys, length, key, value, other_value; 16561 for (i = 0; i < others.length; ++i) { 16562 other = $Opal['$coerce_to!'](others[i], $$$('Hash'), "to_hash"); 16563 other_keys = other.$$keys, length = other_keys.length; 16564 16565 if (block === nil) { 16566 for (j = 0; j < length; j++) { 16567 key = other_keys[j]; 16568 16569 if (key.$$is_string) { 16570 other_value = other.$$smap[key]; 16571 } else { 16572 other_value = key.value; 16573 key = key.key; 16574 } 16575 16576 $hash_put(self, key, other_value); 16577 } 16578 } else { 16579 for (j = 0; j < length; j++) { 16580 key = other_keys[j]; 16581 16582 if (key.$$is_string) { 16583 other_value = other.$$smap[key]; 16584 } else { 16585 other_value = key.value; 16586 key = key.key; 16587 } 16588 16589 value = $hash_get(self, key); 16590 16591 if (value === undefined) { 16592 $hash_put(self, key, other_value); 16593 continue; 16594 } 16595 16596 $hash_put(self, key, block(key, value, other_value)); 16597 } 16598 } 16599 } 16600 16601 return self; 16602 ; 16603 }, -1); 16604 16605 $def(self, '$rassoc', function $$rassoc(object) { 16606 var self = this; 16607 16608 16609 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16610 key = keys[i]; 16611 16612 if (key.$$is_string) { 16613 value = self.$$smap[key]; 16614 } else { 16615 value = key.value; 16616 key = key.key; 16617 } 16618 16619 if ((value)['$=='](object)) { 16620 return [key, value]; 16621 } 16622 } 16623 16624 return nil; 16625 16626 }); 16627 16628 $def(self, '$rehash', function $$rehash() { 16629 var self = this; 16630 16631 16632 $deny_frozen_access(self); 16633 Opal.hash_rehash(self); 16634 return self; 16635 16636 }); 16637 16638 $def(self, '$reject', function $$reject() { 16639 var block = $$reject.$$p || nil, self = this; 16640 16641 $$reject.$$p = null; 16642 16643 ; 16644 if (!$truthy(block)) { 16645 return $send(self, 'enum_for', ["reject"], function $$28(){var self = $$28.$$s == null ? this : $$28.$$s; 16646 16647 return self.$size()}, {$$s: self}) 16648 }; 16649 16650 var hash = $hash(); 16651 16652 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16653 key = keys[i]; 16654 16655 if (key.$$is_string) { 16656 value = self.$$smap[key]; 16657 } else { 16658 value = key.value; 16659 key = key.key; 16660 } 16661 16662 obj = block(key, value); 16663 16664 if (obj === false || obj === nil) { 16665 $hash_put(hash, key, value); 16666 } 16667 } 16668 16669 return hash; 16670 ; 16671 }); 16672 16673 $def(self, '$reject!', function $Hash_reject$excl$29() { 16674 var block = $Hash_reject$excl$29.$$p || nil, self = this; 16675 16676 $Hash_reject$excl$29.$$p = null; 16677 16678 ; 16679 if (!$truthy(block)) { 16680 return $send(self, 'enum_for', ["reject!"], function $$30(){var self = $$30.$$s == null ? this : $$30.$$s; 16681 16682 return self.$size()}, {$$s: self}) 16683 }; 16684 16685 $deny_frozen_access(self); 16686 16687 var changes_were_made = false; 16688 16689 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16690 key = keys[i]; 16691 16692 if (key.$$is_string) { 16693 value = self.$$smap[key]; 16694 } else { 16695 value = key.value; 16696 key = key.key; 16697 } 16698 16699 obj = block(key, value); 16700 16701 if (obj !== false && obj !== nil) { 16702 if ($hash_delete(self, key) !== undefined) { 16703 changes_were_made = true; 16704 length--; 16705 i--; 16706 } 16707 } 16708 } 16709 16710 return changes_were_made ? self : nil; 16711 ; 16712 }); 16713 16714 $def(self, '$replace', function $$replace(other) { 16715 var self = this; 16716 16717 16718 $deny_frozen_access(self);; 16719 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 16720 16721 $hash_init(self); 16722 16723 for (var i = 0, other_keys = other.$$keys, length = other_keys.length, key, value, other_value; i < length; i++) { 16724 key = other_keys[i]; 16725 16726 if (key.$$is_string) { 16727 other_value = other.$$smap[key]; 16728 } else { 16729 other_value = key.value; 16730 key = key.key; 16731 } 16732 16733 $hash_put(self, key, other_value); 16734 } 16735 ; 16736 if ($truthy(other.$default_proc())) { 16737 self['$default_proc='](other.$default_proc()) 16738 } else { 16739 self['$default='](other.$default()) 16740 }; 16741 return self; 16742 }); 16743 16744 $def(self, '$select', function $$select() { 16745 var block = $$select.$$p || nil, self = this; 16746 16747 $$select.$$p = null; 16748 16749 ; 16750 if (!$truthy(block)) { 16751 return $send(self, 'enum_for', ["select"], function $$31(){var self = $$31.$$s == null ? this : $$31.$$s; 16752 16753 return self.$size()}, {$$s: self}) 16754 }; 16755 16756 var hash = $hash(); 16757 16758 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16759 key = keys[i]; 16760 16761 if (key.$$is_string) { 16762 value = self.$$smap[key]; 16763 } else { 16764 value = key.value; 16765 key = key.key; 16766 } 16767 16768 obj = block(key, value); 16769 16770 if (obj !== false && obj !== nil) { 16771 $hash_put(hash, key, value); 16772 } 16773 } 16774 16775 return hash; 16776 ; 16777 }); 16778 16779 $def(self, '$select!', function $Hash_select$excl$32() { 16780 var block = $Hash_select$excl$32.$$p || nil, self = this; 16781 16782 $Hash_select$excl$32.$$p = null; 16783 16784 ; 16785 if (!$truthy(block)) { 16786 return $send(self, 'enum_for', ["select!"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 16787 16788 return self.$size()}, {$$s: self}) 16789 }; 16790 16791 $deny_frozen_access(self); 16792 16793 var result = nil; 16794 16795 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 16796 key = keys[i]; 16797 16798 if (key.$$is_string) { 16799 value = self.$$smap[key]; 16800 } else { 16801 value = key.value; 16802 key = key.key; 16803 } 16804 16805 obj = block(key, value); 16806 16807 if (obj === false || obj === nil) { 16808 if ($hash_delete(self, key) !== undefined) { 16809 length--; 16810 i--; 16811 } 16812 result = self; 16813 } 16814 } 16815 16816 return result; 16817 ; 16818 }); 16819 16820 $def(self, '$shift', function $$shift() { 16821 var self = this; 16822 16823 16824 $deny_frozen_access(self); 16825 var keys = self.$$keys, 16826 key; 16827 16828 if (keys.length > 0) { 16829 key = keys[0]; 16830 16831 key = key.$$is_string ? key : key.key; 16832 16833 return [key, $hash_delete(self, key)]; 16834 } 16835 16836 return nil; 16837 16838 }); 16839 16840 $def(self, '$slice', function $$slice($a) { 16841 var $post_args, keys, self = this; 16842 16843 16844 $post_args = $slice(arguments); 16845 keys = $post_args; 16846 16847 var result = $hash(); 16848 16849 for (var i = 0, length = keys.length; i < length; i++) { 16850 var key = keys[i], value = $hash_get(self, key); 16851 16852 if (value !== undefined) { 16853 $hash_put(result, key, value); 16854 } 16855 } 16856 16857 return result; 16858 ; 16859 }, -1); 16860 16861 $def(self, '$to_a', function $$to_a() { 16862 var self = this; 16863 16864 16865 var result = []; 16866 16867 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16868 key = keys[i]; 16869 16870 if (key.$$is_string) { 16871 value = self.$$smap[key]; 16872 } else { 16873 value = key.value; 16874 key = key.key; 16875 } 16876 16877 result.push([key, value]); 16878 } 16879 16880 return result; 16881 16882 }); 16883 16884 $def(self, '$to_h', function $$to_h() { 16885 var block = $$to_h.$$p || nil, self = this; 16886 16887 $$to_h.$$p = null; 16888 16889 ; 16890 if ((block !== nil)) { 16891 return $send(self, 'map', [], block.$to_proc()).$to_h() 16892 }; 16893 16894 if (self.$$class === Opal.Hash) { 16895 return self; 16896 } 16897 16898 var hash = new Opal.Hash(); 16899 16900 $hash_init(hash); 16901 Opal.hash_clone(self, hash); 16902 16903 return hash; 16904 ; 16905 }); 16906 16907 $def(self, '$to_hash', $return_self); 16908 16909 $def(self, '$to_proc', function $$to_proc() { 16910 var self = this; 16911 16912 return $send(self, 'proc', [], function $$34(key){var self = $$34.$$s == null ? this : $$34.$$s; 16913 16914 16915 ; 16916 16917 if (key == null) { 16918 $Kernel.$raise($$$('ArgumentError'), "no key given") 16919 } 16920 ; 16921 return self['$[]'](key);}, {$$arity: -1, $$s: self}) 16922 }); 16923 16924 $def(self, '$transform_keys', function $$transform_keys() { 16925 var block = $$transform_keys.$$p || nil, self = this; 16926 16927 $$transform_keys.$$p = null; 16928 16929 ; 16930 if (!$truthy(block)) { 16931 return $send(self, 'enum_for', ["transform_keys"], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 16932 16933 return self.$size()}, {$$s: self}) 16934 }; 16935 16936 var result = $hash(); 16937 16938 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16939 key = keys[i]; 16940 16941 if (key.$$is_string) { 16942 value = self.$$smap[key]; 16943 } else { 16944 value = key.value; 16945 key = key.key; 16946 } 16947 16948 key = $yield1(block, key); 16949 16950 $hash_put(result, key, value); 16951 } 16952 16953 return result; 16954 ; 16955 }); 16956 16957 $def(self, '$transform_keys!', function $Hash_transform_keys$excl$36() { 16958 var block = $Hash_transform_keys$excl$36.$$p || nil, self = this; 16959 16960 $Hash_transform_keys$excl$36.$$p = null; 16961 16962 ; 16963 if (!$truthy(block)) { 16964 return $send(self, 'enum_for', ["transform_keys!"], function $$37(){var self = $$37.$$s == null ? this : $$37.$$s; 16965 16966 return self.$size()}, {$$s: self}) 16967 }; 16968 16969 $deny_frozen_access(self); 16970 16971 var keys = Opal.slice(self.$$keys), 16972 i, length = keys.length, key, value, new_key; 16973 16974 for (i = 0; i < length; i++) { 16975 key = keys[i]; 16976 16977 if (key.$$is_string) { 16978 value = self.$$smap[key]; 16979 } else { 16980 value = key.value; 16981 key = key.key; 16982 } 16983 16984 new_key = $yield1(block, key); 16985 16986 $hash_delete(self, key); 16987 $hash_put(self, new_key, value); 16988 } 16989 16990 return self; 16991 ; 16992 }); 16993 16994 $def(self, '$transform_values', function $$transform_values() { 16995 var block = $$transform_values.$$p || nil, self = this; 16996 16997 $$transform_values.$$p = null; 16998 16999 ; 17000 if (!$truthy(block)) { 17001 return $send(self, 'enum_for', ["transform_values"], function $$38(){var self = $$38.$$s == null ? this : $$38.$$s; 17002 17003 return self.$size()}, {$$s: self}) 17004 }; 17005 17006 var result = $hash(); 17007 17008 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 17009 key = keys[i]; 17010 17011 if (key.$$is_string) { 17012 value = self.$$smap[key]; 17013 } else { 17014 value = key.value; 17015 key = key.key; 17016 } 17017 17018 value = $yield1(block, value); 17019 17020 $hash_put(result, key, value); 17021 } 17022 17023 return result; 17024 ; 17025 }); 17026 17027 $def(self, '$transform_values!', function $Hash_transform_values$excl$39() { 17028 var block = $Hash_transform_values$excl$39.$$p || nil, self = this; 17029 17030 $Hash_transform_values$excl$39.$$p = null; 17031 17032 ; 17033 if (!$truthy(block)) { 17034 return $send(self, 'enum_for', ["transform_values!"], function $$40(){var self = $$40.$$s == null ? this : $$40.$$s; 17035 17036 return self.$size()}, {$$s: self}) 17037 }; 17038 17039 $deny_frozen_access(self); 17040 17041 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 17042 key = keys[i]; 17043 17044 if (key.$$is_string) { 17045 value = self.$$smap[key]; 17046 } else { 17047 value = key.value; 17048 key = key.key; 17049 } 17050 17051 value = $yield1(block, value); 17052 17053 $hash_put(self, key, value); 17054 } 17055 17056 return self; 17057 ; 17058 }); 17059 17060 $def(self, '$values', function $$values() { 17061 var self = this; 17062 17063 17064 var result = []; 17065 17066 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 17067 key = keys[i]; 17068 17069 if (key.$$is_string) { 17070 result.push(self.$$smap[key]); 17071 } else { 17072 result.push(key.value); 17073 } 17074 } 17075 17076 return result; 17077 17078 }); 17079 $alias(self, "dup", "clone"); 17080 $alias(self, "each_pair", "each"); 17081 $alias(self, "eql?", "=="); 17082 $alias(self, "filter", "select"); 17083 $alias(self, "filter!", "select!"); 17084 $alias(self, "include?", "has_key?"); 17085 $alias(self, "indices", "indexes"); 17086 $alias(self, "key", "index"); 17087 $alias(self, "key?", "has_key?"); 17088 $alias(self, "member?", "has_key?"); 17089 $alias(self, "size", "length"); 17090 $alias(self, "store", "[]="); 17091 $alias(self, "to_s", "inspect"); 17092 $alias(self, "update", "merge!"); 17093 $alias(self, "value?", "has_value?"); 17094 return $alias(self, "values_at", "indexes"); 17095 })('::', null, $nesting); 17096}; 17097 17098Opal.modules["corelib/number"] = function(Opal) {/* Generated by Opal 1.7.3 */ 17099 var $klass = Opal.klass, $Opal = Opal.Opal, $Kernel = Opal.Kernel, $def = Opal.def, $eqeqeq = Opal.eqeqeq, $truthy = Opal.truthy, $rb_gt = Opal.rb_gt, $not = Opal.not, $rb_lt = Opal.rb_lt, $alias = Opal.alias, $send2 = Opal.send2, $find_super = Opal.find_super, $send = Opal.send, $rb_plus = Opal.rb_plus, $rb_minus = Opal.rb_minus, $eqeq = Opal.eqeq, $return_self = Opal.return_self, $rb_divide = Opal.rb_divide, $to_ary = Opal.to_ary, $rb_times = Opal.rb_times, $rb_le = Opal.rb_le, $rb_ge = Opal.rb_ge, $return_val = Opal.return_val, $const_set = Opal.const_set, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 17100 17101 Opal.add_stubs('require,bridge,raise,name,class,Float,respond_to?,coerce_to!,__coerced__,===,>,!,**,new,<,to_f,==,nan?,infinite?,enum_for,+,-,gcd,lcm,%,/,frexp,to_i,ldexp,rationalize,*,<<,to_r,truncate,-@,size,<=,>=,inspect,angle,to_s,is_a?,abs,__id__,next,coerce_to?'); 17102 17103 self.$require("corelib/numeric"); 17104 (function($base, $super, $parent_nesting) { 17105 var self = $klass($base, $super, 'Number'); 17106 17107 var $nesting = [self].concat($parent_nesting); 17108 17109 17110 $Opal.$bridge(Number, self); 17111 Opal.prop(self.$$prototype, '$$is_number', true); 17112 self.$$is_number_class = true; 17113 (function(self, $parent_nesting) { 17114 17115 17116 17117 $def(self, '$allocate', function $$allocate() { 17118 var self = this; 17119 17120 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 17121 }); 17122 17123 17124 Opal.udef(self, '$' + "new");; 17125 return nil;; 17126 })(Opal.get_singleton_class(self), $nesting); 17127 17128 $def(self, '$coerce', function $$coerce(other) { 17129 var self = this; 17130 17131 17132 if (other === nil) { 17133 $Kernel.$raise($$$('TypeError'), "can't convert " + (other.$class()) + " into Float"); 17134 } 17135 else if (other.$$is_string) { 17136 return [$Kernel.$Float(other), self]; 17137 } 17138 else if (other['$respond_to?']("to_f")) { 17139 return [$Opal['$coerce_to!'](other, $$$('Float'), "to_f"), self]; 17140 } 17141 else if (other.$$is_number) { 17142 return [other, self]; 17143 } 17144 else { 17145 $Kernel.$raise($$$('TypeError'), "can't convert " + (other.$class()) + " into Float"); 17146 } 17147 17148 }); 17149 17150 $def(self, '$__id__', function $$__id__() { 17151 var self = this; 17152 17153 return (self * 2) + 1; 17154 }); 17155 17156 $def(self, '$+', function $Number_$plus$1(other) { 17157 var self = this; 17158 17159 17160 if (other.$$is_number) { 17161 return self + other; 17162 } 17163 else { 17164 return self.$__coerced__("+", other); 17165 } 17166 17167 }); 17168 17169 $def(self, '$-', function $Number_$minus$2(other) { 17170 var self = this; 17171 17172 17173 if (other.$$is_number) { 17174 return self - other; 17175 } 17176 else { 17177 return self.$__coerced__("-", other); 17178 } 17179 17180 }); 17181 17182 $def(self, '$*', function $Number_$$3(other) { 17183 var self = this; 17184 17185 17186 if (other.$$is_number) { 17187 return self * other; 17188 } 17189 else { 17190 return self.$__coerced__("*", other); 17191 } 17192 17193 }); 17194 17195 $def(self, '$/', function $Number_$slash$4(other) { 17196 var self = this; 17197 17198 17199 if (other.$$is_number) { 17200 return self / other; 17201 } 17202 else { 17203 return self.$__coerced__("/", other); 17204 } 17205 17206 }); 17207 17208 $def(self, '$%', function $Number_$percent$5(other) { 17209 var self = this; 17210 17211 17212 if (other.$$is_number) { 17213 if (other == -Infinity) { 17214 return other; 17215 } 17216 else if (other == 0) { 17217 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0"); 17218 } 17219 else if (other < 0 || self < 0) { 17220 return (self % other + other) % other; 17221 } 17222 else { 17223 return self % other; 17224 } 17225 } 17226 else { 17227 return self.$__coerced__("%", other); 17228 } 17229 17230 }); 17231 17232 $def(self, '$&', function $Number_$$6(other) { 17233 var self = this; 17234 17235 17236 if (other.$$is_number) { 17237 return self & other; 17238 } 17239 else { 17240 return self.$__coerced__("&", other); 17241 } 17242 17243 }); 17244 17245 $def(self, '$|', function $Number_$$7(other) { 17246 var self = this; 17247 17248 17249 if (other.$$is_number) { 17250 return self | other; 17251 } 17252 else { 17253 return self.$__coerced__("|", other); 17254 } 17255 17256 }); 17257 17258 $def(self, '$^', function $Number_$$8(other) { 17259 var self = this; 17260 17261 17262 if (other.$$is_number) { 17263 return self ^ other; 17264 } 17265 else { 17266 return self.$__coerced__("^", other); 17267 } 17268 17269 }); 17270 17271 $def(self, '$<', function $Number_$lt$9(other) { 17272 var self = this; 17273 17274 17275 if (other.$$is_number) { 17276 return self < other; 17277 } 17278 else { 17279 return self.$__coerced__("<", other); 17280 } 17281 17282 }); 17283 17284 $def(self, '$<=', function $Number_$lt_eq$10(other) { 17285 var self = this; 17286 17287 17288 if (other.$$is_number) { 17289 return self <= other; 17290 } 17291 else { 17292 return self.$__coerced__("<=", other); 17293 } 17294 17295 }); 17296 17297 $def(self, '$>', function $Number_$gt$11(other) { 17298 var self = this; 17299 17300 17301 if (other.$$is_number) { 17302 return self > other; 17303 } 17304 else { 17305 return self.$__coerced__(">", other); 17306 } 17307 17308 }); 17309 17310 $def(self, '$>=', function $Number_$gt_eq$12(other) { 17311 var self = this; 17312 17313 17314 if (other.$$is_number) { 17315 return self >= other; 17316 } 17317 else { 17318 return self.$__coerced__(">=", other); 17319 } 17320 17321 }); 17322 17323 var spaceship_operator = function(self, other) { 17324 if (other.$$is_number) { 17325 if (isNaN(self) || isNaN(other)) { 17326 return nil; 17327 } 17328 17329 if (self > other) { 17330 return 1; 17331 } else if (self < other) { 17332 return -1; 17333 } else { 17334 return 0; 17335 } 17336 } 17337 else { 17338 return self.$__coerced__("<=>", other); 17339 } 17340 } 17341 ; 17342 17343 $def(self, '$<=>', function $Number_$lt_eq_gt$13(other) { 17344 var self = this; 17345 17346 try { 17347 return spaceship_operator(self, other); 17348 } catch ($err) { 17349 if (Opal.rescue($err, [$$$('ArgumentError')])) { 17350 try { 17351 return nil 17352 } finally { Opal.pop_exception(); } 17353 } else { throw $err; } 17354 } 17355 }); 17356 17357 $def(self, '$<<', function $Number_$lt$lt$14(count) { 17358 var self = this; 17359 17360 17361 count = $Opal['$coerce_to!'](count, $$$('Integer'), "to_int"); 17362 return count > 0 ? self << count : self >> -count; 17363 }); 17364 17365 $def(self, '$>>', function $Number_$gt$gt$15(count) { 17366 var self = this; 17367 17368 17369 count = $Opal['$coerce_to!'](count, $$$('Integer'), "to_int"); 17370 return count > 0 ? self >> count : self << -count; 17371 }); 17372 17373 $def(self, '$[]', function $Number_$$$16(bit) { 17374 var self = this; 17375 17376 17377 bit = $Opal['$coerce_to!'](bit, $$$('Integer'), "to_int"); 17378 17379 if (bit < 0) { 17380 return 0; 17381 } 17382 if (bit >= 32) { 17383 return self < 0 ? 1 : 0; 17384 } 17385 return (self >> bit) & 1; 17386 ; 17387 }); 17388 17389 $def(self, '$+@', function $Number_$plus$$17() { 17390 var self = this; 17391 17392 return +self; 17393 }); 17394 17395 $def(self, '$-@', function $Number_$minus$$18() { 17396 var self = this; 17397 17398 return -self; 17399 }); 17400 17401 $def(self, '$~', function $Number_$$19() { 17402 var self = this; 17403 17404 return ~self; 17405 }); 17406 17407 $def(self, '$**', function $Number_$$$20(other) { 17408 var self = this; 17409 17410 if ($eqeqeq($$$('Integer'), other)) { 17411 if (($not($$$('Integer')['$==='](self)) || ($truthy($rb_gt(other, 0))))) { 17412 return Math.pow(self, other); 17413 } else { 17414 return $$$('Rational').$new(self, 1)['$**'](other) 17415 } 17416 } else if (($rb_lt(self, 0) && (($eqeqeq($$$('Float'), other) || ($eqeqeq($$$('Rational'), other)))))) { 17417 return $$$('Complex').$new(self, 0)['$**'](other.$to_f()) 17418 } else if ($truthy(other.$$is_number != null)) { 17419 return Math.pow(self, other); 17420 } else { 17421 return self.$__coerced__("**", other) 17422 } 17423 }); 17424 17425 $def(self, '$==', function $Number_$eq_eq$21(other) { 17426 var self = this; 17427 17428 17429 if (other.$$is_number) { 17430 return self.valueOf() === other.valueOf(); 17431 } 17432 else if (other['$respond_to?']("==")) { 17433 return other['$=='](self); 17434 } 17435 else { 17436 return false; 17437 } 17438 17439 }); 17440 $alias(self, "===", "=="); 17441 17442 $def(self, '$abs', function $$abs() { 17443 var self = this; 17444 17445 return Math.abs(self); 17446 }); 17447 17448 $def(self, '$abs2', function $$abs2() { 17449 var self = this; 17450 17451 return Math.abs(self * self); 17452 }); 17453 17454 $def(self, '$allbits?', function $Number_allbits$ques$22(mask) { 17455 var self = this; 17456 17457 17458 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 17459 return (self & mask) == mask;; 17460 }); 17461 17462 $def(self, '$anybits?', function $Number_anybits$ques$23(mask) { 17463 var self = this; 17464 17465 17466 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 17467 return (self & mask) !== 0;; 17468 }); 17469 17470 $def(self, '$angle', function $$angle() { 17471 var self = this; 17472 17473 17474 if ($truthy(self['$nan?']())) { 17475 return self 17476 }; 17477 17478 if (self == 0) { 17479 if (1 / self > 0) { 17480 return 0; 17481 } 17482 else { 17483 return Math.PI; 17484 } 17485 } 17486 else if (self < 0) { 17487 return Math.PI; 17488 } 17489 else { 17490 return 0; 17491 } 17492 ; 17493 }); 17494 17495 $def(self, '$bit_length', function $$bit_length() { 17496 var self = this; 17497 17498 17499 if (!$eqeqeq($$$('Integer'), self)) { 17500 $Kernel.$raise($$$('NoMethodError').$new("undefined method `bit_length` for " + (self) + ":Float", "bit_length")) 17501 }; 17502 17503 if (self === 0 || self === -1) { 17504 return 0; 17505 } 17506 17507 var result = 0, 17508 value = self < 0 ? ~self : self; 17509 17510 while (value != 0) { 17511 result += 1; 17512 value >>>= 1; 17513 } 17514 17515 return result; 17516 ; 17517 }); 17518 17519 $def(self, '$ceil', function $$ceil(ndigits) { 17520 var self = this; 17521 17522 17523 if (ndigits == null) ndigits = 0; 17524 17525 var f = self.$to_f(); 17526 17527 if (f % 1 === 0 && ndigits >= 0) { 17528 return f; 17529 } 17530 17531 var factor = Math.pow(10, ndigits), 17532 result = Math.ceil(f * factor) / factor; 17533 17534 if (f % 1 === 0) { 17535 result = Math.round(result); 17536 } 17537 17538 return result; 17539 ; 17540 }, -1); 17541 17542 $def(self, '$chr', function $$chr(encoding) { 17543 var self = this; 17544 17545 17546 ; 17547 return Opal.enc(String.fromCharCode(self), encoding || "BINARY");; 17548 }, -1); 17549 17550 $def(self, '$denominator', function $$denominator() { 17551 var $yield = $$denominator.$$p || nil, self = this; 17552 17553 $$denominator.$$p = null; 17554 if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 17555 return 1 17556 } else { 17557 return $send2(self, $find_super(self, 'denominator', $$denominator, false, true), 'denominator', [], $yield) 17558 } 17559 }); 17560 17561 $def(self, '$downto', function $$downto(stop) { 17562 var block = $$downto.$$p || nil, self = this; 17563 17564 $$downto.$$p = null; 17565 17566 ; 17567 if (!(block !== nil)) { 17568 return $send(self, 'enum_for', ["downto", stop], function $$24(){var self = $$24.$$s == null ? this : $$24.$$s; 17569 17570 17571 if (!$eqeqeq($$$('Numeric'), stop)) { 17572 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed") 17573 }; 17574 if ($truthy($rb_gt(stop, self))) { 17575 return 0 17576 } else { 17577 return $rb_plus($rb_minus(self, stop), 1) 17578 };}, {$$s: self}) 17579 }; 17580 17581 if (!stop.$$is_number) { 17582 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed") 17583 } 17584 for (var i = self; i >= stop; i--) { 17585 block(i); 17586 } 17587 ; 17588 return self; 17589 }); 17590 17591 $def(self, '$equal?', function $Number_equal$ques$25(other) { 17592 var self = this, $ret_or_1 = nil; 17593 17594 if ($truthy(($ret_or_1 = self['$=='](other)))) { 17595 return $ret_or_1 17596 } else { 17597 return isNaN(self) && isNaN(other); 17598 } 17599 }); 17600 17601 $def(self, '$even?', function $Number_even$ques$26() { 17602 var self = this; 17603 17604 return self % 2 === 0; 17605 }); 17606 17607 $def(self, '$floor', function $$floor(ndigits) { 17608 var self = this; 17609 17610 17611 if (ndigits == null) ndigits = 0; 17612 17613 var f = self.$to_f(); 17614 17615 if (f % 1 === 0 && ndigits >= 0) { 17616 return f; 17617 } 17618 17619 var factor = Math.pow(10, ndigits), 17620 result = Math.floor(f * factor) / factor; 17621 17622 if (f % 1 === 0) { 17623 result = Math.round(result); 17624 } 17625 17626 return result; 17627 ; 17628 }, -1); 17629 17630 $def(self, '$gcd', function $$gcd(other) { 17631 var self = this; 17632 17633 17634 if (!$eqeqeq($$$('Integer'), other)) { 17635 $Kernel.$raise($$$('TypeError'), "not an integer") 17636 }; 17637 17638 var min = Math.abs(self), 17639 max = Math.abs(other); 17640 17641 while (min > 0) { 17642 var tmp = min; 17643 17644 min = max % min; 17645 max = tmp; 17646 } 17647 17648 return max; 17649 ; 17650 }); 17651 17652 $def(self, '$gcdlcm', function $$gcdlcm(other) { 17653 var self = this; 17654 17655 return [self.$gcd(other), self.$lcm(other)] 17656 }); 17657 17658 $def(self, '$integer?', function $Number_integer$ques$27() { 17659 var self = this; 17660 17661 return self % 1 === 0; 17662 }); 17663 17664 $def(self, '$is_a?', function $Number_is_a$ques$28(klass) { 17665 var $yield = $Number_is_a$ques$28.$$p || nil, self = this; 17666 17667 $Number_is_a$ques$28.$$p = null; 17668 17669 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 17670 return true 17671 }; 17672 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 17673 return true 17674 }; 17675 if (($eqeq(klass, $$$('Float')) && ($eqeqeq($$$('Float'), self)))) { 17676 return true 17677 }; 17678 return $send2(self, $find_super(self, 'is_a?', $Number_is_a$ques$28, false, true), 'is_a?', [klass], $yield); 17679 }); 17680 17681 $def(self, '$instance_of?', function $Number_instance_of$ques$29(klass) { 17682 var $yield = $Number_instance_of$ques$29.$$p || nil, self = this; 17683 17684 $Number_instance_of$ques$29.$$p = null; 17685 17686 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 17687 return true 17688 }; 17689 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 17690 return true 17691 }; 17692 if (($eqeq(klass, $$$('Float')) && ($eqeqeq($$$('Float'), self)))) { 17693 return true 17694 }; 17695 return $send2(self, $find_super(self, 'instance_of?', $Number_instance_of$ques$29, false, true), 'instance_of?', [klass], $yield); 17696 }); 17697 17698 $def(self, '$lcm', function $$lcm(other) { 17699 var self = this; 17700 17701 17702 if (!$eqeqeq($$$('Integer'), other)) { 17703 $Kernel.$raise($$$('TypeError'), "not an integer") 17704 }; 17705 17706 if (self == 0 || other == 0) { 17707 return 0; 17708 } 17709 else { 17710 return Math.abs(self * other / self.$gcd(other)); 17711 } 17712 ; 17713 }); 17714 17715 $def(self, '$next', function $$next() { 17716 var self = this; 17717 17718 return self + 1; 17719 }); 17720 17721 $def(self, '$nobits?', function $Number_nobits$ques$30(mask) { 17722 var self = this; 17723 17724 17725 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 17726 return (self & mask) == 0;; 17727 }); 17728 17729 $def(self, '$nonzero?', function $Number_nonzero$ques$31() { 17730 var self = this; 17731 17732 return self == 0 ? nil : self; 17733 }); 17734 17735 $def(self, '$numerator', function $$numerator() { 17736 var $yield = $$numerator.$$p || nil, self = this; 17737 17738 $$numerator.$$p = null; 17739 if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 17740 return self 17741 } else { 17742 return $send2(self, $find_super(self, 'numerator', $$numerator, false, true), 'numerator', [], $yield) 17743 } 17744 }); 17745 17746 $def(self, '$odd?', function $Number_odd$ques$32() { 17747 var self = this; 17748 17749 return self % 2 !== 0; 17750 }); 17751 17752 $def(self, '$ord', $return_self); 17753 17754 $def(self, '$pow', function $$pow(b, m) { 17755 var self = this; 17756 17757 17758 ; 17759 17760 if (self == 0) { 17761 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0") 17762 } 17763 17764 if (m === undefined) { 17765 return self['$**'](b); 17766 } else { 17767 if (!($$$('Integer')['$==='](b))) { 17768 $Kernel.$raise($$$('TypeError'), "Integer#pow() 2nd argument not allowed unless a 1st argument is integer") 17769 } 17770 17771 if (b < 0) { 17772 $Kernel.$raise($$$('TypeError'), "Integer#pow() 1st argument cannot be negative when 2nd argument specified") 17773 } 17774 17775 if (!($$$('Integer')['$==='](m))) { 17776 $Kernel.$raise($$$('TypeError'), "Integer#pow() 2nd argument not allowed unless all arguments are integers") 17777 } 17778 17779 if (m === 0) { 17780 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0") 17781 } 17782 17783 return self['$**'](b)['$%'](m) 17784 } 17785 ; 17786 }, -2); 17787 17788 $def(self, '$pred', function $$pred() { 17789 var self = this; 17790 17791 return self - 1; 17792 }); 17793 17794 $def(self, '$quo', function $$quo(other) { 17795 var $yield = $$quo.$$p || nil, self = this; 17796 17797 $$quo.$$p = null; 17798 if ($eqeqeq($$$('Integer'), self)) { 17799 return $send2(self, $find_super(self, 'quo', $$quo, false, true), 'quo', [other], $yield) 17800 } else { 17801 return $rb_divide(self, other) 17802 } 17803 }); 17804 17805 $def(self, '$rationalize', function $$rationalize(eps) { 17806 var $a, $b, self = this, f = nil, n = nil; 17807 17808 17809 ; 17810 17811 if (arguments.length > 1) { 17812 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 17813 } 17814 ; 17815 if ($eqeqeq($$$('Integer'), self)) { 17816 return $$$('Rational').$new(self, 1) 17817 } else if ($truthy(self['$infinite?']())) { 17818 return $Kernel.$raise($$$('FloatDomainError'), "Infinity") 17819 } else if ($truthy(self['$nan?']())) { 17820 return $Kernel.$raise($$$('FloatDomainError'), "NaN") 17821 } else if ($truthy(eps == null)) { 17822 17823 $b = $$$('Math').$frexp(self), $a = $to_ary($b), (f = ($a[0] == null ? nil : $a[0])), (n = ($a[1] == null ? nil : $a[1])), $b; 17824 f = $$$('Math').$ldexp(f, $$$($$$('Float'), 'MANT_DIG')).$to_i(); 17825 n = $rb_minus(n, $$$($$$('Float'), 'MANT_DIG')); 17826 return $$$('Rational').$new($rb_times(2, f), (1)['$<<']($rb_minus(1, n))).$rationalize($$$('Rational').$new(1, (1)['$<<']($rb_minus(1, n)))); 17827 } else { 17828 return self.$to_r().$rationalize(eps) 17829 }; 17830 }, -1); 17831 17832 $def(self, '$remainder', function $$remainder(y) { 17833 var self = this; 17834 17835 return $rb_minus(self, $rb_times(y, $rb_divide(self, y).$truncate())) 17836 }); 17837 17838 $def(self, '$round', function $$round(ndigits) { 17839 var $a, $b, self = this, _ = nil, exp = nil; 17840 17841 17842 ; 17843 if ($eqeqeq($$$('Integer'), self)) { 17844 17845 if ($truthy(ndigits == null)) { 17846 return self 17847 }; 17848 if (($eqeqeq($$$('Float'), ndigits) && ($truthy(ndigits['$infinite?']())))) { 17849 $Kernel.$raise($$$('RangeError'), "Infinity") 17850 }; 17851 ndigits = $Opal['$coerce_to!'](ndigits, $$$('Integer'), "to_int"); 17852 if ($truthy($rb_lt(ndigits, $$$($$$('Integer'), 'MIN')))) { 17853 $Kernel.$raise($$$('RangeError'), "out of bounds") 17854 }; 17855 if ($truthy(ndigits >= 0)) { 17856 return self 17857 }; 17858 ndigits = ndigits['$-@'](); 17859 17860 if (0.415241 * ndigits - 0.125 > self.$size()) { 17861 return 0; 17862 } 17863 17864 var f = Math.pow(10, ndigits), 17865 x = Math.floor((Math.abs(self) + f / 2) / f) * f; 17866 17867 return self < 0 ? -x : x; 17868 ; 17869 } else { 17870 17871 if (($truthy(self['$nan?']()) && ($truthy(ndigits == null)))) { 17872 $Kernel.$raise($$$('FloatDomainError'), "NaN") 17873 }; 17874 ndigits = $Opal['$coerce_to!'](ndigits || 0, $$$('Integer'), "to_int"); 17875 if ($truthy($rb_le(ndigits, 0))) { 17876 if ($truthy(self['$nan?']())) { 17877 $Kernel.$raise($$$('RangeError'), "NaN") 17878 } else if ($truthy(self['$infinite?']())) { 17879 $Kernel.$raise($$$('FloatDomainError'), "Infinity") 17880 } 17881 } else if ($eqeq(ndigits, 0)) { 17882 return Math.round(self) 17883 } else if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 17884 return self 17885 }; 17886 $b = $$$('Math').$frexp(self), $a = $to_ary($b), (_ = ($a[0] == null ? nil : $a[0])), (exp = ($a[1] == null ? nil : $a[1])), $b; 17887 if ($truthy($rb_ge(ndigits, $rb_minus($rb_plus($$$($$$('Float'), 'DIG'), 2), ($truthy($rb_gt(exp, 0)) ? ($rb_divide(exp, 4)) : ($rb_minus($rb_divide(exp, 3), 1))))))) { 17888 return self 17889 }; 17890 if ($truthy($rb_lt(ndigits, ($truthy($rb_gt(exp, 0)) ? ($rb_plus($rb_divide(exp, 3), 1)) : ($rb_divide(exp, 4)))['$-@']()))) { 17891 return 0 17892 }; 17893 return Math.round(self * Math.pow(10, ndigits)) / Math.pow(10, ndigits);; 17894 }; 17895 }, -1); 17896 17897 $def(self, '$times', function $$times() { 17898 var block = $$times.$$p || nil, self = this; 17899 17900 $$times.$$p = null; 17901 17902 ; 17903 if (!$truthy(block)) { 17904 return $send(self, 'enum_for', ["times"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 17905 17906 return self}, {$$s: self}) 17907 }; 17908 17909 for (var i = 0; i < self; i++) { 17910 block(i); 17911 } 17912 ; 17913 return self; 17914 }); 17915 17916 $def(self, '$to_f', $return_self); 17917 17918 $def(self, '$to_i', function $$to_i() { 17919 var self = this; 17920 17921 return self < 0 ? Math.ceil(self) : Math.floor(self); 17922 }); 17923 17924 $def(self, '$to_r', function $$to_r() { 17925 var $a, $b, self = this, f = nil, e = nil; 17926 17927 if ($eqeqeq($$$('Integer'), self)) { 17928 return $$$('Rational').$new(self, 1) 17929 } else { 17930 17931 $b = $$$('Math').$frexp(self), $a = $to_ary($b), (f = ($a[0] == null ? nil : $a[0])), (e = ($a[1] == null ? nil : $a[1])), $b; 17932 f = $$$('Math').$ldexp(f, $$$($$$('Float'), 'MANT_DIG')).$to_i(); 17933 e = $rb_minus(e, $$$($$$('Float'), 'MANT_DIG')); 17934 return $rb_times(f, $$$($$$('Float'), 'RADIX')['$**'](e)).$to_r(); 17935 } 17936 }); 17937 17938 $def(self, '$to_s', function $$to_s(base) { 17939 var self = this; 17940 17941 17942 if (base == null) base = 10; 17943 base = $Opal['$coerce_to!'](base, $$$('Integer'), "to_int"); 17944 if (($truthy($rb_lt(base, 2)) || ($truthy($rb_gt(base, 36))))) { 17945 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)) 17946 }; 17947 if (($eqeq(self, 0) && ($truthy(1/self === -Infinity)))) { 17948 return "-0.0" 17949 }; 17950 return self.toString(base);; 17951 }, -1); 17952 17953 $def(self, '$truncate', function $$truncate(ndigits) { 17954 var self = this; 17955 17956 17957 if (ndigits == null) ndigits = 0; 17958 17959 var f = self.$to_f(); 17960 17961 if (f % 1 === 0 && ndigits >= 0) { 17962 return f; 17963 } 17964 17965 var factor = Math.pow(10, ndigits), 17966 result = parseInt(f * factor, 10) / factor; 17967 17968 if (f % 1 === 0) { 17969 result = Math.round(result); 17970 } 17971 17972 return result; 17973 ; 17974 }, -1); 17975 17976 $def(self, '$digits', function $$digits(base) { 17977 var self = this; 17978 17979 17980 if (base == null) base = 10; 17981 if ($rb_lt(self, 0)) { 17982 $Kernel.$raise($$$($$$('Math'), 'DomainError'), "out of domain") 17983 }; 17984 base = $Opal['$coerce_to!'](base, $$$('Integer'), "to_int"); 17985 if ($truthy($rb_lt(base, 2))) { 17986 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)) 17987 }; 17988 17989 if (self != parseInt(self)) $Kernel.$raise($$$('NoMethodError'), "undefined method `digits' for " + (self.$inspect())) 17990 17991 var value = self, result = []; 17992 17993 if (self == 0) { 17994 return [0]; 17995 } 17996 17997 while (value != 0) { 17998 result.push(value % base); 17999 value = parseInt(value / base, 10); 18000 } 18001 18002 return result; 18003 ; 18004 }, -1); 18005 18006 $def(self, '$divmod', function $$divmod(other) { 18007 var $yield = $$divmod.$$p || nil, self = this; 18008 18009 $$divmod.$$p = null; 18010 if (($truthy(self['$nan?']()) || ($truthy(other['$nan?']())))) { 18011 return $Kernel.$raise($$$('FloatDomainError'), "NaN") 18012 } else if ($truthy(self['$infinite?']())) { 18013 return $Kernel.$raise($$$('FloatDomainError'), "Infinity") 18014 } else { 18015 return $send2(self, $find_super(self, 'divmod', $$divmod, false, true), 'divmod', [other], $yield) 18016 } 18017 }); 18018 18019 $def(self, '$upto', function $$upto(stop) { 18020 var block = $$upto.$$p || nil, self = this; 18021 18022 $$upto.$$p = null; 18023 18024 ; 18025 if (!(block !== nil)) { 18026 return $send(self, 'enum_for', ["upto", stop], function $$34(){var self = $$34.$$s == null ? this : $$34.$$s; 18027 18028 18029 if (!$eqeqeq($$$('Numeric'), stop)) { 18030 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed") 18031 }; 18032 if ($truthy($rb_lt(stop, self))) { 18033 return 0 18034 } else { 18035 return $rb_plus($rb_minus(stop, self), 1) 18036 };}, {$$s: self}) 18037 }; 18038 18039 if (!stop.$$is_number) { 18040 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed") 18041 } 18042 for (var i = self; i <= stop; i++) { 18043 block(i); 18044 } 18045 ; 18046 return self; 18047 }); 18048 18049 $def(self, '$zero?', function $Number_zero$ques$35() { 18050 var self = this; 18051 18052 return self == 0; 18053 }); 18054 18055 $def(self, '$size', $return_val(4)); 18056 18057 $def(self, '$nan?', function $Number_nan$ques$36() { 18058 var self = this; 18059 18060 return isNaN(self); 18061 }); 18062 18063 $def(self, '$finite?', function $Number_finite$ques$37() { 18064 var self = this; 18065 18066 return self != Infinity && self != -Infinity && !isNaN(self); 18067 }); 18068 18069 $def(self, '$infinite?', function $Number_infinite$ques$38() { 18070 var self = this; 18071 18072 18073 if (self == Infinity) { 18074 return +1; 18075 } 18076 else if (self == -Infinity) { 18077 return -1; 18078 } 18079 else { 18080 return nil; 18081 } 18082 18083 }); 18084 18085 $def(self, '$positive?', function $Number_positive$ques$39() { 18086 var self = this; 18087 18088 return self != 0 && (self == Infinity || 1 / self > 0); 18089 }); 18090 18091 $def(self, '$negative?', function $Number_negative$ques$40() { 18092 var self = this; 18093 18094 return self == -Infinity || 1 / self < 0; 18095 }); 18096 18097 function numberToUint8Array(num) { 18098 var uint8array = new Uint8Array(8); 18099 new DataView(uint8array.buffer).setFloat64(0, num, true); 18100 return uint8array; 18101 } 18102 18103 function uint8ArrayToNumber(arr) { 18104 return new DataView(arr.buffer).getFloat64(0, true); 18105 } 18106 18107 function incrementNumberBit(num) { 18108 var arr = numberToUint8Array(num); 18109 for (var i = 0; i < arr.length; i++) { 18110 if (arr[i] === 0xff) { 18111 arr[i] = 0; 18112 } else { 18113 arr[i]++; 18114 break; 18115 } 18116 } 18117 return uint8ArrayToNumber(arr); 18118 } 18119 18120 function decrementNumberBit(num) { 18121 var arr = numberToUint8Array(num); 18122 for (var i = 0; i < arr.length; i++) { 18123 if (arr[i] === 0) { 18124 arr[i] = 0xff; 18125 } else { 18126 arr[i]--; 18127 break; 18128 } 18129 } 18130 return uint8ArrayToNumber(arr); 18131 } 18132 ; 18133 18134 $def(self, '$next_float', function $$next_float() { 18135 var self = this; 18136 18137 18138 if ($eqeq(self, $$$($$$('Float'), 'INFINITY'))) { 18139 return $$$($$$('Float'), 'INFINITY') 18140 }; 18141 if ($truthy(self['$nan?']())) { 18142 return $$$($$$('Float'), 'NAN') 18143 }; 18144 if ($rb_ge(self, 0)) { 18145 return incrementNumberBit(Math.abs(self)); 18146 } else { 18147 return decrementNumberBit(self); 18148 }; 18149 }); 18150 18151 $def(self, '$prev_float', function $$prev_float() { 18152 var self = this; 18153 18154 18155 if ($eqeq(self, $$$($$$('Float'), 'INFINITY')['$-@']())) { 18156 return $$$($$$('Float'), 'INFINITY')['$-@']() 18157 }; 18158 if ($truthy(self['$nan?']())) { 18159 return $$$($$$('Float'), 'NAN') 18160 }; 18161 if ($rb_gt(self, 0)) { 18162 return decrementNumberBit(self); 18163 } else { 18164 return -incrementNumberBit(Math.abs(self)); 18165 }; 18166 }); 18167 $alias(self, "arg", "angle"); 18168 $alias(self, "eql?", "=="); 18169 $alias(self, "fdiv", "/"); 18170 $alias(self, "inspect", "to_s"); 18171 $alias(self, "kind_of?", "is_a?"); 18172 $alias(self, "magnitude", "abs"); 18173 $alias(self, "modulo", "%"); 18174 $alias(self, "object_id", "__id__"); 18175 $alias(self, "phase", "angle"); 18176 $alias(self, "succ", "next"); 18177 return $alias(self, "to_int", "to_i"); 18178 })('::', $$$('Numeric'), $nesting); 18179 $const_set('::', 'Fixnum', $$$('Number')); 18180 (function($base, $super, $parent_nesting) { 18181 var self = $klass($base, $super, 'Integer'); 18182 18183 var $nesting = [self].concat($parent_nesting); 18184 18185 18186 self.$$is_number_class = true; 18187 self.$$is_integer_class = true; 18188 (function(self, $parent_nesting) { 18189 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 18190 18191 18192 18193 $def(self, '$allocate', function $$allocate() { 18194 var self = this; 18195 18196 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 18197 }); 18198 18199 Opal.udef(self, '$' + "new");; 18200 18201 $def(self, '$sqrt', function $$sqrt(n) { 18202 18203 18204 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 18205 18206 if (n < 0) { 18207 $Kernel.$raise($$$($$$('Math'), 'DomainError'), "Numerical argument is out of domain - \"isqrt\"") 18208 } 18209 18210 return parseInt(Math.sqrt(n), 10); 18211 ; 18212 }); 18213 return $def(self, '$try_convert', function $$try_convert(object) { 18214 var self = this; 18215 18216 return $$('Opal')['$coerce_to?'](object, self, "to_int") 18217 }); 18218 })(Opal.get_singleton_class(self), $nesting); 18219 $const_set(self, 'MAX', Math.pow(2, 30) - 1); 18220 return $const_set(self, 'MIN', -Math.pow(2, 30)); 18221 })('::', $$$('Numeric'), $nesting); 18222 return (function($base, $super, $parent_nesting) { 18223 var self = $klass($base, $super, 'Float'); 18224 18225 var $nesting = [self].concat($parent_nesting); 18226 18227 18228 self.$$is_number_class = true; 18229 (function(self, $parent_nesting) { 18230 18231 18232 18233 $def(self, '$allocate', function $$allocate() { 18234 var self = this; 18235 18236 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 18237 }); 18238 18239 Opal.udef(self, '$' + "new");; 18240 return $def(self, '$===', function $eq_eq_eq$41(other) { 18241 18242 return !!other.$$is_number; 18243 }); 18244 })(Opal.get_singleton_class(self), $nesting); 18245 $const_set(self, 'INFINITY', Infinity); 18246 $const_set(self, 'MAX', Number.MAX_VALUE); 18247 $const_set(self, 'MIN', Number.MIN_VALUE); 18248 $const_set(self, 'NAN', NaN); 18249 $const_set(self, 'DIG', 15); 18250 $const_set(self, 'MANT_DIG', 53); 18251 $const_set(self, 'RADIX', 2); 18252 return $const_set(self, 'EPSILON', Number.EPSILON || 2.2204460492503130808472633361816E-16); 18253 })('::', $$$('Numeric'), $nesting); 18254}; 18255 18256Opal.modules["corelib/range"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18257 var $klass = Opal.klass, $truthy = Opal.truthy, $Kernel = Opal.Kernel, $def = Opal.def, $not = Opal.not, $send2 = Opal.send2, $find_super = Opal.find_super, $rb_lt = Opal.rb_lt, $rb_le = Opal.rb_le, $send = Opal.send, $eqeq = Opal.eqeq, $eqeqeq = Opal.eqeqeq, $return_ivar = Opal.return_ivar, $rb_gt = Opal.rb_gt, $rb_minus = Opal.rb_minus, $Opal = Opal.Opal, $rb_divide = Opal.rb_divide, $rb_plus = Opal.rb_plus, $rb_times = Opal.rb_times, $rb_ge = Opal.rb_ge, $thrower = Opal.thrower, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 18258 18259 Opal.add_stubs('require,include,attr_reader,raise,nil?,<=>,include?,!,<,<=,enum_for,size,upto,to_proc,respond_to?,class,succ,==,===,exclude_end?,eql?,begin,end,last,to_a,>,-@,-,to_i,coerce_to!,ceil,/,is_a?,new,loop,+,*,>=,each_with_index,%,step,bsearch,inspect,[],hash,cover?'); 18260 18261 self.$require("corelib/enumerable"); 18262 return (function($base, $super, $parent_nesting) { 18263 var self = $klass($base, $super, 'Range'); 18264 18265 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 18266 18267 $proto.begin = $proto.end = $proto.excl = nil; 18268 18269 self.$include($$$('Enumerable')); 18270 self.$$prototype.$$is_range = true; 18271 self.$attr_reader("begin", "end"); 18272 18273 $def(self, '$initialize', function $$initialize(first, last, exclude) { 18274 var self = this; 18275 18276 18277 if (exclude == null) exclude = false; 18278 if ($truthy(self.begin)) { 18279 $Kernel.$raise($$$('NameError'), "'initialize' called twice") 18280 }; 18281 if (!(($truthy(first['$<=>'](last)) || ($truthy(first['$nil?']()))) || ($truthy(last['$nil?']())))) { 18282 $Kernel.$raise($$$('ArgumentError'), "bad value for range") 18283 }; 18284 self.begin = first; 18285 self.end = last; 18286 return (self.excl = exclude); 18287 }, -3); 18288 18289 $def(self, '$===', function $Range_$eq_eq_eq$1(value) { 18290 var self = this; 18291 18292 return self['$include?'](value) 18293 }); 18294 18295 function is_infinite(self) { 18296 if (self.begin === nil || self.end === nil || 18297 self.begin === -Infinity || self.end === Infinity || 18298 self.begin === Infinity || self.end === -Infinity) return true; 18299 return false; 18300 } 18301 ; 18302 18303 $def(self, '$count', function $$count() { 18304 var block = $$count.$$p || nil, self = this; 18305 18306 $$count.$$p = null; 18307 18308 ; 18309 if (($not((block !== nil)) && ($truthy(is_infinite(self))))) { 18310 return $$$($$$('Float'), 'INFINITY') 18311 }; 18312 return $send2(self, $find_super(self, 'count', $$count, false, true), 'count', [], block); 18313 }); 18314 18315 $def(self, '$to_a', function $$to_a() { 18316 var $yield = $$to_a.$$p || nil, self = this; 18317 18318 $$to_a.$$p = null; 18319 18320 if ($truthy(is_infinite(self))) { 18321 $Kernel.$raise($$$('TypeError'), "cannot convert endless range to an array") 18322 }; 18323 return $send2(self, $find_super(self, 'to_a', $$to_a, false, true), 'to_a', [], $yield); 18324 }); 18325 18326 $def(self, '$cover?', function $Range_cover$ques$2(value) { 18327 var self = this, beg_cmp = nil, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, end_cmp = nil; 18328 18329 18330 beg_cmp = ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = ($truthy(($ret_or_3 = self.begin['$nil?']())) ? (-1) : ($ret_or_3)))) ? ($ret_or_2) : (self.begin['$<=>'](value))))) && ($ret_or_1)); 18331 end_cmp = ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = ($truthy(($ret_or_3 = self.end['$nil?']())) ? (-1) : ($ret_or_3)))) ? ($ret_or_2) : (value['$<=>'](self.end))))) && ($ret_or_1)); 18332 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = ($truthy(self.excl) ? (($truthy(($ret_or_3 = end_cmp)) ? ($rb_lt(end_cmp, 0)) : ($ret_or_3))) : ($truthy(($ret_or_3 = end_cmp)) ? ($rb_le(end_cmp, 0)) : ($ret_or_3))))) ? (beg_cmp) : ($ret_or_2))))) { 18333 return $rb_le(beg_cmp, 0) 18334 } else { 18335 return $ret_or_1 18336 }; 18337 }); 18338 18339 $def(self, '$each', function $$each() { 18340 var block = $$each.$$p || nil, self = this, current = nil, last = nil, $ret_or_1 = nil; 18341 18342 $$each.$$p = null; 18343 18344 ; 18345 if (!(block !== nil)) { 18346 return $send(self, 'enum_for', ["each"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 18347 18348 return self.$size()}, {$$s: self}) 18349 }; 18350 18351 var i, limit; 18352 18353 if (self.begin.$$is_number && self.end.$$is_number) { 18354 if (self.begin % 1 !== 0 || self.end % 1 !== 0) { 18355 $Kernel.$raise($$$('TypeError'), "can't iterate from Float") 18356 } 18357 18358 for (i = self.begin, limit = self.end + ($truthy(self.excl) ? (0) : (1)); i < limit; i++) { 18359 block(i); 18360 } 18361 18362 return self; 18363 } 18364 18365 if (self.begin.$$is_string && self.end.$$is_string) { 18366 $send(self.begin, 'upto', [self.end, self.excl], block.$to_proc()) 18367 return self; 18368 } 18369 ; 18370 current = self.begin; 18371 last = self.end; 18372 if (!$truthy(current['$respond_to?']("succ"))) { 18373 $Kernel.$raise($$$('TypeError'), "can't iterate from " + (current.$class())) 18374 }; 18375 while ($truthy(($truthy(($ret_or_1 = self.end['$nil?']())) ? ($ret_or_1) : ($rb_lt(current['$<=>'](last), 0))))) { 18376 18377 Opal.yield1(block, current); 18378 current = current.$succ(); 18379 }; 18380 if (($not(self.excl) && ($eqeq(current, last)))) { 18381 Opal.yield1(block, current) 18382 }; 18383 return self; 18384 }); 18385 18386 $def(self, '$eql?', function $Range_eql$ques$4(other) { 18387 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 18388 18389 18390 if (!$eqeqeq($$$('Range'), other)) { 18391 return false 18392 }; 18393 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = self.excl['$==='](other['$exclude_end?']()))) ? (self.begin['$eql?'](other.$begin())) : ($ret_or_2))))) { 18394 return self.end['$eql?'](other.$end()) 18395 } else { 18396 return $ret_or_1 18397 }; 18398 }); 18399 18400 $def(self, '$exclude_end?', $return_ivar("excl")); 18401 18402 $def(self, '$first', function $$first(n) { 18403 var $yield = $$first.$$p || nil, self = this; 18404 18405 $$first.$$p = null; 18406 18407 ; 18408 if ($truthy(self.begin['$nil?']())) { 18409 $Kernel.$raise($$$('RangeError'), "cannot get the minimum of beginless range") 18410 }; 18411 if ($truthy(n == null)) { 18412 return self.begin 18413 }; 18414 return $send2(self, $find_super(self, 'first', $$first, false, true), 'first', [n], $yield); 18415 }, -1); 18416 18417 $def(self, '$last', function $$last(n) { 18418 var self = this; 18419 18420 18421 ; 18422 if ($truthy(self.end['$nil?']())) { 18423 $Kernel.$raise($$$('RangeError'), "cannot get the maximum of endless range") 18424 }; 18425 if ($truthy(n == null)) { 18426 return self.end 18427 }; 18428 return self.$to_a().$last(n); 18429 }, -1); 18430 18431 $def(self, '$max', function $$max() { 18432 var $yield = $$max.$$p || nil, self = this; 18433 18434 $$max.$$p = null; 18435 if ($truthy(self.end['$nil?']())) { 18436 return $Kernel.$raise($$$('RangeError'), "cannot get the maximum of endless range") 18437 } else if (($yield !== nil)) { 18438 return $send2(self, $find_super(self, 'max', $$max, false, true), 'max', [], $yield) 18439 } else if (($not(self.begin['$nil?']()) && (($truthy($rb_gt(self.begin, self.end)) || (($truthy(self.excl) && ($eqeq(self.begin, self.end)))))))) { 18440 return nil 18441 } else { 18442 return self.excl ? self.end - 1 : self.end 18443 } 18444 }); 18445 18446 $def(self, '$min', function $$min() { 18447 var $yield = $$min.$$p || nil, self = this; 18448 18449 $$min.$$p = null; 18450 if ($truthy(self.begin['$nil?']())) { 18451 return $Kernel.$raise($$$('RangeError'), "cannot get the minimum of beginless range") 18452 } else if (($yield !== nil)) { 18453 return $send2(self, $find_super(self, 'min', $$min, false, true), 'min', [], $yield) 18454 } else if (($not(self.end['$nil?']()) && (($truthy($rb_gt(self.begin, self.end)) || (($truthy(self.excl) && ($eqeq(self.begin, self.end)))))))) { 18455 return nil 18456 } else { 18457 return self.begin 18458 } 18459 }); 18460 18461 $def(self, '$size', function $$size() { 18462 var self = this, infinity = nil, range_begin = nil, range_end = nil; 18463 18464 18465 infinity = $$$($$$('Float'), 'INFINITY'); 18466 if ((($eqeq(self.begin, infinity) && ($not(self.end['$nil?']()))) || (($eqeq(self.end, infinity['$-@']()) && ($not(self.begin['$nil?']())))))) { 18467 return 0 18468 }; 18469 if ($truthy(is_infinite(self))) { 18470 return infinity 18471 }; 18472 if (!($eqeqeq($$$('Numeric'), self.begin) && ($eqeqeq($$$('Numeric'), self.end)))) { 18473 return nil 18474 }; 18475 range_begin = self.begin; 18476 range_end = self.end; 18477 if ($truthy(self.excl)) { 18478 range_end = $rb_minus(range_end, 1) 18479 }; 18480 if ($truthy($rb_lt(range_end, range_begin))) { 18481 return 0 18482 }; 18483 return (Math.abs(range_end - range_begin) + 1).$to_i(); 18484 }); 18485 18486 $def(self, '$step', function $$step(n) { 18487 var $yield = $$step.$$p || nil, self = this, $ret_or_1 = nil, i = nil; 18488 18489 $$step.$$p = null; 18490 18491 ; 18492 18493 function coerceStepSize() { 18494 if (n == null) { 18495 n = 1; 18496 } 18497 else if (!n.$$is_number) { 18498 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int") 18499 } 18500 18501 if (n < 0) { 18502 $Kernel.$raise($$$('ArgumentError'), "step can't be negative") 18503 } else if (n === 0) { 18504 $Kernel.$raise($$$('ArgumentError'), "step can't be 0") 18505 } 18506 } 18507 18508 function enumeratorSize() { 18509 if (!self.begin['$respond_to?']("succ")) { 18510 return nil; 18511 } 18512 18513 if (self.begin.$$is_string && self.end.$$is_string) { 18514 return nil; 18515 } 18516 18517 if (n % 1 === 0) { 18518 return $rb_divide(self.$size(), n).$ceil(); 18519 } else { 18520 // n is a float 18521 var begin = self.begin, end = self.end, 18522 abs = Math.abs, floor = Math.floor, 18523 err = (abs(begin) + abs(end) + abs(end - begin)) / abs(n) * $$$($$$('Float'), 'EPSILON'), 18524 size; 18525 18526 if (err > 0.5) { 18527 err = 0.5; 18528 } 18529 18530 if (self.excl) { 18531 size = floor((end - begin) / n - err); 18532 if (size * n + begin < end) { 18533 size++; 18534 } 18535 } else { 18536 size = floor((end - begin) / n + err) + 1 18537 } 18538 18539 return size; 18540 } 18541 } 18542 ; 18543 if (!($yield !== nil)) { 18544 if (((($truthy(self.begin['$is_a?']($$('Numeric'))) || ($truthy(self.begin['$nil?']()))) && (($truthy(self.end['$is_a?']($$('Numeric'))) || ($truthy(self.end['$nil?']()))))) && ($not(($truthy(($ret_or_1 = self.begin['$nil?']())) ? (self.end['$nil?']()) : ($ret_or_1)))))) { 18545 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new(self, n, "step") 18546 } else { 18547 return $send(self, 'enum_for', ["step", n], function $$5(){ 18548 18549 coerceStepSize(); 18550 return enumeratorSize(); 18551 }) 18552 } 18553 }; 18554 coerceStepSize(); 18555 if ($truthy(self.begin.$$is_number && self.end.$$is_number)) { 18556 18557 i = 0; 18558 (function(){try { var $t_break = $thrower('break'); return $send(self, 'loop', [], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s, current = nil; 18559 if (self.begin == null) self.begin = nil; 18560 if (self.excl == null) self.excl = nil; 18561 if (self.end == null) self.end = nil; 18562 18563 18564 current = $rb_plus(self.begin, $rb_times(i, n)); 18565 if ($truthy(self.excl)) { 18566 if ($truthy($rb_ge(current, self.end))) { 18567 $t_break.$throw() 18568 } 18569 } else if ($truthy($rb_gt(current, self.end))) { 18570 $t_break.$throw() 18571 }; 18572 Opal.yield1($yield, current); 18573 return (i = $rb_plus(i, 1));}, {$$s: self})} catch($e) { 18574 if ($e === $t_break) return $e.$v; 18575 throw $e; 18576 }})(); 18577 } else { 18578 18579 18580 if (self.begin.$$is_string && self.end.$$is_string && n % 1 !== 0) { 18581 $Kernel.$raise($$$('TypeError'), "no implicit conversion to float from string") 18582 } 18583 ; 18584 $send(self, 'each_with_index', [], function $$7(value, idx){ 18585 18586 if (value == null) value = nil; 18587 if (idx == null) idx = nil; 18588 if ($eqeq(idx['$%'](n), 0)) { 18589 return Opal.yield1($yield, value); 18590 } else { 18591 return nil 18592 };}); 18593 }; 18594 return self; 18595 }, -1); 18596 18597 $def(self, '$%', function $Range_$percent$8(n) { 18598 var self = this; 18599 18600 if (($truthy(self.begin['$is_a?']($$('Numeric'))) && ($truthy(self.end['$is_a?']($$('Numeric')))))) { 18601 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new(self, n, "%") 18602 } else { 18603 return self.$step(n) 18604 } 18605 }); 18606 18607 $def(self, '$bsearch', function $$bsearch() { 18608 var block = $$bsearch.$$p || nil, self = this; 18609 18610 $$bsearch.$$p = null; 18611 18612 ; 18613 if (!(block !== nil)) { 18614 return self.$enum_for("bsearch") 18615 }; 18616 if ($truthy(is_infinite(self) && (self.begin.$$is_number || self.end.$$is_number))) { 18617 $Kernel.$raise($$$('NotImplementedError'), "Can't #bsearch an infinite range") 18618 }; 18619 if (!$truthy(self.begin.$$is_number && self.end.$$is_number)) { 18620 $Kernel.$raise($$$('TypeError'), "can't do binary search for " + (self.begin.$class())) 18621 }; 18622 return $send(self.$to_a(), 'bsearch', [], block.$to_proc()); 18623 }); 18624 18625 $def(self, '$to_s', function $$to_s() { 18626 var self = this, $ret_or_1 = nil; 18627 18628 return "" + (($truthy(($ret_or_1 = self.begin)) ? ($ret_or_1) : (""))) + (($truthy(self.excl) ? ("...") : (".."))) + (($truthy(($ret_or_1 = self.end)) ? ($ret_or_1) : (""))) 18629 }); 18630 18631 $def(self, '$inspect', function $$inspect() { 18632 var self = this, $ret_or_1 = nil; 18633 18634 return "" + (($truthy(($ret_or_1 = self.begin)) ? (self.begin.$inspect()) : ($ret_or_1))) + (($truthy(self.excl) ? ("...") : (".."))) + (($truthy(($ret_or_1 = self.end)) ? (self.end.$inspect()) : ($ret_or_1))) 18635 }); 18636 18637 $def(self, '$marshal_load', function $$marshal_load(args) { 18638 var self = this; 18639 18640 18641 self.begin = args['$[]']("begin"); 18642 self.end = args['$[]']("end"); 18643 return (self.excl = args['$[]']("excl")); 18644 }); 18645 18646 $def(self, '$hash', function $$hash() { 18647 var self = this; 18648 18649 return [self.begin, self.end, self.excl].$hash() 18650 }); 18651 $alias(self, "==", "eql?"); 18652 $alias(self, "include?", "cover?"); 18653 return $alias(self, "member?", "cover?"); 18654 })('::', null, $nesting); 18655}; 18656 18657Opal.modules["corelib/proc"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18658 var $slice = Opal.slice, $klass = Opal.klass, $truthy = Opal.truthy, $Kernel = Opal.Kernel, $defs = Opal.defs, $def = Opal.def, $send = Opal.send, $to_a = Opal.to_a, $return_self = Opal.return_self, $ensure_kwargs = Opal.ensure_kwargs, $Opal = Opal.Opal, $alias = Opal.alias, nil = Opal.nil, $$$ = Opal.$$$; 18659 18660 Opal.add_stubs('raise,proc,call,to_proc,new,source_location,coerce_to!,dup'); 18661 return (function($base, $super) { 18662 var self = $klass($base, $super, 'Proc'); 18663 18664 18665 18666 Opal.prop(self.$$prototype, '$$is_proc', true); 18667 Opal.prop(self.$$prototype, '$$is_lambda', false); 18668 $defs(self, '$new', function $Proc_new$1() { 18669 var block = $Proc_new$1.$$p || nil; 18670 18671 $Proc_new$1.$$p = null; 18672 18673 ; 18674 if (!$truthy(block)) { 18675 $Kernel.$raise($$$('ArgumentError'), "tried to create a Proc object without a block") 18676 }; 18677 return block; 18678 }); 18679 18680 $def(self, '$call', function $$call($a) { 18681 var block = $$call.$$p || nil, $post_args, args, self = this; 18682 18683 $$call.$$p = null; 18684 18685 ; 18686 $post_args = $slice(arguments); 18687 args = $post_args; 18688 18689 if (block !== nil) { 18690 self.$$p = block; 18691 } 18692 18693 var result, $brk = self.$$brk, $ret = self.$$ret; 18694 18695 if ($brk || ($ret && self.$$is_lambda)) { 18696 try { 18697 if (self.$$is_lambda) { 18698 result = self.apply(null, args); 18699 } 18700 else { 18701 result = Opal.yieldX(self, args); 18702 } 18703 } catch (err) { 18704 if (err === $brk) { 18705 return err.$v; 18706 } 18707 else if (self.$$is_lambda && err === $ret) { 18708 return err.$v; 18709 } 18710 else { 18711 throw err; 18712 } 18713 } 18714 } 18715 else { 18716 if (self.$$is_lambda) { 18717 result = self.apply(null, args); 18718 } 18719 else { 18720 result = Opal.yieldX(self, args); 18721 } 18722 } 18723 18724 return result; 18725 ; 18726 }, -1); 18727 18728 $def(self, '$>>', function $Proc_$gt$gt$2(other) { 18729 var $yield = $Proc_$gt$gt$2.$$p || nil, self = this; 18730 18731 $Proc_$gt$gt$2.$$p = null; 18732 return $send($Kernel, 'proc', [], function $$3($a){var block = $$3.$$p || nil, $post_args, args, self = $$3.$$s == null ? this : $$3.$$s, out = nil; 18733 18734 $$3.$$p = null; 18735 18736 ; 18737 $post_args = $slice(arguments); 18738 args = $post_args; 18739 out = $send(self, 'call', $to_a(args), block.$to_proc()); 18740 return other.$call(out);}, {$$arity: -1, $$s: self}) 18741 }); 18742 18743 $def(self, '$<<', function $Proc_$lt$lt$4(other) { 18744 var $yield = $Proc_$lt$lt$4.$$p || nil, self = this; 18745 18746 $Proc_$lt$lt$4.$$p = null; 18747 return $send($Kernel, 'proc', [], function $$5($a){var block = $$5.$$p || nil, $post_args, args, self = $$5.$$s == null ? this : $$5.$$s, out = nil; 18748 18749 $$5.$$p = null; 18750 18751 ; 18752 $post_args = $slice(arguments); 18753 args = $post_args; 18754 out = $send(other, 'call', $to_a(args), block.$to_proc()); 18755 return self.$call(out);}, {$$arity: -1, $$s: self}) 18756 }); 18757 18758 $def(self, '$to_proc', $return_self); 18759 18760 $def(self, '$lambda?', function $Proc_lambda$ques$6() { 18761 var self = this; 18762 18763 return !!self.$$is_lambda; 18764 }); 18765 18766 $def(self, '$arity', function $$arity() { 18767 var self = this; 18768 18769 18770 if (self.$$is_curried) { 18771 return -1; 18772 } else if (self.$$arity != null) { 18773 return self.$$arity; 18774 } else { 18775 return self.length; 18776 } 18777 18778 }); 18779 18780 $def(self, '$source_location', function $$source_location() { 18781 var self = this, $ret_or_1 = nil; 18782 18783 18784 if (self.$$is_curried) { return nil; }; 18785 if ($truthy(($ret_or_1 = self.$$source_location))) { 18786 return $ret_or_1 18787 } else { 18788 return nil 18789 }; 18790 }); 18791 18792 $def(self, '$binding', function $$binding() { 18793 var $a, self = this; 18794 18795 18796 if (self.$$is_curried) { $Kernel.$raise($$$('ArgumentError'), "Can't create Binding") }; 18797 if ($truthy((($a = $$$('::', 'Binding', 'skip_raise')) ? 'constant' : nil))) { 18798 return $$$('Binding').$new(nil, [], self.$$s, self.$source_location()) 18799 } else { 18800 return nil 18801 }; 18802 }); 18803 18804 $def(self, '$parameters', function $$parameters($kwargs) { 18805 var lambda, self = this; 18806 18807 18808 $kwargs = $ensure_kwargs($kwargs); 18809 18810 lambda = $kwargs.$$smap["lambda"];; 18811 18812 if (self.$$is_curried) { 18813 return [["rest"]]; 18814 } else if (self.$$parameters) { 18815 if (lambda == null ? self.$$is_lambda : lambda) { 18816 return self.$$parameters; 18817 } else { 18818 var result = [], i, length; 18819 18820 for (i = 0, length = self.$$parameters.length; i < length; i++) { 18821 var parameter = self.$$parameters[i]; 18822 18823 if (parameter[0] === 'req') { 18824 // required arguments always have name 18825 parameter = ['opt', parameter[1]]; 18826 } 18827 18828 result.push(parameter); 18829 } 18830 18831 return result; 18832 } 18833 } else { 18834 return []; 18835 } 18836 ; 18837 }, -1); 18838 18839 $def(self, '$curry', function $$curry(arity) { 18840 var self = this; 18841 18842 18843 ; 18844 18845 if (arity === undefined) { 18846 arity = self.length; 18847 } 18848 else { 18849 arity = $Opal['$coerce_to!'](arity, $$$('Integer'), "to_int"); 18850 if (self.$$is_lambda && arity !== self.length) { 18851 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arity) + " for " + (self.length) + ")") 18852 } 18853 } 18854 18855 function curried () { 18856 var args = $slice(arguments), 18857 length = args.length, 18858 result; 18859 18860 if (length > arity && self.$$is_lambda && !self.$$is_curried) { 18861 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (length) + " for " + (arity) + ")") 18862 } 18863 18864 if (length >= arity) { 18865 return self.$call.apply(self, args); 18866 } 18867 18868 result = function () { 18869 return curried.apply(null, 18870 args.concat($slice(arguments))); 18871 } 18872 result.$$is_lambda = self.$$is_lambda; 18873 result.$$is_curried = true; 18874 18875 return result; 18876 }; 18877 18878 curried.$$is_lambda = self.$$is_lambda; 18879 curried.$$is_curried = true; 18880 return curried; 18881 ; 18882 }, -1); 18883 18884 $def(self, '$dup', function $$dup() { 18885 var self = this; 18886 18887 18888 var original_proc = self.$$original_proc || self, 18889 proc = function () { 18890 return original_proc.apply(this, arguments); 18891 }; 18892 18893 for (var prop in self) { 18894 if (self.hasOwnProperty(prop)) { 18895 proc[prop] = self[prop]; 18896 } 18897 } 18898 18899 return proc; 18900 18901 }); 18902 $alias(self, "===", "call"); 18903 $alias(self, "clone", "dup"); 18904 $alias(self, "yield", "call"); 18905 return $alias(self, "[]", "call"); 18906 })('::', Function) 18907}; 18908 18909Opal.modules["corelib/method"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18910 var $klass = Opal.klass, $def = Opal.def, $truthy = Opal.truthy, $slice = Opal.slice, $alias = Opal.alias, $Kernel = Opal.Kernel, $send = Opal.send, $to_a = Opal.to_a, nil = Opal.nil, $$$ = Opal.$$$; 18911 18912 Opal.add_stubs('attr_reader,arity,curry,>>,<<,new,class,join,source_location,call,raise,bind,to_proc'); 18913 18914 (function($base, $super) { 18915 var self = $klass($base, $super, 'Method'); 18916 18917 var $proto = self.$$prototype; 18918 18919 $proto.method = $proto.receiver = $proto.owner = $proto.name = nil; 18920 18921 self.$attr_reader("owner", "receiver", "name"); 18922 18923 $def(self, '$initialize', function $$initialize(receiver, owner, method, name) { 18924 var self = this; 18925 18926 18927 self.receiver = receiver; 18928 self.owner = owner; 18929 self.name = name; 18930 return (self.method = method); 18931 }); 18932 18933 $def(self, '$arity', function $$arity() { 18934 var self = this; 18935 18936 return self.method.$arity() 18937 }); 18938 18939 $def(self, '$parameters', function $$parameters() { 18940 var self = this; 18941 18942 return self.method.$$parameters 18943 }); 18944 18945 $def(self, '$source_location', function $$source_location() { 18946 var self = this, $ret_or_1 = nil; 18947 18948 if ($truthy(($ret_or_1 = self.method.$$source_location))) { 18949 return $ret_or_1 18950 } else { 18951 return ["(eval)", 0] 18952 } 18953 }); 18954 18955 $def(self, '$comments', function $$comments() { 18956 var self = this, $ret_or_1 = nil; 18957 18958 if ($truthy(($ret_or_1 = self.method.$$comments))) { 18959 return $ret_or_1 18960 } else { 18961 return [] 18962 } 18963 }); 18964 18965 $def(self, '$call', function $$call($a) { 18966 var block = $$call.$$p || nil, $post_args, args, self = this; 18967 18968 $$call.$$p = null; 18969 18970 ; 18971 $post_args = $slice(arguments); 18972 args = $post_args; 18973 18974 self.method.$$p = block; 18975 18976 return self.method.apply(self.receiver, args); 18977 ; 18978 }, -1); 18979 18980 $def(self, '$curry', function $$curry(arity) { 18981 var self = this; 18982 18983 18984 ; 18985 return self.method.$curry(arity); 18986 }, -1); 18987 18988 $def(self, '$>>', function $Method_$gt$gt$1(other) { 18989 var self = this; 18990 18991 return self.method['$>>'](other) 18992 }); 18993 18994 $def(self, '$<<', function $Method_$lt$lt$2(other) { 18995 var self = this; 18996 18997 return self.method['$<<'](other) 18998 }); 18999 19000 $def(self, '$unbind', function $$unbind() { 19001 var self = this; 19002 19003 return $$$('UnboundMethod').$new(self.receiver.$class(), self.owner, self.method, self.name) 19004 }); 19005 19006 $def(self, '$to_proc', function $$to_proc() { 19007 var self = this; 19008 19009 19010 var proc = self.$call.bind(self); 19011 proc.$$unbound = self.method; 19012 proc.$$is_lambda = true; 19013 proc.$$arity = self.method.$$arity == null ? self.method.length : self.method.$$arity; 19014 proc.$$parameters = self.method.$$parameters; 19015 return proc; 19016 19017 }); 19018 19019 $def(self, '$inspect', function $$inspect() { 19020 var self = this; 19021 19022 return "#<" + (self.$class()) + ": " + (self.receiver.$class()) + "#" + (self.name) + " (defined in " + (self.owner) + " in " + (self.$source_location().$join(":")) + ")>" 19023 }); 19024 $alias(self, "[]", "call"); 19025 return $alias(self, "===", "call"); 19026 })('::', null); 19027 return (function($base, $super) { 19028 var self = $klass($base, $super, 'UnboundMethod'); 19029 19030 var $proto = self.$$prototype; 19031 19032 $proto.method = $proto.owner = $proto.name = $proto.source = nil; 19033 19034 self.$attr_reader("source", "owner", "name"); 19035 19036 $def(self, '$initialize', function $$initialize(source, owner, method, name) { 19037 var self = this; 19038 19039 19040 self.source = source; 19041 self.owner = owner; 19042 self.method = method; 19043 return (self.name = name); 19044 }); 19045 19046 $def(self, '$arity', function $$arity() { 19047 var self = this; 19048 19049 return self.method.$arity() 19050 }); 19051 19052 $def(self, '$parameters', function $$parameters() { 19053 var self = this; 19054 19055 return self.method.$$parameters 19056 }); 19057 19058 $def(self, '$source_location', function $$source_location() { 19059 var self = this, $ret_or_1 = nil; 19060 19061 if ($truthy(($ret_or_1 = self.method.$$source_location))) { 19062 return $ret_or_1 19063 } else { 19064 return ["(eval)", 0] 19065 } 19066 }); 19067 19068 $def(self, '$comments', function $$comments() { 19069 var self = this, $ret_or_1 = nil; 19070 19071 if ($truthy(($ret_or_1 = self.method.$$comments))) { 19072 return $ret_or_1 19073 } else { 19074 return [] 19075 } 19076 }); 19077 19078 $def(self, '$bind', function $$bind(object) { 19079 var self = this; 19080 19081 19082 if (self.owner.$$is_module || Opal.is_a(object, self.owner)) { 19083 return $$$('Method').$new(object, self.owner, self.method, self.name); 19084 } 19085 else { 19086 $Kernel.$raise($$$('TypeError'), "can't bind singleton method to a different class (expected " + (object) + ".kind_of?(" + (self.owner) + " to be true)"); 19087 } 19088 19089 }); 19090 19091 $def(self, '$bind_call', function $$bind_call(object, $a) { 19092 var block = $$bind_call.$$p || nil, $post_args, args, self = this; 19093 19094 $$bind_call.$$p = null; 19095 19096 ; 19097 $post_args = $slice(arguments, 1); 19098 args = $post_args; 19099 return $send(self.$bind(object), 'call', $to_a(args), block.$to_proc()); 19100 }, -2); 19101 return $def(self, '$inspect', function $$inspect() { 19102 var self = this; 19103 19104 return "#<" + (self.$class()) + ": " + (self.source) + "#" + (self.name) + " (defined in " + (self.owner) + " in " + (self.$source_location().$join(":")) + ")>" 19105 }); 19106 })('::', null); 19107}; 19108 19109Opal.modules["corelib/variables"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19110 var $gvars = Opal.gvars, $const_set = Opal.const_set, $Object = Opal.Object, $hash2 = Opal.hash2, nil = Opal.nil; 19111 19112 Opal.add_stubs('new'); 19113 19114 $gvars['&'] = $gvars['~'] = $gvars['`'] = $gvars["'"] = nil; 19115 $gvars.LOADED_FEATURES = ($gvars["\""] = Opal.loaded_features); 19116 $gvars.LOAD_PATH = ($gvars[":"] = []); 19117 $gvars["/"] = "\n"; 19118 $gvars[","] = nil; 19119 $const_set('::', 'ARGV', []); 19120 $const_set('::', 'ARGF', $Object.$new()); 19121 $const_set('::', 'ENV', $hash2([], {})); 19122 $gvars.VERBOSE = false; 19123 $gvars.DEBUG = false; 19124 return ($gvars.SAFE = 0); 19125}; 19126 19127Opal.modules["corelib/io"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19128 var $klass = Opal.klass, $const_set = Opal.const_set, $not = Opal.not, $truthy = Opal.truthy, $def = Opal.def, $return_ivar = Opal.return_ivar, $return_val = Opal.return_val, $slice = Opal.slice, $Kernel = Opal.Kernel, $gvars = Opal.gvars, $send = Opal.send, $to_a = Opal.to_a, $rb_plus = Opal.rb_plus, $neqeq = Opal.neqeq, $range = Opal.range, $hash2 = Opal.hash2, $eqeq = Opal.eqeq, $to_ary = Opal.to_ary, $rb_gt = Opal.rb_gt, $assign_ivar_val = Opal.assign_ivar_val, $alias = Opal.alias, $a, nil = Opal.nil, $$$ = Opal.$$$; 19129 19130 Opal.add_stubs('attr_reader,attr_accessor,!,match?,include?,size,write,String,flatten,puts,sysread_noraise,+,!=,[],ord,getc,readchar,raise,gets,==,to_str,length,split,sub,sysread,>,to_a,each_line,enum_for,getbyte,closed_write?,closed_read?,each,eof,new,write_proc=,read_proc='); 19131 19132 (function($base, $super) { 19133 var self = $klass($base, $super, 'IO'); 19134 19135 var $proto = self.$$prototype; 19136 19137 $proto.read_buffer = $proto.closed = nil; 19138 19139 $const_set(self, 'SEEK_SET', 0); 19140 $const_set(self, 'SEEK_CUR', 1); 19141 $const_set(self, 'SEEK_END', 2); 19142 $const_set(self, 'SEEK_DATA', 3); 19143 $const_set(self, 'SEEK_HOLE', 4); 19144 $const_set(self, 'READABLE', 1); 19145 $const_set(self, 'WRITABLE', 4); 19146 self.$attr_reader("eof"); 19147 self.$attr_accessor("read_proc", "sync", "tty", "write_proc"); 19148 19149 $def(self, '$initialize', function $$initialize(fd, flags) { 19150 var self = this; 19151 19152 19153 if (flags == null) flags = "r"; 19154 self.fd = fd; 19155 self.flags = flags; 19156 self.eof = false; 19157 if (($truthy(flags['$include?']("r")) && ($not(flags['$match?'](/[wa+]/))))) { 19158 return (self.closed = "write") 19159 } else if (($truthy(flags['$match?'](/[wa]/)) && ($not(flags['$match?'](/[r+]/))))) { 19160 return (self.closed = "read") 19161 } else { 19162 return nil 19163 }; 19164 }, -2); 19165 19166 $def(self, '$fileno', $return_ivar("fd")); 19167 19168 $def(self, '$tty?', function $IO_tty$ques$1() { 19169 var self = this; 19170 19171 return self.tty == true; 19172 }); 19173 19174 $def(self, '$write', function $$write(string) { 19175 var self = this; 19176 19177 19178 self.write_proc(string); 19179 return string.$size(); 19180 }); 19181 19182 $def(self, '$flush', $return_val(nil)); 19183 19184 $def(self, '$<<', function $IO_$lt$lt$2(string) { 19185 var self = this; 19186 19187 19188 self.$write(string); 19189 return self; 19190 }); 19191 19192 $def(self, '$print', function $$print($a) { 19193 var $post_args, args, self = this; 19194 if ($gvars[","] == null) $gvars[","] = nil; 19195 19196 19197 $post_args = $slice(arguments); 19198 args = $post_args; 19199 19200 for (var i = 0, ii = args.length; i < ii; i++) { 19201 args[i] = $Kernel.$String(args[i]) 19202 } 19203 self.$write(args.join($gvars[","])); 19204 ; 19205 return nil; 19206 }, -1); 19207 19208 $def(self, '$puts', function $$puts($a) { 19209 var $post_args, args, self = this; 19210 19211 19212 $post_args = $slice(arguments); 19213 args = $post_args; 19214 19215 var line 19216 if (args.length === 0) { 19217 self.$write("\n"); 19218 return nil; 19219 } else { 19220 for (var i = 0, ii = args.length; i < ii; i++) { 19221 if (args[i].$$is_array){ 19222 var ary = (args[i]).$flatten() 19223 if (ary.length > 0) $send(self, 'puts', $to_a((ary))) 19224 } else { 19225 if (args[i].$$is_string) { 19226 line = args[i].valueOf(); 19227 } else { 19228 line = $Kernel.$String(args[i]); 19229 } 19230 if (!line.endsWith("\n")) line += "\n" 19231 self.$write(line) 19232 } 19233 } 19234 } 19235 ; 19236 return nil; 19237 }, -1); 19238 19239 $def(self, '$getc', function $$getc() { 19240 var self = this, $ret_or_1 = nil, parts = nil, ret = nil; 19241 19242 19243 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 19244 parts = ""; 19245 do { 19246 19247 self.read_buffer = $rb_plus(self.read_buffer, parts); 19248 if ($neqeq(self.read_buffer, "")) { 19249 19250 ret = self.read_buffer['$[]'](0); 19251 self.read_buffer = self.read_buffer['$[]']($range(1, -1, false)); 19252 return ret; 19253 }; 19254 } while ($truthy((parts = self.$sysread_noraise(1))));; 19255 return nil; 19256 }); 19257 19258 $def(self, '$getbyte', function $$getbyte() { 19259 var $a, self = this; 19260 19261 return ($a = self.$getc(), ($a === nil || $a == null) ? nil : $a.$ord()) 19262 }); 19263 19264 $def(self, '$readbyte', function $$readbyte() { 19265 var self = this; 19266 19267 return self.$readchar().$ord() 19268 }); 19269 19270 $def(self, '$readchar', function $$readchar() { 19271 var self = this, $ret_or_1 = nil; 19272 19273 if ($truthy(($ret_or_1 = self.$getc()))) { 19274 return $ret_or_1 19275 } else { 19276 return $Kernel.$raise($$$('EOFError'), "end of file reached") 19277 } 19278 }); 19279 19280 $def(self, '$readline', function $$readline($a) { 19281 var $post_args, args, self = this, $ret_or_1 = nil; 19282 19283 19284 $post_args = $slice(arguments); 19285 args = $post_args; 19286 if ($truthy(($ret_or_1 = $send(self, 'gets', $to_a(args))))) { 19287 return $ret_or_1 19288 } else { 19289 return $Kernel.$raise($$$('EOFError'), "end of file reached") 19290 }; 19291 }, -1); 19292 19293 $def(self, '$gets', function $$gets(sep, limit, opts) { 19294 var $a, $b, self = this, orig_sep = nil, $ret_or_1 = nil, seplen = nil, data = nil, ret = nil, orig_buffer = nil; 19295 if ($gvars["/"] == null) $gvars["/"] = nil; 19296 19297 19298 if (sep == null) sep = false; 19299 if (limit == null) limit = nil; 19300 if (opts == null) opts = $hash2([], {}); 19301 if (($truthy(sep.$$is_number) && ($not(limit)))) { 19302 $a = [false, sep, limit], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]), $a 19303 }; 19304 if ((($truthy(sep.$$is_hash) && ($not(limit))) && ($eqeq(opts, $hash2([], {}))))) { 19305 $a = [false, nil, sep], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]), $a 19306 } else if (($truthy(limit.$$is_hash) && ($eqeq(opts, $hash2([], {}))))) { 19307 $a = [sep, nil, limit], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]), $a 19308 }; 19309 orig_sep = sep; 19310 if ($eqeq(sep, false)) { 19311 sep = $gvars["/"] 19312 }; 19313 if ($eqeq(sep, "")) { 19314 sep = /\r?\n\r?\n/ 19315 }; 19316 sep = ($truthy(($ret_or_1 = sep)) ? ($ret_or_1) : ("")); 19317 if (!$eqeq(orig_sep, "")) { 19318 sep = sep.$to_str() 19319 }; 19320 seplen = ($eqeq(orig_sep, "") ? (2) : (sep.$length())); 19321 if ($eqeq(sep, " ")) { 19322 sep = / / 19323 }; 19324 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 19325 data = ""; 19326 ret = nil; 19327 do { 19328 19329 self.read_buffer = $rb_plus(self.read_buffer, data); 19330 if (($neqeq(sep, "") && ($truthy(($truthy(sep.$$is_regexp) ? (self.read_buffer['$match?'](sep)) : (self.read_buffer['$include?'](sep))))))) { 19331 19332 orig_buffer = self.read_buffer; 19333 $b = self.read_buffer.$split(sep, 2), $a = $to_ary($b), (ret = ($a[0] == null ? nil : $a[0])), (self.read_buffer = ($a[1] == null ? nil : $a[1])), $b; 19334 if ($neqeq(ret, orig_buffer)) { 19335 ret = $rb_plus(ret, orig_buffer['$[]'](ret.$length(), seplen)) 19336 }; 19337 break; 19338 }; 19339 } while ($truthy((data = self.$sysread_noraise(($eqeq(sep, "") ? (65536) : (1))))));; 19340 if (!$truthy(ret)) { 19341 19342 $a = [($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")), ""], (ret = $a[0]), (self.read_buffer = $a[1]), $a; 19343 if ($eqeq(ret, "")) { 19344 ret = nil 19345 }; 19346 }; 19347 if ($truthy(ret)) { 19348 19349 if ($truthy(limit)) { 19350 19351 ret = ret['$[]'](Opal.Range.$new(0,limit, true)); 19352 self.read_buffer = $rb_plus(ret['$[]'](Opal.Range.$new(limit, -1, false)), self.read_buffer); 19353 }; 19354 if ($truthy(opts['$[]']("chomp"))) { 19355 ret = ret.$sub(/\r?\n$/, "") 19356 }; 19357 if ($eqeq(orig_sep, "")) { 19358 ret = ret.$sub(/^[\r\n]+/, "") 19359 }; 19360 }; 19361 if ($eqeq(orig_sep, false)) { 19362 $gvars._ = ret 19363 }; 19364 return ret; 19365 }, -1); 19366 19367 $def(self, '$sysread', function $$sysread(integer) { 19368 var self = this, $ret_or_1 = nil; 19369 19370 if ($truthy(($ret_or_1 = self.read_proc(integer)))) { 19371 return $ret_or_1 19372 } else { 19373 19374 self.eof = true; 19375 return $Kernel.$raise($$$('EOFError'), "end of file reached"); 19376 } 19377 }); 19378 19379 $def(self, '$sysread_noraise', function $$sysread_noraise(integer) { 19380 var self = this; 19381 19382 try { 19383 return self.$sysread(integer) 19384 } catch ($err) { 19385 if (Opal.rescue($err, [$$$('EOFError')])) { 19386 try { 19387 return nil 19388 } finally { Opal.pop_exception(); } 19389 } else { throw $err; } 19390 } 19391 }); 19392 19393 $def(self, '$readpartial', function $$readpartial(integer) { 19394 var $a, self = this, $ret_or_1 = nil, part = nil, ret = nil; 19395 19396 19397 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 19398 part = self.$sysread(integer); 19399 $a = [$rb_plus(self.read_buffer, ($truthy(($ret_or_1 = part)) ? ($ret_or_1) : (""))), ""], (ret = $a[0]), (self.read_buffer = $a[1]), $a; 19400 if ($eqeq(ret, "")) { 19401 ret = nil 19402 }; 19403 return ret; 19404 }); 19405 19406 $def(self, '$read', function $$read(integer) { 19407 var $a, self = this, $ret_or_1 = nil, parts = nil, ret = nil; 19408 19409 19410 if (integer == null) integer = nil; 19411 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 19412 parts = ""; 19413 ret = nil; 19414 do { 19415 19416 self.read_buffer = $rb_plus(self.read_buffer, parts); 19417 if (($truthy(integer) && ($truthy($rb_gt(self.read_buffer.$length(), integer))))) { 19418 19419 $a = [self.read_buffer['$[]'](Opal.Range.$new(0,integer, true)), self.read_buffer['$[]'](Opal.Range.$new(integer, -1, false))], (ret = $a[0]), (self.read_buffer = $a[1]), $a; 19420 return ret; 19421 }; 19422 } while ($truthy((parts = self.$sysread_noraise(($truthy(($ret_or_1 = integer)) ? ($ret_or_1) : (65536))))));; 19423 $a = [self.read_buffer, ""], (ret = $a[0]), (self.read_buffer = $a[1]), $a; 19424 return ret; 19425 }, -1); 19426 19427 $def(self, '$readlines', function $$readlines(separator) { 19428 var self = this; 19429 if ($gvars["/"] == null) $gvars["/"] = nil; 19430 19431 19432 if (separator == null) separator = $gvars["/"]; 19433 return self.$each_line(separator).$to_a(); 19434 }, -1); 19435 19436 $def(self, '$each', function $$each($a, $b) { 19437 var block = $$each.$$p || nil, $post_args, sep, args, self = this, s = nil; 19438 if ($gvars["/"] == null) $gvars["/"] = nil; 19439 19440 $$each.$$p = null; 19441 19442 ; 19443 $post_args = $slice(arguments); 19444 19445 if ($post_args.length > 0) sep = $post_args.shift();if (sep == null) sep = $gvars["/"]; 19446 args = $post_args; 19447 if (!(block !== nil)) { 19448 return $send(self, 'enum_for', ["each", sep].concat($to_a(args))) 19449 }; 19450 while ($truthy((s = $send(self, 'gets', [sep].concat($to_a(args)))))) { 19451 Opal.yield1(block, s) 19452 }; 19453 return self; 19454 }, -1); 19455 19456 $def(self, '$each_byte', function $$each_byte() { 19457 var block = $$each_byte.$$p || nil, self = this, s = nil; 19458 19459 $$each_byte.$$p = null; 19460 19461 ; 19462 if (!(block !== nil)) { 19463 return self.$enum_for("each_byte") 19464 }; 19465 while ($truthy((s = self.$getbyte()))) { 19466 Opal.yield1(block, s) 19467 }; 19468 return self; 19469 }); 19470 19471 $def(self, '$each_char', function $$each_char() { 19472 var block = $$each_char.$$p || nil, self = this, s = nil; 19473 19474 $$each_char.$$p = null; 19475 19476 ; 19477 if (!(block !== nil)) { 19478 return self.$enum_for("each_char") 19479 }; 19480 while ($truthy((s = self.$getc()))) { 19481 Opal.yield1(block, s) 19482 }; 19483 return self; 19484 }); 19485 19486 $def(self, '$close', $assign_ivar_val("closed", "both")); 19487 19488 $def(self, '$close_read', function $$close_read() { 19489 var self = this; 19490 19491 if ($eqeq(self.closed, "write")) { 19492 return (self.closed = "both") 19493 } else { 19494 return (self.closed = "read") 19495 } 19496 }); 19497 19498 $def(self, '$close_write', function $$close_write() { 19499 var self = this; 19500 19501 if ($eqeq(self.closed, "read")) { 19502 return (self.closed = "both") 19503 } else { 19504 return (self.closed = "write") 19505 } 19506 }); 19507 19508 $def(self, '$closed?', function $IO_closed$ques$3() { 19509 var self = this; 19510 19511 return self.closed['$==']("both") 19512 }); 19513 19514 $def(self, '$closed_read?', function $IO_closed_read$ques$4() { 19515 var self = this, $ret_or_1 = nil; 19516 19517 if ($truthy(($ret_or_1 = self.closed['$==']("read")))) { 19518 return $ret_or_1 19519 } else { 19520 return self.closed['$==']("both") 19521 } 19522 }); 19523 19524 $def(self, '$closed_write?', function $IO_closed_write$ques$5() { 19525 var self = this, $ret_or_1 = nil; 19526 19527 if ($truthy(($ret_or_1 = self.closed['$==']("write")))) { 19528 return $ret_or_1 19529 } else { 19530 return self.closed['$==']("both") 19531 } 19532 }); 19533 19534 $def(self, '$check_writable', function $$check_writable() { 19535 var self = this; 19536 19537 if ($truthy(self['$closed_write?']())) { 19538 return $Kernel.$raise($$$('IOError'), "not opened for writing") 19539 } else { 19540 return nil 19541 } 19542 }); 19543 19544 $def(self, '$check_readable', function $$check_readable() { 19545 var self = this; 19546 19547 if ($truthy(self['$closed_read?']())) { 19548 return $Kernel.$raise($$$('IOError'), "not opened for reading") 19549 } else { 19550 return nil 19551 } 19552 }); 19553 $alias(self, "each_line", "each"); 19554 return $alias(self, "eof?", "eof"); 19555 })('::', null); 19556 $const_set('::', 'STDIN', ($gvars.stdin = $$$('IO').$new(0, "r"))); 19557 $const_set('::', 'STDOUT', ($gvars.stdout = $$$('IO').$new(1, "w"))); 19558 $const_set('::', 'STDERR', ($gvars.stderr = $$$('IO').$new(2, "w"))); 19559 var console = Opal.global.console; 19560 $$$('STDOUT')['$write_proc='](typeof(process) === 'object' && typeof(process.stdout) === 'object' ? function(s){process.stdout.write(s)} : function(s){console.log(s)}); 19561 $$$('STDERR')['$write_proc='](typeof(process) === 'object' && typeof(process.stderr) === 'object' ? function(s){process.stderr.write(s)} : function(s){console.warn(s)}); 19562 return ($a = [function(s) { var p = prompt(); if (p !== null) return p + "\n"; return nil; }], $send($$$('STDIN'), 'read_proc=', $a), $a[$a.length - 1]); 19563}; 19564 19565Opal.modules["opal/regexp_anchors"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19566 var $module = Opal.module, $const_set = Opal.const_set, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 19567 19568 Opal.add_stubs('new'); 19569 return (function($base, $parent_nesting) { 19570 var self = $module($base, 'Opal'); 19571 19572 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 19573 19574 19575 $const_set(self, 'REGEXP_START', "^"); 19576 $const_set(self, 'REGEXP_END', "$"); 19577 $const_set(self, 'FORBIDDEN_STARTING_IDENTIFIER_CHARS', "\\u0001-\\u002F\\u003A-\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 19578 $const_set(self, 'FORBIDDEN_ENDING_IDENTIFIER_CHARS', "\\u0001-\\u0020\\u0022-\\u002F\\u003A-\\u003E\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 19579 $const_set(self, 'INLINE_IDENTIFIER_REGEXP', $$('Regexp').$new("[^" + ($$$(self, 'FORBIDDEN_STARTING_IDENTIFIER_CHARS')) + "]*[^" + ($$$(self, 'FORBIDDEN_ENDING_IDENTIFIER_CHARS')) + "]")); 19580 $const_set(self, 'FORBIDDEN_CONST_NAME_CHARS', "\\u0001-\\u0020\\u0021-\\u002F\\u003B-\\u003F\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 19581 return $const_set(self, 'CONST_NAME_REGEXP', $$('Regexp').$new("" + ($$$(self, 'REGEXP_START')) + "(::)?[A-Z][^" + ($$$(self, 'FORBIDDEN_CONST_NAME_CHARS')) + "]*" + ($$$(self, 'REGEXP_END')))); 19582 })($nesting[0], $nesting) 19583}; 19584 19585Opal.modules["opal/mini"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19586 var $Object = Opal.Object, nil = Opal.nil; 19587 19588 Opal.add_stubs('require'); 19589 19590 $Object.$require("opal/base"); 19591 $Object.$require("corelib/nil"); 19592 $Object.$require("corelib/boolean"); 19593 $Object.$require("corelib/string"); 19594 $Object.$require("corelib/comparable"); 19595 $Object.$require("corelib/enumerable"); 19596 $Object.$require("corelib/enumerator"); 19597 $Object.$require("corelib/array"); 19598 $Object.$require("corelib/hash"); 19599 $Object.$require("corelib/number"); 19600 $Object.$require("corelib/range"); 19601 $Object.$require("corelib/proc"); 19602 $Object.$require("corelib/method"); 19603 $Object.$require("corelib/regexp"); 19604 $Object.$require("corelib/variables"); 19605 $Object.$require("corelib/io"); 19606 return $Object.$require("opal/regexp_anchors"); 19607}; 19608 19609Opal.modules["corelib/kernel/format"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19610 var $coerce_to = Opal.coerce_to, $module = Opal.module, $slice = Opal.slice, $truthy = Opal.truthy, $eqeq = Opal.eqeq, $Opal = Opal.Opal, $Kernel = Opal.Kernel, $gvars = Opal.gvars, $def = Opal.def, $alias = Opal.alias, nil = Opal.nil, $$$ = Opal.$$$; 19611 19612 Opal.add_stubs('respond_to?,[],==,length,coerce_to?,nil?,to_a,raise,to_int,fetch,Integer,Float,to_ary,to_str,inspect,to_s,format'); 19613 return (function($base) { 19614 var self = $module($base, 'Kernel'); 19615 19616 19617 19618 19619 $def(self, '$format', function $$format(format_string, $a) { 19620 var $post_args, args, ary = nil; 19621 if ($gvars.DEBUG == null) $gvars.DEBUG = nil; 19622 19623 19624 $post_args = $slice(arguments, 1); 19625 args = $post_args; 19626 if (($eqeq(args.$length(), 1) && ($truthy(args['$[]'](0)['$respond_to?']("to_ary"))))) { 19627 19628 ary = $Opal['$coerce_to?'](args['$[]'](0), $$$('Array'), "to_ary"); 19629 if (!$truthy(ary['$nil?']())) { 19630 args = ary.$to_a() 19631 }; 19632 }; 19633 19634 var result = '', 19635 //used for slicing: 19636 begin_slice = 0, 19637 end_slice, 19638 //used for iterating over the format string: 19639 i, 19640 len = format_string.length, 19641 //used for processing field values: 19642 arg, 19643 str, 19644 //used for processing %g and %G fields: 19645 exponent, 19646 //used for keeping track of width and precision: 19647 width, 19648 precision, 19649 //used for holding temporary values: 19650 tmp_num, 19651 //used for processing %{} and %<> fileds: 19652 hash_parameter_key, 19653 closing_brace_char, 19654 //used for processing %b, %B, %o, %x, and %X fields: 19655 base_number, 19656 base_prefix, 19657 base_neg_zero_regex, 19658 base_neg_zero_digit, 19659 //used for processing arguments: 19660 next_arg, 19661 seq_arg_num = 1, 19662 pos_arg_num = 0, 19663 //used for keeping track of flags: 19664 flags, 19665 FNONE = 0, 19666 FSHARP = 1, 19667 FMINUS = 2, 19668 FPLUS = 4, 19669 FZERO = 8, 19670 FSPACE = 16, 19671 FWIDTH = 32, 19672 FPREC = 64, 19673 FPREC0 = 128; 19674 19675 function CHECK_FOR_FLAGS() { 19676 if (flags&FWIDTH) { $Kernel.$raise($$$('ArgumentError'), "flag after width") } 19677 if (flags&FPREC0) { $Kernel.$raise($$$('ArgumentError'), "flag after precision") } 19678 } 19679 19680 function CHECK_FOR_WIDTH() { 19681 if (flags&FWIDTH) { $Kernel.$raise($$$('ArgumentError'), "width given twice") } 19682 if (flags&FPREC0) { $Kernel.$raise($$$('ArgumentError'), "width after precision") } 19683 } 19684 19685 function GET_NTH_ARG(num) { 19686 if (num >= args.length) { $Kernel.$raise($$$('ArgumentError'), "too few arguments") } 19687 return args[num]; 19688 } 19689 19690 function GET_NEXT_ARG() { 19691 switch (pos_arg_num) { 19692 case -1: $Kernel.$raise($$$('ArgumentError'), "unnumbered(" + (seq_arg_num) + ") mixed with numbered") // raise 19693 case -2: $Kernel.$raise($$$('ArgumentError'), "unnumbered(" + (seq_arg_num) + ") mixed with named") // raise 19694 } 19695 pos_arg_num = seq_arg_num++; 19696 return GET_NTH_ARG(pos_arg_num - 1); 19697 } 19698 19699 function GET_POS_ARG(num) { 19700 if (pos_arg_num > 0) { 19701 $Kernel.$raise($$$('ArgumentError'), "numbered(" + (num) + ") after unnumbered(" + (pos_arg_num) + ")") 19702 } 19703 if (pos_arg_num === -2) { 19704 $Kernel.$raise($$$('ArgumentError'), "numbered(" + (num) + ") after named") 19705 } 19706 if (num < 1) { 19707 $Kernel.$raise($$$('ArgumentError'), "invalid index - " + (num) + "$") 19708 } 19709 pos_arg_num = -1; 19710 return GET_NTH_ARG(num - 1); 19711 } 19712 19713 function GET_ARG() { 19714 return (next_arg === undefined ? GET_NEXT_ARG() : next_arg); 19715 } 19716 19717 function READ_NUM(label) { 19718 var num, str = ''; 19719 for (;; i++) { 19720 if (i === len) { 19721 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %*[0-9]") 19722 } 19723 if (format_string.charCodeAt(i) < 48 || format_string.charCodeAt(i) > 57) { 19724 i--; 19725 num = parseInt(str, 10) || 0; 19726 if (num > 2147483647) { 19727 $Kernel.$raise($$$('ArgumentError'), "" + (label) + " too big") 19728 } 19729 return num; 19730 } 19731 str += format_string.charAt(i); 19732 } 19733 } 19734 19735 function READ_NUM_AFTER_ASTER(label) { 19736 var arg, num = READ_NUM(label); 19737 if (format_string.charAt(i + 1) === '$') { 19738 i++; 19739 arg = GET_POS_ARG(num); 19740 } else { 19741 arg = GET_NEXT_ARG(); 19742 } 19743 return (arg).$to_int(); 19744 } 19745 19746 for (i = format_string.indexOf('%'); i !== -1; i = format_string.indexOf('%', i)) { 19747 str = undefined; 19748 19749 flags = FNONE; 19750 width = -1; 19751 precision = -1; 19752 next_arg = undefined; 19753 19754 end_slice = i; 19755 19756 i++; 19757 19758 switch (format_string.charAt(i)) { 19759 case '%': 19760 begin_slice = i; 19761 // no-break 19762 case '': 19763 case '\n': 19764 case '\0': 19765 i++; 19766 continue; 19767 } 19768 19769 format_sequence: for (; i < len; i++) { 19770 switch (format_string.charAt(i)) { 19771 19772 case ' ': 19773 CHECK_FOR_FLAGS(); 19774 flags |= FSPACE; 19775 continue format_sequence; 19776 19777 case '#': 19778 CHECK_FOR_FLAGS(); 19779 flags |= FSHARP; 19780 continue format_sequence; 19781 19782 case '+': 19783 CHECK_FOR_FLAGS(); 19784 flags |= FPLUS; 19785 continue format_sequence; 19786 19787 case '-': 19788 CHECK_FOR_FLAGS(); 19789 flags |= FMINUS; 19790 continue format_sequence; 19791 19792 case '0': 19793 CHECK_FOR_FLAGS(); 19794 flags |= FZERO; 19795 continue format_sequence; 19796 19797 case '1': 19798 case '2': 19799 case '3': 19800 case '4': 19801 case '5': 19802 case '6': 19803 case '7': 19804 case '8': 19805 case '9': 19806 tmp_num = READ_NUM('width'); 19807 if (format_string.charAt(i + 1) === '$') { 19808 if (i + 2 === len) { 19809 str = '%'; 19810 i++; 19811 break format_sequence; 19812 } 19813 if (next_arg !== undefined) { 19814 $Kernel.$raise($$$('ArgumentError'), "value given twice - %" + (tmp_num) + "$") 19815 } 19816 next_arg = GET_POS_ARG(tmp_num); 19817 i++; 19818 } else { 19819 CHECK_FOR_WIDTH(); 19820 flags |= FWIDTH; 19821 width = tmp_num; 19822 } 19823 continue format_sequence; 19824 19825 case '<': 19826 case '\{': 19827 closing_brace_char = (format_string.charAt(i) === '<' ? '>' : '\}'); 19828 hash_parameter_key = ''; 19829 19830 i++; 19831 19832 for (;; i++) { 19833 if (i === len) { 19834 $Kernel.$raise($$$('ArgumentError'), "malformed name - unmatched parenthesis") 19835 } 19836 if (format_string.charAt(i) === closing_brace_char) { 19837 19838 if (pos_arg_num > 0) { 19839 $Kernel.$raise($$$('ArgumentError'), "named " + (hash_parameter_key) + " after unnumbered(" + (pos_arg_num) + ")") 19840 } 19841 if (pos_arg_num === -1) { 19842 $Kernel.$raise($$$('ArgumentError'), "named " + (hash_parameter_key) + " after numbered") 19843 } 19844 pos_arg_num = -2; 19845 19846 if (args[0] === undefined || !args[0].$$is_hash) { 19847 $Kernel.$raise($$$('ArgumentError'), "one hash required") 19848 } 19849 19850 next_arg = (args[0]).$fetch(hash_parameter_key); 19851 19852 if (closing_brace_char === '>') { 19853 continue format_sequence; 19854 } else { 19855 str = next_arg.toString(); 19856 if (precision !== -1) { str = str.slice(0, precision); } 19857 if (flags&FMINUS) { 19858 while (str.length < width) { str = str + ' '; } 19859 } else { 19860 while (str.length < width) { str = ' ' + str; } 19861 } 19862 break format_sequence; 19863 } 19864 } 19865 hash_parameter_key += format_string.charAt(i); 19866 } 19867 // raise 19868 19869 case '*': 19870 i++; 19871 CHECK_FOR_WIDTH(); 19872 flags |= FWIDTH; 19873 width = READ_NUM_AFTER_ASTER('width'); 19874 if (width < 0) { 19875 flags |= FMINUS; 19876 width = -width; 19877 } 19878 continue format_sequence; 19879 19880 case '.': 19881 if (flags&FPREC0) { 19882 $Kernel.$raise($$$('ArgumentError'), "precision given twice") 19883 } 19884 flags |= FPREC|FPREC0; 19885 precision = 0; 19886 i++; 19887 if (format_string.charAt(i) === '*') { 19888 i++; 19889 precision = READ_NUM_AFTER_ASTER('precision'); 19890 if (precision < 0) { 19891 flags &= ~FPREC; 19892 } 19893 continue format_sequence; 19894 } 19895 precision = READ_NUM('precision'); 19896 continue format_sequence; 19897 19898 case 'd': 19899 case 'i': 19900 case 'u': 19901 arg = $Kernel.$Integer(GET_ARG()); 19902 if (arg >= 0) { 19903 str = arg.toString(); 19904 while (str.length < precision) { str = '0' + str; } 19905 if (flags&FMINUS) { 19906 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19907 while (str.length < width) { str = str + ' '; } 19908 } else { 19909 if (flags&FZERO && precision === -1) { 19910 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; } 19911 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19912 } else { 19913 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19914 while (str.length < width) { str = ' ' + str; } 19915 } 19916 } 19917 } else { 19918 str = (-arg).toString(); 19919 while (str.length < precision) { str = '0' + str; } 19920 if (flags&FMINUS) { 19921 str = '-' + str; 19922 while (str.length < width) { str = str + ' '; } 19923 } else { 19924 if (flags&FZERO && precision === -1) { 19925 while (str.length < width - 1) { str = '0' + str; } 19926 str = '-' + str; 19927 } else { 19928 str = '-' + str; 19929 while (str.length < width) { str = ' ' + str; } 19930 } 19931 } 19932 } 19933 break format_sequence; 19934 19935 case 'b': 19936 case 'B': 19937 case 'o': 19938 case 'x': 19939 case 'X': 19940 switch (format_string.charAt(i)) { 19941 case 'b': 19942 case 'B': 19943 base_number = 2; 19944 base_prefix = '0b'; 19945 base_neg_zero_regex = /^1+/; 19946 base_neg_zero_digit = '1'; 19947 break; 19948 case 'o': 19949 base_number = 8; 19950 base_prefix = '0'; 19951 base_neg_zero_regex = /^3?7+/; 19952 base_neg_zero_digit = '7'; 19953 break; 19954 case 'x': 19955 case 'X': 19956 base_number = 16; 19957 base_prefix = '0x'; 19958 base_neg_zero_regex = /^f+/; 19959 base_neg_zero_digit = 'f'; 19960 break; 19961 } 19962 arg = $Kernel.$Integer(GET_ARG()); 19963 if (arg >= 0) { 19964 str = arg.toString(base_number); 19965 while (str.length < precision) { str = '0' + str; } 19966 if (flags&FMINUS) { 19967 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19968 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 19969 while (str.length < width) { str = str + ' '; } 19970 } else { 19971 if (flags&FZERO && precision === -1) { 19972 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0) - ((flags&FSHARP && arg !== 0) ? base_prefix.length : 0)) { str = '0' + str; } 19973 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 19974 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19975 } else { 19976 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 19977 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 19978 while (str.length < width) { str = ' ' + str; } 19979 } 19980 } 19981 } else { 19982 if (flags&FPLUS || flags&FSPACE) { 19983 str = (-arg).toString(base_number); 19984 while (str.length < precision) { str = '0' + str; } 19985 if (flags&FMINUS) { 19986 if (flags&FSHARP) { str = base_prefix + str; } 19987 str = '-' + str; 19988 while (str.length < width) { str = str + ' '; } 19989 } else { 19990 if (flags&FZERO && precision === -1) { 19991 while (str.length < width - 1 - (flags&FSHARP ? 2 : 0)) { str = '0' + str; } 19992 if (flags&FSHARP) { str = base_prefix + str; } 19993 str = '-' + str; 19994 } else { 19995 if (flags&FSHARP) { str = base_prefix + str; } 19996 str = '-' + str; 19997 while (str.length < width) { str = ' ' + str; } 19998 } 19999 } 20000 } else { 20001 str = (arg >>> 0).toString(base_number).replace(base_neg_zero_regex, base_neg_zero_digit); 20002 while (str.length < precision - 2) { str = base_neg_zero_digit + str; } 20003 if (flags&FMINUS) { 20004 str = '..' + str; 20005 if (flags&FSHARP) { str = base_prefix + str; } 20006 while (str.length < width) { str = str + ' '; } 20007 } else { 20008 if (flags&FZERO && precision === -1) { 20009 while (str.length < width - 2 - (flags&FSHARP ? base_prefix.length : 0)) { str = base_neg_zero_digit + str; } 20010 str = '..' + str; 20011 if (flags&FSHARP) { str = base_prefix + str; } 20012 } else { 20013 str = '..' + str; 20014 if (flags&FSHARP) { str = base_prefix + str; } 20015 while (str.length < width) { str = ' ' + str; } 20016 } 20017 } 20018 } 20019 } 20020 if (format_string.charAt(i) === format_string.charAt(i).toUpperCase()) { 20021 str = str.toUpperCase(); 20022 } 20023 break format_sequence; 20024 20025 case 'f': 20026 case 'e': 20027 case 'E': 20028 case 'g': 20029 case 'G': 20030 arg = $Kernel.$Float(GET_ARG()); 20031 if (arg >= 0 || isNaN(arg)) { 20032 if (arg === Infinity) { 20033 str = 'Inf'; 20034 } else { 20035 switch (format_string.charAt(i)) { 20036 case 'f': 20037 str = arg.toFixed(precision === -1 ? 6 : precision); 20038 break; 20039 case 'e': 20040 case 'E': 20041 str = arg.toExponential(precision === -1 ? 6 : precision); 20042 break; 20043 case 'g': 20044 case 'G': 20045 str = arg.toExponential(); 20046 exponent = parseInt(str.split('e')[1], 10); 20047 if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) { 20048 str = arg.toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision); 20049 } 20050 break; 20051 } 20052 } 20053 if (flags&FMINUS) { 20054 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 20055 while (str.length < width) { str = str + ' '; } 20056 } else { 20057 if (flags&FZERO && arg !== Infinity && !isNaN(arg)) { 20058 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; } 20059 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 20060 } else { 20061 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 20062 while (str.length < width) { str = ' ' + str; } 20063 } 20064 } 20065 } else { 20066 if (arg === -Infinity) { 20067 str = 'Inf'; 20068 } else { 20069 switch (format_string.charAt(i)) { 20070 case 'f': 20071 str = (-arg).toFixed(precision === -1 ? 6 : precision); 20072 break; 20073 case 'e': 20074 case 'E': 20075 str = (-arg).toExponential(precision === -1 ? 6 : precision); 20076 break; 20077 case 'g': 20078 case 'G': 20079 str = (-arg).toExponential(); 20080 exponent = parseInt(str.split('e')[1], 10); 20081 if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) { 20082 str = (-arg).toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision); 20083 } 20084 break; 20085 } 20086 } 20087 if (flags&FMINUS) { 20088 str = '-' + str; 20089 while (str.length < width) { str = str + ' '; } 20090 } else { 20091 if (flags&FZERO && arg !== -Infinity) { 20092 while (str.length < width - 1) { str = '0' + str; } 20093 str = '-' + str; 20094 } else { 20095 str = '-' + str; 20096 while (str.length < width) { str = ' ' + str; } 20097 } 20098 } 20099 } 20100 if (format_string.charAt(i) === format_string.charAt(i).toUpperCase() && arg !== Infinity && arg !== -Infinity && !isNaN(arg)) { 20101 str = str.toUpperCase(); 20102 } 20103 str = str.replace(/([eE][-+]?)([0-9])$/, '$10$2'); 20104 break format_sequence; 20105 20106 case 'a': 20107 case 'A': 20108 // Not implemented because there are no specs for this field type. 20109 $Kernel.$raise($$$('NotImplementedError'), "`A` and `a` format field types are not implemented in Opal yet") 20110 // raise 20111 20112 case 'c': 20113 arg = GET_ARG(); 20114 if ((arg)['$respond_to?']("to_ary")) { arg = (arg).$to_ary()[0]; } 20115 if ((arg)['$respond_to?']("to_str")) { 20116 str = (arg).$to_str(); 20117 } else { 20118 str = String.fromCharCode($coerce_to(arg, $$$('Integer'), 'to_int')); 20119 } 20120 if (str.length !== 1) { 20121 $Kernel.$raise($$$('ArgumentError'), "%c requires a character") 20122 } 20123 if (flags&FMINUS) { 20124 while (str.length < width) { str = str + ' '; } 20125 } else { 20126 while (str.length < width) { str = ' ' + str; } 20127 } 20128 break format_sequence; 20129 20130 case 'p': 20131 str = (GET_ARG()).$inspect(); 20132 if (precision !== -1) { str = str.slice(0, precision); } 20133 if (flags&FMINUS) { 20134 while (str.length < width) { str = str + ' '; } 20135 } else { 20136 while (str.length < width) { str = ' ' + str; } 20137 } 20138 break format_sequence; 20139 20140 case 's': 20141 str = (GET_ARG()).$to_s(); 20142 if (precision !== -1) { str = str.slice(0, precision); } 20143 if (flags&FMINUS) { 20144 while (str.length < width) { str = str + ' '; } 20145 } else { 20146 while (str.length < width) { str = ' ' + str; } 20147 } 20148 break format_sequence; 20149 20150 default: 20151 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %" + (format_string.charAt(i))) 20152 } 20153 } 20154 20155 if (str === undefined) { 20156 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %") 20157 } 20158 20159 result += format_string.slice(begin_slice, end_slice) + str; 20160 begin_slice = i + 1; 20161 } 20162 20163 if ($gvars.DEBUG && pos_arg_num >= 0 && seq_arg_num < args.length) { 20164 $Kernel.$raise($$$('ArgumentError'), "too many arguments for format string") 20165 } 20166 20167 return result + format_string.slice(begin_slice); 20168 ; 20169 }, -2); 20170 return $alias(self, "sprintf", "format"); 20171 })('::') 20172}; 20173 20174Opal.modules["corelib/string/encoding"] = function(Opal) {/* Generated by Opal 1.7.3 */ 20175 var $klass = Opal.klass, $hash2 = Opal.hash2, $rb_plus = Opal.rb_plus, $truthy = Opal.truthy, $send = Opal.send, $defs = Opal.defs, $eqeq = Opal.eqeq, $def = Opal.def, $return_ivar = Opal.return_ivar, $return_val = Opal.return_val, $slice = Opal.slice, $Kernel = Opal.Kernel, $Opal = Opal.Opal, $rb_lt = Opal.rb_lt, $a, self = Opal.top, $nesting = [], $$ = Opal.$r($nesting), nil = Opal.nil, $$$ = Opal.$$$; 20176 20177 Opal.add_stubs('require,+,[],clone,initialize,new,instance_eval,to_proc,each,const_set,tr,==,default_external,attr_accessor,singleton_class,attr_reader,raise,register,length,bytes,force_encoding,dup,bytesize,enum_for,each_byte,to_a,each_char,each_codepoint,coerce_to!,find,<,default_external='); 20178 20179 self.$require("corelib/string"); 20180 (function($base, $super) { 20181 var self = $klass($base, $super, 'Encoding'); 20182 20183 var $proto = self.$$prototype; 20184 20185 $proto.name = $proto.dummy = nil; 20186 20187 $defs(self, '$register', function $$register(name, options) { 20188 var block = $$register.$$p || nil, self = this, names = nil, $ret_or_1 = nil, ascii = nil, dummy = nil, encoding = nil, register = nil; 20189 20190 $$register.$$p = null; 20191 20192 ; 20193 if (options == null) options = $hash2([], {}); 20194 names = $rb_plus([name], ($truthy(($ret_or_1 = options['$[]']("aliases"))) ? ($ret_or_1) : ([]))); 20195 ascii = ($truthy(($ret_or_1 = options['$[]']("ascii"))) && ($ret_or_1)); 20196 dummy = ($truthy(($ret_or_1 = options['$[]']("dummy"))) && ($ret_or_1)); 20197 if ($truthy(options['$[]']("inherits"))) { 20198 20199 encoding = options['$[]']("inherits").$clone(); 20200 encoding.$initialize(name, names, ascii, dummy); 20201 } else { 20202 encoding = self.$new(name, names, ascii, dummy) 20203 }; 20204 if ((block !== nil)) { 20205 $send(encoding, 'instance_eval', [], block.$to_proc()) 20206 }; 20207 register = Opal.encodings; 20208 return $send(names, 'each', [], function $$1(encoding_name){var self = $$1.$$s == null ? this : $$1.$$s; 20209 20210 20211 if (encoding_name == null) encoding_name = nil; 20212 self.$const_set(encoding_name.$tr("-", "_"), encoding); 20213 return register[encoding_name] = encoding;}, {$$s: self}); 20214 }, -2); 20215 $defs(self, '$find', function $$find(name) { 20216 var self = this; 20217 20218 20219 if ($eqeq(name, "default_external")) { 20220 return self.$default_external() 20221 }; 20222 return Opal.find_encoding(name);; 20223 }); 20224 self.$singleton_class().$attr_accessor("default_external"); 20225 self.$attr_reader("name", "names"); 20226 20227 $def(self, '$initialize', function $$initialize(name, names, ascii, dummy) { 20228 var self = this; 20229 20230 20231 self.name = name; 20232 self.names = names; 20233 self.ascii = ascii; 20234 return (self.dummy = dummy); 20235 }); 20236 20237 $def(self, '$ascii_compatible?', $return_ivar("ascii")); 20238 20239 $def(self, '$dummy?', $return_ivar("dummy")); 20240 20241 $def(self, '$binary?', $return_val(false)); 20242 20243 $def(self, '$to_s', $return_ivar("name")); 20244 20245 $def(self, '$inspect', function $$inspect() { 20246 var self = this; 20247 20248 return "#<Encoding:" + (self.name) + (($truthy(self.dummy) ? (" (dummy)") : nil)) + ">" 20249 }); 20250 20251 $def(self, '$charsize', function $$charsize(string) { 20252 20253 20254 var len = 0; 20255 for (var i = 0, length = string.length; i < length; i++) { 20256 var charcode = string.charCodeAt(i); 20257 if (!(charcode >= 0xD800 && charcode <= 0xDBFF)) { 20258 len++; 20259 } 20260 } 20261 return len; 20262 20263 }); 20264 20265 $def(self, '$each_char', function $$each_char(string) { 20266 var block = $$each_char.$$p || nil; 20267 20268 $$each_char.$$p = null; 20269 20270 ; 20271 20272 var low_surrogate = ""; 20273 for (var i = 0, length = string.length; i < length; i++) { 20274 var charcode = string.charCodeAt(i); 20275 var chr = string.charAt(i); 20276 if (charcode >= 0xDC00 && charcode <= 0xDFFF) { 20277 low_surrogate = chr; 20278 continue; 20279 } 20280 else if (charcode >= 0xD800 && charcode <= 0xDBFF) { 20281 chr = low_surrogate + chr; 20282 } 20283 if (string.encoding.name != "UTF-8") { 20284 chr = new String(chr); 20285 chr.encoding = string.encoding; 20286 } 20287 Opal.yield1(block, chr); 20288 } 20289 ; 20290 }); 20291 20292 $def(self, '$each_byte', function $$each_byte($a) { 20293 var $post_args, $fwd_rest; 20294 20295 20296 $post_args = $slice(arguments); 20297 $fwd_rest = $post_args; 20298 return $Kernel.$raise($$$('NotImplementedError')); 20299 }, -1); 20300 20301 $def(self, '$bytesize', function $$bytesize($a) { 20302 var $post_args, $fwd_rest; 20303 20304 20305 $post_args = $slice(arguments); 20306 $fwd_rest = $post_args; 20307 return $Kernel.$raise($$$('NotImplementedError')); 20308 }, -1); 20309 $klass('::', $$$('StandardError'), 'EncodingError'); 20310 return ($klass('::', $$$('EncodingError'), 'CompatibilityError'), nil); 20311 })('::', null); 20312 $send($$$('Encoding'), 'register', ["UTF-8", $hash2(["aliases", "ascii"], {"aliases": ["CP65001"], "ascii": true})], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 20313 20314 20315 20316 $def(self, '$each_byte', function $$each_byte(string) { 20317 var block = $$each_byte.$$p || nil; 20318 20319 $$each_byte.$$p = null; 20320 20321 ; 20322 20323 // Taken from: https://github.com/feross/buffer/blob/f52dffd9df0445b93c0c9065c2f8f0f46b2c729a/index.js#L1954-L2032 20324 var units = Infinity; 20325 var codePoint; 20326 var length = string.length; 20327 var leadSurrogate = null; 20328 20329 for (var i = 0; i < length; ++i) { 20330 codePoint = string.charCodeAt(i); 20331 20332 // is surrogate component 20333 if (codePoint > 0xD7FF && codePoint < 0xE000) { 20334 // last char was a lead 20335 if (!leadSurrogate) { 20336 // no lead yet 20337 if (codePoint > 0xDBFF) { 20338 // unexpected trail 20339 if ((units -= 3) > -1) { 20340 Opal.yield1(block, 0xEF); 20341 Opal.yield1(block, 0xBF); 20342 Opal.yield1(block, 0xBD); 20343 } 20344 continue; 20345 } else if (i + 1 === length) { 20346 // unpaired lead 20347 if ((units -= 3) > -1) { 20348 Opal.yield1(block, 0xEF); 20349 Opal.yield1(block, 0xBF); 20350 Opal.yield1(block, 0xBD); 20351 } 20352 continue; 20353 } 20354 20355 // valid lead 20356 leadSurrogate = codePoint; 20357 20358 continue; 20359 } 20360 20361 // 2 leads in a row 20362 if (codePoint < 0xDC00) { 20363 if ((units -= 3) > -1) { 20364 Opal.yield1(block, 0xEF); 20365 Opal.yield1(block, 0xBF); 20366 Opal.yield1(block, 0xBD); 20367 } 20368 leadSurrogate = codePoint; 20369 continue; 20370 } 20371 20372 // valid surrogate pair 20373 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000; 20374 } else if (leadSurrogate) { 20375 // valid bmp char, but last char was a lead 20376 if ((units -= 3) > -1) { 20377 Opal.yield1(block, 0xEF); 20378 Opal.yield1(block, 0xBF); 20379 Opal.yield1(block, 0xBD); 20380 } 20381 } 20382 20383 leadSurrogate = null; 20384 20385 // encode utf8 20386 if (codePoint < 0x80) { 20387 if ((units -= 1) < 0) break; 20388 Opal.yield1(block, codePoint); 20389 } else if (codePoint < 0x800) { 20390 if ((units -= 2) < 0) break; 20391 Opal.yield1(block, codePoint >> 0x6 | 0xC0); 20392 Opal.yield1(block, codePoint & 0x3F | 0x80); 20393 } else if (codePoint < 0x10000) { 20394 if ((units -= 3) < 0) break; 20395 Opal.yield1(block, codePoint >> 0xC | 0xE0); 20396 Opal.yield1(block, codePoint >> 0x6 & 0x3F | 0x80); 20397 Opal.yield1(block, codePoint & 0x3F | 0x80); 20398 } else if (codePoint < 0x110000) { 20399 if ((units -= 4) < 0) break; 20400 Opal.yield1(block, codePoint >> 0x12 | 0xF0); 20401 Opal.yield1(block, codePoint >> 0xC & 0x3F | 0x80); 20402 Opal.yield1(block, codePoint >> 0x6 & 0x3F | 0x80); 20403 Opal.yield1(block, codePoint & 0x3F | 0x80); 20404 } else { 20405 // Invalid code point 20406 } 20407 } 20408 ; 20409 }); 20410 return $def(self, '$bytesize', function $$bytesize(string) { 20411 20412 return string.$bytes().$length() 20413 });}, {$$s: self}); 20414 $send($$$('Encoding'), 'register', ["UTF-16LE"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 20415 20416 20417 20418 $def(self, '$each_byte', function $$each_byte(string) { 20419 var block = $$each_byte.$$p || nil; 20420 20421 $$each_byte.$$p = null; 20422 20423 ; 20424 20425 for (var i = 0, length = string.length; i < length; i++) { 20426 var code = string.charCodeAt(i); 20427 20428 Opal.yield1(block, code & 0xff); 20429 Opal.yield1(block, code >> 8); 20430 } 20431 ; 20432 }); 20433 return $def(self, '$bytesize', function $$bytesize(string) { 20434 20435 return string.length * 2; 20436 });}, {$$s: self}); 20437 $send($$$('Encoding'), 'register', ["UTF-16BE", $hash2(["inherits"], {"inherits": $$$($$$('Encoding'), 'UTF_16LE')})], function $$4(){var self = $$4.$$s == null ? this : $$4.$$s; 20438 20439 return $def(self, '$each_byte', function $$each_byte(string) { 20440 var block = $$each_byte.$$p || nil; 20441 20442 $$each_byte.$$p = null; 20443 20444 ; 20445 20446 for (var i = 0, length = string.length; i < length; i++) { 20447 var code = string.charCodeAt(i); 20448 20449 Opal.yield1(block, code >> 8); 20450 Opal.yield1(block, code & 0xff); 20451 } 20452 ; 20453 })}, {$$s: self}); 20454 $send($$$('Encoding'), 'register', ["UTF-32LE"], function $$5(){var self = $$5.$$s == null ? this : $$5.$$s; 20455 20456 20457 20458 $def(self, '$each_byte', function $$each_byte(string) { 20459 var block = $$each_byte.$$p || nil; 20460 20461 $$each_byte.$$p = null; 20462 20463 ; 20464 20465 for (var i = 0, length = string.length; i < length; i++) { 20466 var code = string.charCodeAt(i); 20467 20468 Opal.yield1(block, code & 0xff); 20469 Opal.yield1(block, code >> 8); 20470 Opal.yield1(block, 0); 20471 Opal.yield1(block, 0); 20472 } 20473 ; 20474 }); 20475 return $def(self, '$bytesize', function $$bytesize(string) { 20476 20477 return string.length * 4; 20478 });}, {$$s: self}); 20479 $send($$$('Encoding'), 'register', ["UTF-32BE", $hash2(["inherits"], {"inherits": $$$($$$('Encoding'), 'UTF_32LE')})], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s; 20480 20481 return $def(self, '$each_byte', function $$each_byte(string) { 20482 var block = $$each_byte.$$p || nil; 20483 20484 $$each_byte.$$p = null; 20485 20486 ; 20487 20488 for (var i = 0, length = string.length; i < length; i++) { 20489 var code = string.charCodeAt(i); 20490 20491 Opal.yield1(block, 0); 20492 Opal.yield1(block, 0); 20493 Opal.yield1(block, code >> 8); 20494 Opal.yield1(block, code & 0xff); 20495 } 20496 ; 20497 })}, {$$s: self}); 20498 $send($$$('Encoding'), 'register', ["ASCII-8BIT", $hash2(["aliases", "ascii"], {"aliases": ["BINARY"], "ascii": true})], function $$7(){var self = $$7.$$s == null ? this : $$7.$$s; 20499 20500 20501 20502 $def(self, '$each_char', function $$each_char(string) { 20503 var block = $$each_char.$$p || nil; 20504 20505 $$each_char.$$p = null; 20506 20507 ; 20508 20509 for (var i = 0, length = string.length; i < length; i++) { 20510 var chr = new String(string.charAt(i)); 20511 chr.encoding = string.encoding; 20512 Opal.yield1(block, chr); 20513 } 20514 ; 20515 }); 20516 20517 $def(self, '$charsize', function $$charsize(string) { 20518 20519 return string.length; 20520 }); 20521 20522 $def(self, '$each_byte', function $$each_byte(string) { 20523 var block = $$each_byte.$$p || nil; 20524 20525 $$each_byte.$$p = null; 20526 20527 ; 20528 20529 for (var i = 0, length = string.length; i < length; i++) { 20530 var code = string.charCodeAt(i); 20531 Opal.yield1(block, code & 0xff); 20532 } 20533 ; 20534 }); 20535 20536 $def(self, '$bytesize', function $$bytesize(string) { 20537 20538 return string.length; 20539 }); 20540 return $def(self, '$binary?', $return_val(true));}, {$$s: self}); 20541 $$$('Encoding').$register("ISO-8859-1", $hash2(["aliases", "ascii", "inherits"], {"aliases": ["ISO8859-1"], "ascii": true, "inherits": $$$($$$('Encoding'), 'ASCII_8BIT')})); 20542 $$$('Encoding').$register("US-ASCII", $hash2(["aliases", "ascii", "inherits"], {"aliases": ["ASCII"], "ascii": true, "inherits": $$$($$$('Encoding'), 'ASCII_8BIT')})); 20543 (function($base, $super) { 20544 var self = $klass($base, $super, 'String'); 20545 20546 var $proto = self.$$prototype; 20547 20548 $proto.internal_encoding = $proto.bytes = $proto.encoding = nil; 20549 20550 self.$attr_reader("encoding"); 20551 self.$attr_reader("internal_encoding"); 20552 Opal.prop(String.prototype, 'bytes', nil); 20553 Opal.prop(String.prototype, 'encoding', $$$($$$('Encoding'), 'UTF_8')); 20554 Opal.prop(String.prototype, 'internal_encoding', $$$($$$('Encoding'), 'UTF_8')); 20555 20556 $def(self, '$b', function $$b() { 20557 var self = this; 20558 20559 return self.$dup().$force_encoding("binary") 20560 }); 20561 20562 $def(self, '$bytesize', function $$bytesize() { 20563 var self = this; 20564 20565 return self.internal_encoding.$bytesize(self) 20566 }); 20567 20568 $def(self, '$each_byte', function $$each_byte() { 20569 var block = $$each_byte.$$p || nil, self = this; 20570 20571 $$each_byte.$$p = null; 20572 20573 ; 20574 if (!(block !== nil)) { 20575 return $send(self, 'enum_for', ["each_byte"], function $$8(){var self = $$8.$$s == null ? this : $$8.$$s; 20576 20577 return self.$bytesize()}, {$$s: self}) 20578 }; 20579 $send(self.internal_encoding, 'each_byte', [self], block.$to_proc()); 20580 return self; 20581 }); 20582 20583 $def(self, '$bytes', function $$bytes() { 20584 var self = this, $ret_or_1 = nil; 20585 20586 20587 20588 if (typeof self === 'string') { 20589 return (new String(self)).$each_byte().$to_a(); 20590 } 20591 ; 20592 self.bytes = ($truthy(($ret_or_1 = self.bytes)) ? ($ret_or_1) : (self.$each_byte().$to_a())); 20593 return self.bytes.$dup(); 20594 }); 20595 20596 $def(self, '$each_char', function $$each_char() { 20597 var block = $$each_char.$$p || nil, self = this; 20598 20599 $$each_char.$$p = null; 20600 20601 ; 20602 if (!(block !== nil)) { 20603 return $send(self, 'enum_for', ["each_char"], function $$9(){var self = $$9.$$s == null ? this : $$9.$$s; 20604 20605 return self.$length()}, {$$s: self}) 20606 }; 20607 $send(self.encoding, 'each_char', [self], block.$to_proc()); 20608 return self; 20609 }); 20610 20611 $def(self, '$chars', function $$chars() { 20612 var block = $$chars.$$p || nil, self = this; 20613 20614 $$chars.$$p = null; 20615 20616 ; 20617 if (!$truthy(block)) { 20618 return self.$each_char().$to_a() 20619 }; 20620 return $send(self, 'each_char', [], block.$to_proc()); 20621 }); 20622 20623 $def(self, '$each_codepoint', function $$each_codepoint() { 20624 var block = $$each_codepoint.$$p || nil, self = this; 20625 20626 $$each_codepoint.$$p = null; 20627 20628 ; 20629 if (!(block !== nil)) { 20630 return self.$enum_for("each_codepoint") 20631 }; 20632 20633 for (var i = 0, length = self.length; i < length; i++) { 20634 Opal.yield1(block, self.codePointAt(i)); 20635 } 20636 ; 20637 return self; 20638 }); 20639 20640 $def(self, '$codepoints', function $$codepoints() { 20641 var block = $$codepoints.$$p || nil, self = this; 20642 20643 $$codepoints.$$p = null; 20644 20645 ; 20646 if ((block !== nil)) { 20647 return $send(self, 'each_codepoint', [], block.$to_proc()) 20648 }; 20649 return self.$each_codepoint().$to_a(); 20650 }); 20651 20652 $def(self, '$encode', function $$encode(encoding) { 20653 var self = this; 20654 20655 return Opal.enc(self, encoding); 20656 }); 20657 20658 $def(self, '$force_encoding', function $$force_encoding(encoding) { 20659 var self = this; 20660 20661 20662 var str = self; 20663 20664 if (encoding === str.encoding) { return str; } 20665 20666 encoding = $Opal['$coerce_to!'](encoding, $$$('String'), "to_s"); 20667 encoding = $$$('Encoding').$find(encoding); 20668 20669 if (encoding === str.encoding) { return str; } 20670 20671 str = Opal.set_encoding(str, encoding); 20672 20673 return str; 20674 20675 }); 20676 20677 $def(self, '$getbyte', function $$getbyte(idx) { 20678 var self = this, string_bytes = nil; 20679 20680 20681 string_bytes = self.$bytes(); 20682 idx = $Opal['$coerce_to!'](idx, $$$('Integer'), "to_int"); 20683 if ($truthy($rb_lt(string_bytes.$length(), idx))) { 20684 return nil 20685 }; 20686 return string_bytes['$[]'](idx); 20687 }); 20688 20689 $def(self, '$initialize_copy', function $$initialize_copy(other) { 20690 20691 return "\n" + " self.encoding = other.encoding;\n" + " self.internal_encoding = other.internal_encoding;\n" + " " 20692 }); 20693 return $def(self, '$valid_encoding?', $return_val(true)); 20694 })('::', null); 20695 return ($a = [$$$($$('Encoding'), 'UTF_8')], $send($$$('Encoding'), 'default_external=', $a), $a[$a.length - 1]); 20696}; 20697 20698Opal.modules["corelib/math"] = function(Opal) {/* Generated by Opal 1.7.3 */ 20699 var $type_error = Opal.type_error, $module = Opal.module, $const_set = Opal.const_set, $Class = Opal.Class, $slice = Opal.slice, $Kernel = Opal.Kernel, $defs = Opal.defs, $truthy = Opal.truthy, $send = Opal.send, $def = Opal.def, $rb_minus = Opal.rb_minus, $eqeqeq = Opal.eqeqeq, $rb_divide = Opal.rb_divide, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 20700 20701 Opal.add_stubs('new,raise,Float,Integer,module_function,each,define_method,checked,float!,===,gamma,-,integer!,/,infinite?'); 20702 return (function($base, $parent_nesting) { 20703 var self = $module($base, 'Math'); 20704 20705 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 20706 20707 20708 $const_set(self, 'E', Math.E); 20709 $const_set(self, 'PI', Math.PI); 20710 $const_set(self, 'DomainError', $Class.$new($$$('StandardError'))); 20711 $defs(self, '$checked', function $$checked(method, $a) { 20712 var $post_args, args; 20713 20714 20715 $post_args = $slice(arguments, 1); 20716 args = $post_args; 20717 20718 if (isNaN(args[0]) || (args.length == 2 && isNaN(args[1]))) { 20719 return NaN; 20720 } 20721 20722 var result = Math[method].apply(null, args); 20723 20724 if (isNaN(result)) { 20725 $Kernel.$raise($$('DomainError'), "Numerical argument is out of domain - \"" + (method) + "\""); 20726 } 20727 20728 return result; 20729 ; 20730 }, -2); 20731 $defs(self, '$float!', function $Math_float$excl$1(value) { 20732 20733 try { 20734 return $Kernel.$Float(value) 20735 } catch ($err) { 20736 if (Opal.rescue($err, [$$$('ArgumentError')])) { 20737 try { 20738 return $Kernel.$raise($type_error(value, $$$('Float'))) 20739 } finally { Opal.pop_exception(); } 20740 } else { throw $err; } 20741 } 20742 }); 20743 $defs(self, '$integer!', function $Math_integer$excl$2(value) { 20744 20745 try { 20746 return $Kernel.$Integer(value) 20747 } catch ($err) { 20748 if (Opal.rescue($err, [$$$('ArgumentError')])) { 20749 try { 20750 return $Kernel.$raise($type_error(value, $$$('Integer'))) 20751 } finally { Opal.pop_exception(); } 20752 } else { throw $err; } 20753 } 20754 }); 20755 self.$module_function(); 20756 if (!$truthy((typeof(Math.erf) !== "undefined"))) { 20757 20758 Opal.prop(Math, 'erf', function(x) { 20759 var A1 = 0.254829592, 20760 A2 = -0.284496736, 20761 A3 = 1.421413741, 20762 A4 = -1.453152027, 20763 A5 = 1.061405429, 20764 P = 0.3275911; 20765 20766 var sign = 1; 20767 20768 if (x < 0) { 20769 sign = -1; 20770 } 20771 20772 x = Math.abs(x); 20773 20774 var t = 1.0 / (1.0 + P * x); 20775 var y = 1.0 - (((((A5 * t + A4) * t) + A3) * t + A2) * t + A1) * t * Math.exp(-x * x); 20776 20777 return sign * y; 20778 }); 20779 20780 }; 20781 if (!$truthy((typeof(Math.erfc) !== "undefined"))) { 20782 20783 Opal.prop(Math, 'erfc', function(x) { 20784 var z = Math.abs(x), 20785 t = 1.0 / (0.5 * z + 1.0); 20786 20787 var A1 = t * 0.17087277 + -0.82215223, 20788 A2 = t * A1 + 1.48851587, 20789 A3 = t * A2 + -1.13520398, 20790 A4 = t * A3 + 0.27886807, 20791 A5 = t * A4 + -0.18628806, 20792 A6 = t * A5 + 0.09678418, 20793 A7 = t * A6 + 0.37409196, 20794 A8 = t * A7 + 1.00002368, 20795 A9 = t * A8, 20796 A10 = -z * z - 1.26551223 + A9; 20797 20798 var a = t * Math.exp(A10); 20799 20800 if (x < 0.0) { 20801 return 2.0 - a; 20802 } 20803 else { 20804 return a; 20805 } 20806 }); 20807 20808 }; 20809 $send(["acos", "acosh", "asin", "asinh", "atan", "atanh", "cbrt", "cos", "cosh", "erf", "erfc", "exp", "sin", "sinh", "sqrt", "tanh"], 'each', [], function $Math$3(method){var self = $Math$3.$$s == null ? this : $Math$3.$$s; 20810 20811 20812 if (method == null) method = nil; 20813 return $send(self, 'define_method', [method], function $$4(x){ 20814 20815 if (x == null) x = nil; 20816 return $$$('Math').$checked(method, $$$('Math')['$float!'](x));});}, {$$s: self}); 20817 20818 $def(self, '$atan2', function $$atan2(y, x) { 20819 20820 return $$$('Math').$checked("atan2", $$$('Math')['$float!'](y), $$$('Math')['$float!'](x)) 20821 }); 20822 20823 $def(self, '$hypot', function $$hypot(x, y) { 20824 20825 return $$$('Math').$checked("hypot", $$$('Math')['$float!'](x), $$$('Math')['$float!'](y)) 20826 }); 20827 20828 $def(self, '$frexp', function $$frexp(x) { 20829 20830 20831 x = $$('Math')['$float!'](x); 20832 20833 if (isNaN(x)) { 20834 return [NaN, 0]; 20835 } 20836 20837 var ex = Math.floor(Math.log(Math.abs(x)) / Math.log(2)) + 1, 20838 frac = x / Math.pow(2, ex); 20839 20840 return [frac, ex]; 20841 ; 20842 }); 20843 20844 $def(self, '$gamma', function $$gamma(n) { 20845 20846 20847 n = $$('Math')['$float!'](n); 20848 20849 var i, t, x, value, result, twoN, threeN, fourN, fiveN; 20850 20851 var G = 4.7421875; 20852 20853 var P = [ 20854 0.99999999999999709182, 20855 57.156235665862923517, 20856 -59.597960355475491248, 20857 14.136097974741747174, 20858 -0.49191381609762019978, 20859 0.33994649984811888699e-4, 20860 0.46523628927048575665e-4, 20861 -0.98374475304879564677e-4, 20862 0.15808870322491248884e-3, 20863 -0.21026444172410488319e-3, 20864 0.21743961811521264320e-3, 20865 -0.16431810653676389022e-3, 20866 0.84418223983852743293e-4, 20867 -0.26190838401581408670e-4, 20868 0.36899182659531622704e-5 20869 ]; 20870 20871 20872 if (isNaN(n)) { 20873 return NaN; 20874 } 20875 20876 if (n === 0 && 1 / n < 0) { 20877 return -Infinity; 20878 } 20879 20880 if (n === -1 || n === -Infinity) { 20881 $Kernel.$raise($$('DomainError'), "Numerical argument is out of domain - \"gamma\""); 20882 } 20883 20884 if ($$('Integer')['$==='](n)) { 20885 if (n <= 0) { 20886 return isFinite(n) ? Infinity : NaN; 20887 } 20888 20889 if (n > 171) { 20890 return Infinity; 20891 } 20892 20893 value = n - 2; 20894 result = n - 1; 20895 20896 while (value > 1) { 20897 result *= value; 20898 value--; 20899 } 20900 20901 if (result == 0) { 20902 result = 1; 20903 } 20904 20905 return result; 20906 } 20907 20908 if (n < 0.5) { 20909 return Math.PI / (Math.sin(Math.PI * n) * $$$('Math').$gamma($rb_minus(1, n))); 20910 } 20911 20912 if (n >= 171.35) { 20913 return Infinity; 20914 } 20915 20916 if (n > 85.0) { 20917 twoN = n * n; 20918 threeN = twoN * n; 20919 fourN = threeN * n; 20920 fiveN = fourN * n; 20921 20922 return Math.sqrt(2 * Math.PI / n) * Math.pow((n / Math.E), n) * 20923 (1 + 1 / (12 * n) + 1 / (288 * twoN) - 139 / (51840 * threeN) - 20924 571 / (2488320 * fourN) + 163879 / (209018880 * fiveN) + 20925 5246819 / (75246796800 * fiveN * n)); 20926 } 20927 20928 n -= 1; 20929 x = P[0]; 20930 20931 for (i = 1; i < P.length; ++i) { 20932 x += P[i] / (n + i); 20933 } 20934 20935 t = n + G + 0.5; 20936 20937 return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * x; 20938 ; 20939 }); 20940 20941 $def(self, '$ldexp', function $$ldexp(mantissa, exponent) { 20942 20943 20944 mantissa = $$('Math')['$float!'](mantissa); 20945 exponent = $$('Math')['$integer!'](exponent); 20946 20947 if (isNaN(exponent)) { 20948 $Kernel.$raise($$$('RangeError'), "float NaN out of range of integer"); 20949 } 20950 20951 return mantissa * Math.pow(2, exponent); 20952 ; 20953 }); 20954 20955 $def(self, '$lgamma', function $$lgamma(n) { 20956 20957 20958 if (n == -1) { 20959 return [Infinity, 1]; 20960 } 20961 else { 20962 return [Math.log(Math.abs($$$('Math').$gamma(n))), $$$('Math').$gamma(n) < 0 ? -1 : 1]; 20963 } 20964 20965 }); 20966 20967 $def(self, '$log', function $$log(x, base) { 20968 20969 20970 ; 20971 if ($eqeqeq($$$('String'), x)) { 20972 $Kernel.$raise($type_error(x, $$$('Float'))) 20973 }; 20974 if ($truthy(base == null)) { 20975 return $$$('Math').$checked("log", $$$('Math')['$float!'](x)) 20976 } else { 20977 20978 if ($eqeqeq($$$('String'), base)) { 20979 $Kernel.$raise($type_error(base, $$$('Float'))) 20980 }; 20981 return $rb_divide($$$('Math').$checked("log", $$$('Math')['$float!'](x)), $$$('Math').$checked("log", $$$('Math')['$float!'](base))); 20982 }; 20983 }, -2); 20984 20985 $def(self, '$log10', function $$log10(x) { 20986 20987 20988 if ($eqeqeq($$$('String'), x)) { 20989 $Kernel.$raise($type_error(x, $$$('Float'))) 20990 }; 20991 return $$$('Math').$checked("log10", $$$('Math')['$float!'](x)); 20992 }); 20993 20994 $def(self, '$log2', function $$log2(x) { 20995 20996 20997 if ($eqeqeq($$$('String'), x)) { 20998 $Kernel.$raise($type_error(x, $$$('Float'))) 20999 }; 21000 return $$$('Math').$checked("log2", $$$('Math')['$float!'](x)); 21001 }); 21002 return $def(self, '$tan', function $$tan(x) { 21003 21004 21005 x = $$$('Math')['$float!'](x); 21006 if ($truthy(x['$infinite?']())) { 21007 return $$$($$$('Float'), 'NAN') 21008 }; 21009 return $$$('Math').$checked("tan", $$$('Math')['$float!'](x)); 21010 }); 21011 })('::', $nesting) 21012}; 21013 21014Opal.modules["corelib/complex/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21015 var $module = Opal.module, $truthy = Opal.truthy, $def = Opal.def, $klass = Opal.klass, $nesting = [], nil = Opal.nil; 21016 21017 Opal.add_stubs('new,from_string'); 21018 21019 (function($base, $parent_nesting) { 21020 var self = $module($base, 'Kernel'); 21021 21022 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 21023 21024 return $def(self, '$Complex', function $$Complex(real, imag) { 21025 21026 21027 if (imag == null) imag = nil; 21028 if ($truthy(imag)) { 21029 return $$('Complex').$new(real, imag) 21030 } else { 21031 return $$('Complex').$new(real, 0) 21032 }; 21033 }, -2) 21034 })('::', $nesting); 21035 return (function($base, $super, $parent_nesting) { 21036 var self = $klass($base, $super, 'String'); 21037 21038 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 21039 21040 return $def(self, '$to_c', function $$to_c() { 21041 var self = this; 21042 21043 return $$('Complex').$from_string(self) 21044 }) 21045 })('::', null, $nesting); 21046}; 21047 21048Opal.modules["corelib/complex"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21049 var $klass = Opal.klass, $truthy = Opal.truthy, $eqeqeq = Opal.eqeqeq, $Kernel = Opal.Kernel, $defs = Opal.defs, $rb_times = Opal.rb_times, $def = Opal.def, $rb_plus = Opal.rb_plus, $rb_minus = Opal.rb_minus, $rb_divide = Opal.rb_divide, $eqeq = Opal.eqeq, $to_ary = Opal.to_ary, $rb_gt = Opal.rb_gt, $neqeq = Opal.neqeq, $return_val = Opal.return_val, $const_set = Opal.const_set, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 21050 21051 Opal.add_stubs('require,real?,===,raise,new,*,cos,sin,attr_reader,freeze,class,==,real,imag,Complex,-@,+,__coerced__,-,nan?,/,conj,abs2,quo,polar,exp,log,>,!=,divmod,**,hypot,atan2,lcm,denominator,finite?,infinite?,numerator,abs,arg,rationalize,to_f,to_i,to_r,inspect,zero?,positive?,Rational,rect,angle'); 21052 21053 self.$require("corelib/numeric"); 21054 self.$require("corelib/complex/base"); 21055 return (function($base, $super, $parent_nesting) { 21056 var self = $klass($base, $super, 'Complex'); 21057 21058 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 21059 21060 $proto.real = $proto.imag = nil; 21061 21062 $defs(self, '$rect', function $$rect(real, imag) { 21063 var self = this; 21064 21065 21066 if (imag == null) imag = 0; 21067 if (!((($eqeqeq($$$('Numeric'), real) && ($truthy(real['$real?']()))) && ($eqeqeq($$$('Numeric'), imag))) && ($truthy(imag['$real?']())))) { 21068 $Kernel.$raise($$$('TypeError'), "not a real") 21069 }; 21070 return self.$new(real, imag); 21071 }, -2); 21072 $defs(self, '$polar', function $$polar(r, theta) { 21073 var self = this; 21074 21075 21076 if (theta == null) theta = 0; 21077 if (!((($eqeqeq($$$('Numeric'), r) && ($truthy(r['$real?']()))) && ($eqeqeq($$$('Numeric'), theta))) && ($truthy(theta['$real?']())))) { 21078 $Kernel.$raise($$$('TypeError'), "not a real") 21079 }; 21080 return self.$new($rb_times(r, $$$('Math').$cos(theta)), $rb_times(r, $$$('Math').$sin(theta))); 21081 }, -2); 21082 self.$attr_reader("real", "imag"); 21083 21084 $def(self, '$initialize', function $$initialize(real, imag) { 21085 var self = this; 21086 21087 21088 if (imag == null) imag = 0; 21089 self.real = real; 21090 self.imag = imag; 21091 return self.$freeze(); 21092 }, -2); 21093 21094 $def(self, '$coerce', function $$coerce(other) { 21095 var self = this; 21096 21097 if ($eqeqeq($$$('Complex'), other)) { 21098 return [other, self] 21099 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21100 return [$$$('Complex').$new(other, 0), self] 21101 } else { 21102 return $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Complex") 21103 } 21104 }); 21105 21106 $def(self, '$==', function $Complex_$eq_eq$1(other) { 21107 var self = this, $ret_or_1 = nil; 21108 21109 if ($eqeqeq($$$('Complex'), other)) { 21110 if ($truthy(($ret_or_1 = self.real['$=='](other.$real())))) { 21111 return self.imag['$=='](other.$imag()) 21112 } else { 21113 return $ret_or_1 21114 } 21115 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21116 if ($truthy(($ret_or_1 = self.real['$=='](other)))) { 21117 return self.imag['$=='](0) 21118 } else { 21119 return $ret_or_1 21120 } 21121 } else { 21122 return other['$=='](self) 21123 } 21124 }); 21125 21126 $def(self, '$-@', function $Complex_$minus$$2() { 21127 var self = this; 21128 21129 return $Kernel.$Complex(self.real['$-@'](), self.imag['$-@']()) 21130 }); 21131 21132 $def(self, '$+', function $Complex_$plus$3(other) { 21133 var self = this; 21134 21135 if ($eqeqeq($$$('Complex'), other)) { 21136 return $Kernel.$Complex($rb_plus(self.real, other.$real()), $rb_plus(self.imag, other.$imag())) 21137 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21138 return $Kernel.$Complex($rb_plus(self.real, other), self.imag) 21139 } else { 21140 return self.$__coerced__("+", other) 21141 } 21142 }); 21143 21144 $def(self, '$-', function $Complex_$minus$4(other) { 21145 var self = this; 21146 21147 if ($eqeqeq($$$('Complex'), other)) { 21148 return $Kernel.$Complex($rb_minus(self.real, other.$real()), $rb_minus(self.imag, other.$imag())) 21149 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21150 return $Kernel.$Complex($rb_minus(self.real, other), self.imag) 21151 } else { 21152 return self.$__coerced__("-", other) 21153 } 21154 }); 21155 21156 $def(self, '$*', function $Complex_$$5(other) { 21157 var self = this; 21158 21159 if ($eqeqeq($$$('Complex'), other)) { 21160 return $Kernel.$Complex($rb_minus($rb_times(self.real, other.$real()), $rb_times(self.imag, other.$imag())), $rb_plus($rb_times(self.real, other.$imag()), $rb_times(self.imag, other.$real()))) 21161 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21162 return $Kernel.$Complex($rb_times(self.real, other), $rb_times(self.imag, other)) 21163 } else { 21164 return self.$__coerced__("*", other) 21165 } 21166 }); 21167 21168 $def(self, '$/', function $Complex_$slash$6(other) { 21169 var self = this; 21170 21171 if ($eqeqeq($$$('Complex'), other)) { 21172 if ((((($eqeqeq($$$('Number'), self.real) && ($truthy(self.real['$nan?']()))) || (($eqeqeq($$$('Number'), self.imag) && ($truthy(self.imag['$nan?']()))))) || (($eqeqeq($$$('Number'), other.$real()) && ($truthy(other.$real()['$nan?']()))))) || (($eqeqeq($$$('Number'), other.$imag()) && ($truthy(other.$imag()['$nan?']())))))) { 21173 return $$$('Complex').$new($$$($$$('Float'), 'NAN'), $$$($$$('Float'), 'NAN')) 21174 } else { 21175 return $rb_divide($rb_times(self, other.$conj()), other.$abs2()) 21176 } 21177 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 21178 return $Kernel.$Complex(self.real.$quo(other), self.imag.$quo(other)) 21179 } else { 21180 return self.$__coerced__("/", other) 21181 } 21182 }); 21183 21184 $def(self, '$**', function $Complex_$$$7(other) { 21185 var $a, $b, self = this, r = nil, theta = nil, ore = nil, oim = nil, nr = nil, ntheta = nil, x = nil, z = nil, n = nil, div = nil, mod = nil; 21186 21187 21188 if ($eqeq(other, 0)) { 21189 return $$$('Complex').$new(1, 0) 21190 }; 21191 if ($eqeqeq($$$('Complex'), other)) { 21192 21193 $b = self.$polar(), $a = $to_ary($b), (r = ($a[0] == null ? nil : $a[0])), (theta = ($a[1] == null ? nil : $a[1])), $b; 21194 ore = other.$real(); 21195 oim = other.$imag(); 21196 nr = $$$('Math').$exp($rb_minus($rb_times(ore, $$$('Math').$log(r)), $rb_times(oim, theta))); 21197 ntheta = $rb_plus($rb_times(theta, ore), $rb_times(oim, $$$('Math').$log(r))); 21198 return $$$('Complex').$polar(nr, ntheta); 21199 } else if ($eqeqeq($$$('Integer'), other)) { 21200 if ($truthy($rb_gt(other, 0))) { 21201 21202 x = self; 21203 z = x; 21204 n = $rb_minus(other, 1); 21205 while ($neqeq(n, 0)) { 21206 21207 $b = n.$divmod(2), $a = $to_ary($b), (div = ($a[0] == null ? nil : $a[0])), (mod = ($a[1] == null ? nil : $a[1])), $b; 21208 while ($eqeq(mod, 0)) { 21209 21210 x = $Kernel.$Complex($rb_minus($rb_times(x.$real(), x.$real()), $rb_times(x.$imag(), x.$imag())), $rb_times($rb_times(2, x.$real()), x.$imag())); 21211 n = div; 21212 $b = n.$divmod(2), $a = $to_ary($b), (div = ($a[0] == null ? nil : $a[0])), (mod = ($a[1] == null ? nil : $a[1])), $b; 21213 }; 21214 z = $rb_times(z, x); 21215 n = $rb_minus(n, 1); 21216 }; 21217 return z; 21218 } else { 21219 return $rb_divide($$$('Rational').$new(1, 1), self)['$**'](other['$-@']()) 21220 } 21221 } else if (($eqeqeq($$$('Float'), other) || ($eqeqeq($$$('Rational'), other)))) { 21222 21223 $b = self.$polar(), $a = $to_ary($b), (r = ($a[0] == null ? nil : $a[0])), (theta = ($a[1] == null ? nil : $a[1])), $b; 21224 return $$$('Complex').$polar(r['$**'](other), $rb_times(theta, other)); 21225 } else { 21226 return self.$__coerced__("**", other) 21227 }; 21228 }); 21229 21230 $def(self, '$abs', function $$abs() { 21231 var self = this; 21232 21233 return $$$('Math').$hypot(self.real, self.imag) 21234 }); 21235 21236 $def(self, '$abs2', function $$abs2() { 21237 var self = this; 21238 21239 return $rb_plus($rb_times(self.real, self.real), $rb_times(self.imag, self.imag)) 21240 }); 21241 21242 $def(self, '$angle', function $$angle() { 21243 var self = this; 21244 21245 return $$$('Math').$atan2(self.imag, self.real) 21246 }); 21247 21248 $def(self, '$conj', function $$conj() { 21249 var self = this; 21250 21251 return $Kernel.$Complex(self.real, self.imag['$-@']()) 21252 }); 21253 21254 $def(self, '$denominator', function $$denominator() { 21255 var self = this; 21256 21257 return self.real.$denominator().$lcm(self.imag.$denominator()) 21258 }); 21259 21260 $def(self, '$eql?', function $Complex_eql$ques$8(other) { 21261 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 21262 21263 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = $$('Complex')['$==='](other))) ? (self.real.$class()['$=='](self.imag.$class())) : ($ret_or_2))))) { 21264 return self['$=='](other) 21265 } else { 21266 return $ret_or_1 21267 } 21268 }); 21269 21270 $def(self, '$fdiv', function $$fdiv(other) { 21271 var self = this; 21272 21273 21274 if (!$eqeqeq($$$('Numeric'), other)) { 21275 $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Complex") 21276 }; 21277 return $rb_divide(self, other); 21278 }); 21279 21280 $def(self, '$finite?', function $Complex_finite$ques$9() { 21281 var self = this, $ret_or_1 = nil; 21282 21283 if ($truthy(($ret_or_1 = self.real['$finite?']()))) { 21284 return self.imag['$finite?']() 21285 } else { 21286 return $ret_or_1 21287 } 21288 }); 21289 21290 $def(self, '$hash', function $$hash() { 21291 var self = this; 21292 21293 return "Complex:" + (self.real) + ":" + (self.imag) 21294 }); 21295 21296 $def(self, '$infinite?', function $Complex_infinite$ques$10() { 21297 var self = this, $ret_or_1 = nil; 21298 21299 if ($truthy(($ret_or_1 = self.real['$infinite?']()))) { 21300 return $ret_or_1 21301 } else { 21302 return self.imag['$infinite?']() 21303 } 21304 }); 21305 21306 $def(self, '$inspect', function $$inspect() { 21307 var self = this; 21308 21309 return "(" + (self) + ")" 21310 }); 21311 21312 $def(self, '$numerator', function $$numerator() { 21313 var self = this, d = nil; 21314 21315 21316 d = self.$denominator(); 21317 return $Kernel.$Complex($rb_times(self.real.$numerator(), $rb_divide(d, self.real.$denominator())), $rb_times(self.imag.$numerator(), $rb_divide(d, self.imag.$denominator()))); 21318 }); 21319 21320 $def(self, '$polar', function $$polar() { 21321 var self = this; 21322 21323 return [self.$abs(), self.$arg()] 21324 }); 21325 21326 $def(self, '$rationalize', function $$rationalize(eps) { 21327 var self = this; 21328 21329 21330 ; 21331 21332 if (arguments.length > 1) { 21333 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 21334 } 21335 ; 21336 if ($neqeq(self.imag, 0)) { 21337 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Rational") 21338 }; 21339 return self.$real().$rationalize(eps); 21340 }, -1); 21341 21342 $def(self, '$real?', $return_val(false)); 21343 21344 $def(self, '$rect', function $$rect() { 21345 var self = this; 21346 21347 return [self.real, self.imag] 21348 }); 21349 21350 $def(self, '$to_f', function $$to_f() { 21351 var self = this; 21352 21353 21354 if (!$eqeq(self.imag, 0)) { 21355 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Float") 21356 }; 21357 return self.real.$to_f(); 21358 }); 21359 21360 $def(self, '$to_i', function $$to_i() { 21361 var self = this; 21362 21363 21364 if (!$eqeq(self.imag, 0)) { 21365 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Integer") 21366 }; 21367 return self.real.$to_i(); 21368 }); 21369 21370 $def(self, '$to_r', function $$to_r() { 21371 var self = this; 21372 21373 21374 if (!$eqeq(self.imag, 0)) { 21375 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Rational") 21376 }; 21377 return self.real.$to_r(); 21378 }); 21379 21380 $def(self, '$to_s', function $$to_s() { 21381 var self = this, result = nil; 21382 21383 21384 result = self.real.$inspect(); 21385 result = $rb_plus(result, (((($eqeqeq($$$('Number'), self.imag) && ($truthy(self.imag['$nan?']()))) || ($truthy(self.imag['$positive?']()))) || ($truthy(self.imag['$zero?']()))) ? ("+") : ("-"))); 21386 result = $rb_plus(result, self.imag.$abs().$inspect()); 21387 if (($eqeqeq($$$('Number'), self.imag) && (($truthy(self.imag['$nan?']()) || ($truthy(self.imag['$infinite?']())))))) { 21388 result = $rb_plus(result, "*") 21389 }; 21390 return $rb_plus(result, "i"); 21391 }); 21392 $const_set($nesting[0], 'I', self.$new(0, 1)); 21393 $defs(self, '$from_string', function $$from_string(str) { 21394 21395 21396 var re = /[+-]?[\d_]+(\.[\d_]+)?(e\d+)?/, 21397 match = str.match(re), 21398 real, imag, denominator; 21399 21400 function isFloat() { 21401 return re.test(str); 21402 } 21403 21404 function cutFloat() { 21405 var match = str.match(re); 21406 var number = match[0]; 21407 str = str.slice(number.length); 21408 return number.replace(/_/g, ''); 21409 } 21410 21411 // handles both floats and rationals 21412 function cutNumber() { 21413 if (isFloat()) { 21414 var numerator = parseFloat(cutFloat()); 21415 21416 if (str[0] === '/') { 21417 // rational real part 21418 str = str.slice(1); 21419 21420 if (isFloat()) { 21421 var denominator = parseFloat(cutFloat()); 21422 return $Kernel.$Rational(numerator, denominator); 21423 } else { 21424 // reverting '/' 21425 str = '/' + str; 21426 return numerator; 21427 } 21428 } else { 21429 // float real part, no denominator 21430 return numerator; 21431 } 21432 } else { 21433 return null; 21434 } 21435 } 21436 21437 real = cutNumber(); 21438 21439 if (!real) { 21440 if (str[0] === 'i') { 21441 // i => Complex(0, 1) 21442 return $Kernel.$Complex(0, 1); 21443 } 21444 if (str[0] === '-' && str[1] === 'i') { 21445 // -i => Complex(0, -1) 21446 return $Kernel.$Complex(0, -1); 21447 } 21448 if (str[0] === '+' && str[1] === 'i') { 21449 // +i => Complex(0, 1) 21450 return $Kernel.$Complex(0, 1); 21451 } 21452 // anything => Complex(0, 0) 21453 return $Kernel.$Complex(0, 0); 21454 } 21455 21456 imag = cutNumber(); 21457 if (!imag) { 21458 if (str[0] === 'i') { 21459 // 3i => Complex(0, 3) 21460 return $Kernel.$Complex(0, real); 21461 } else { 21462 // 3 => Complex(3, 0) 21463 return $Kernel.$Complex(real, 0); 21464 } 21465 } else { 21466 // 3+2i => Complex(3, 2) 21467 return $Kernel.$Complex(real, imag); 21468 } 21469 21470 }); 21471 (function(self, $parent_nesting) { 21472 21473 return $alias(self, "rectangular", "rect") 21474 })(Opal.get_singleton_class(self), $nesting); 21475 $alias(self, "arg", "angle"); 21476 $alias(self, "conjugate", "conj"); 21477 $alias(self, "divide", "/"); 21478 $alias(self, "imaginary", "imag"); 21479 $alias(self, "magnitude", "abs"); 21480 $alias(self, "phase", "arg"); 21481 $alias(self, "quo", "/"); 21482 $alias(self, "rectangular", "rect"); 21483 21484 Opal.udef(self, '$' + "negative?");; 21485 21486 Opal.udef(self, '$' + "positive?");; 21487 21488 21489 Opal.udef(self, '$' + "step");; 21490 return nil;; 21491 })('::', $$$('Numeric'), $nesting); 21492}; 21493 21494Opal.modules["corelib/rational/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21495 var $module = Opal.module, $def = Opal.def, $klass = Opal.klass, nil = Opal.nil, $$$ = Opal.$$$; 21496 21497 Opal.add_stubs('convert,from_string'); 21498 21499 (function($base) { 21500 var self = $module($base, 'Kernel'); 21501 21502 21503 return $def(self, '$Rational', function $$Rational(numerator, denominator) { 21504 21505 21506 if (denominator == null) denominator = 1; 21507 return $$$('Rational').$convert(numerator, denominator); 21508 }, -2) 21509 })('::'); 21510 return (function($base, $super) { 21511 var self = $klass($base, $super, 'String'); 21512 21513 21514 return $def(self, '$to_r', function $$to_r() { 21515 var self = this; 21516 21517 return $$$('Rational').$from_string(self) 21518 }) 21519 })('::', null); 21520}; 21521 21522Opal.modules["corelib/rational"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21523 var $klass = Opal.klass, $eqeq = Opal.eqeq, $Kernel = Opal.Kernel, $truthy = Opal.truthy, $rb_lt = Opal.rb_lt, $rb_divide = Opal.rb_divide, $defs = Opal.defs, $eqeqeq = Opal.eqeqeq, $not = Opal.not, $Opal = Opal.Opal, $def = Opal.def, $return_ivar = Opal.return_ivar, $rb_minus = Opal.rb_minus, $rb_times = Opal.rb_times, $rb_plus = Opal.rb_plus, $rb_gt = Opal.rb_gt, $rb_le = Opal.rb_le, $return_self = Opal.return_self, $alias = Opal.alias, self = Opal.top, nil = Opal.nil, $$$ = Opal.$$$; 21524 21525 Opal.add_stubs('require,to_i,==,raise,<,-@,new,gcd,/,nil?,===,reduce,to_r,!,equal?,coerce_to!,freeze,to_f,numerator,denominator,<=>,-,*,__coerced__,+,Rational,>,**,abs,ceil,with_precision,floor,<=,truncate,send'); 21526 21527 self.$require("corelib/numeric"); 21528 self.$require("corelib/rational/base"); 21529 return (function($base, $super) { 21530 var self = $klass($base, $super, 'Rational'); 21531 21532 var $proto = self.$$prototype; 21533 21534 $proto.num = $proto.den = nil; 21535 21536 $defs(self, '$reduce', function $$reduce(num, den) { 21537 var self = this, gcd = nil; 21538 21539 21540 num = num.$to_i(); 21541 den = den.$to_i(); 21542 if ($eqeq(den, 0)) { 21543 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0") 21544 } else if ($truthy($rb_lt(den, 0))) { 21545 21546 num = num['$-@'](); 21547 den = den['$-@'](); 21548 } else if ($eqeq(den, 1)) { 21549 return self.$new(num, den) 21550 }; 21551 gcd = num.$gcd(den); 21552 return self.$new($rb_divide(num, gcd), $rb_divide(den, gcd)); 21553 }); 21554 $defs(self, '$convert', function $$convert(num, den) { 21555 var self = this; 21556 21557 21558 if (($truthy(num['$nil?']()) || ($truthy(den['$nil?']())))) { 21559 $Kernel.$raise($$$('TypeError'), "cannot convert nil into Rational") 21560 }; 21561 if (($eqeqeq($$$('Integer'), num) && ($eqeqeq($$$('Integer'), den)))) { 21562 return self.$reduce(num, den) 21563 }; 21564 if ((($eqeqeq($$$('Float'), num) || ($eqeqeq($$$('String'), num))) || ($eqeqeq($$$('Complex'), num)))) { 21565 num = num.$to_r() 21566 }; 21567 if ((($eqeqeq($$$('Float'), den) || ($eqeqeq($$$('String'), den))) || ($eqeqeq($$$('Complex'), den)))) { 21568 den = den.$to_r() 21569 }; 21570 if (($truthy(den['$equal?'](1)) && ($not($$$('Integer')['$==='](num))))) { 21571 return $Opal['$coerce_to!'](num, $$$('Rational'), "to_r") 21572 } else if (($eqeqeq($$$('Numeric'), num) && ($eqeqeq($$$('Numeric'), den)))) { 21573 return $rb_divide(num, den) 21574 } else { 21575 return self.$reduce(num, den) 21576 }; 21577 }); 21578 21579 $def(self, '$initialize', function $$initialize(num, den) { 21580 var self = this; 21581 21582 21583 self.num = num; 21584 self.den = den; 21585 return self.$freeze(); 21586 }); 21587 21588 $def(self, '$numerator', $return_ivar("num")); 21589 21590 $def(self, '$denominator', $return_ivar("den")); 21591 21592 $def(self, '$coerce', function $$coerce(other) { 21593 var self = this, $ret_or_1 = nil; 21594 21595 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21596 return [other, self] 21597 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21598 return [other.$to_r(), self] 21599 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21600 return [other, self.$to_f()] 21601 } else { 21602 return nil 21603 } 21604 }); 21605 21606 $def(self, '$==', function $Rational_$eq_eq$1(other) { 21607 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 21608 21609 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21610 if ($truthy(($ret_or_2 = self.num['$=='](other.$numerator())))) { 21611 return self.den['$=='](other.$denominator()) 21612 } else { 21613 return $ret_or_2 21614 } 21615 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21616 if ($truthy(($ret_or_2 = self.num['$=='](other)))) { 21617 return self.den['$=='](1) 21618 } else { 21619 return $ret_or_2 21620 } 21621 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21622 return self.$to_f()['$=='](other) 21623 } else { 21624 return other['$=='](self) 21625 } 21626 }); 21627 21628 $def(self, '$<=>', function $Rational_$lt_eq_gt$2(other) { 21629 var self = this, $ret_or_1 = nil; 21630 21631 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21632 return $rb_minus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator()))['$<=>'](0) 21633 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21634 return $rb_minus(self.num, $rb_times(self.den, other))['$<=>'](0) 21635 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21636 return self.$to_f()['$<=>'](other) 21637 } else { 21638 return self.$__coerced__("<=>", other) 21639 } 21640 }); 21641 21642 $def(self, '$+', function $Rational_$plus$3(other) { 21643 var self = this, $ret_or_1 = nil, num = nil, den = nil; 21644 21645 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21646 21647 num = $rb_plus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator())); 21648 den = $rb_times(self.den, other.$denominator()); 21649 return $Kernel.$Rational(num, den); 21650 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21651 return $Kernel.$Rational($rb_plus(self.num, $rb_times(other, self.den)), self.den) 21652 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21653 return $rb_plus(self.$to_f(), other) 21654 } else { 21655 return self.$__coerced__("+", other) 21656 } 21657 }); 21658 21659 $def(self, '$-', function $Rational_$minus$4(other) { 21660 var self = this, $ret_or_1 = nil, num = nil, den = nil; 21661 21662 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21663 21664 num = $rb_minus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator())); 21665 den = $rb_times(self.den, other.$denominator()); 21666 return $Kernel.$Rational(num, den); 21667 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21668 return $Kernel.$Rational($rb_minus(self.num, $rb_times(other, self.den)), self.den) 21669 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21670 return $rb_minus(self.$to_f(), other) 21671 } else { 21672 return self.$__coerced__("-", other) 21673 } 21674 }); 21675 21676 $def(self, '$*', function $Rational_$$5(other) { 21677 var self = this, $ret_or_1 = nil, num = nil, den = nil; 21678 21679 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21680 21681 num = $rb_times(self.num, other.$numerator()); 21682 den = $rb_times(self.den, other.$denominator()); 21683 return $Kernel.$Rational(num, den); 21684 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21685 return $Kernel.$Rational($rb_times(self.num, other), self.den) 21686 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21687 return $rb_times(self.$to_f(), other) 21688 } else { 21689 return self.$__coerced__("*", other) 21690 } 21691 }); 21692 21693 $def(self, '$/', function $Rational_$slash$6(other) { 21694 var self = this, $ret_or_1 = nil, num = nil, den = nil; 21695 21696 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 21697 21698 num = $rb_times(self.num, other.$denominator()); 21699 den = $rb_times(self.den, other.$numerator()); 21700 return $Kernel.$Rational(num, den); 21701 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 21702 if ($eqeq(other, 0)) { 21703 return $rb_divide(self.$to_f(), 0.0) 21704 } else { 21705 return $Kernel.$Rational(self.num, $rb_times(self.den, other)) 21706 } 21707 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21708 return $rb_divide(self.$to_f(), other) 21709 } else { 21710 return self.$__coerced__("/", other) 21711 } 21712 }); 21713 21714 $def(self, '$**', function $Rational_$$$7(other) { 21715 var self = this, $ret_or_1 = nil; 21716 21717 if ($eqeqeq($$$('Integer'), ($ret_or_1 = other))) { 21718 if (($eqeq(self, 0) && ($truthy($rb_lt(other, 0))))) { 21719 return $$$($$$('Float'), 'INFINITY') 21720 } else if ($truthy($rb_gt(other, 0))) { 21721 return $Kernel.$Rational(self.num['$**'](other), self.den['$**'](other)) 21722 } else if ($truthy($rb_lt(other, 0))) { 21723 return $Kernel.$Rational(self.den['$**'](other['$-@']()), self.num['$**'](other['$-@']())) 21724 } else { 21725 return $Kernel.$Rational(1, 1) 21726 } 21727 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 21728 return self.$to_f()['$**'](other) 21729 } else if ($eqeqeq($$$('Rational'), $ret_or_1)) { 21730 if ($eqeq(other, 0)) { 21731 return $Kernel.$Rational(1, 1) 21732 } else if ($eqeq(other.$denominator(), 1)) { 21733 if ($truthy($rb_lt(other, 0))) { 21734 return $Kernel.$Rational(self.den['$**'](other.$numerator().$abs()), self.num['$**'](other.$numerator().$abs())) 21735 } else { 21736 return $Kernel.$Rational(self.num['$**'](other.$numerator()), self.den['$**'](other.$numerator())) 21737 } 21738 } else if (($eqeq(self, 0) && ($truthy($rb_lt(other, 0))))) { 21739 return $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0") 21740 } else { 21741 return self.$to_f()['$**'](other) 21742 } 21743 } else { 21744 return self.$__coerced__("**", other) 21745 } 21746 }); 21747 21748 $def(self, '$abs', function $$abs() { 21749 var self = this; 21750 21751 return $Kernel.$Rational(self.num.$abs(), self.den.$abs()) 21752 }); 21753 21754 $def(self, '$ceil', function $$ceil(precision) { 21755 var self = this; 21756 21757 21758 if (precision == null) precision = 0; 21759 if ($eqeq(precision, 0)) { 21760 return $rb_divide(self.num['$-@'](), self.den)['$-@']().$ceil() 21761 } else { 21762 return self.$with_precision("ceil", precision) 21763 }; 21764 }, -1); 21765 21766 $def(self, '$floor', function $$floor(precision) { 21767 var self = this; 21768 21769 21770 if (precision == null) precision = 0; 21771 if ($eqeq(precision, 0)) { 21772 return $rb_divide(self.num['$-@'](), self.den)['$-@']().$floor() 21773 } else { 21774 return self.$with_precision("floor", precision) 21775 }; 21776 }, -1); 21777 21778 $def(self, '$hash', function $$hash() { 21779 var self = this; 21780 21781 return "Rational:" + (self.num) + ":" + (self.den) 21782 }); 21783 21784 $def(self, '$inspect', function $$inspect() { 21785 var self = this; 21786 21787 return "(" + (self) + ")" 21788 }); 21789 21790 $def(self, '$rationalize', function $$rationalize(eps) { 21791 var self = this; 21792 21793 21794 ; 21795 21796 if (arguments.length > 1) { 21797 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 21798 } 21799 21800 if (eps == null) { 21801 return self; 21802 } 21803 21804 var e = eps.$abs(), 21805 a = $rb_minus(self, e), 21806 b = $rb_plus(self, e); 21807 21808 var p0 = 0, 21809 p1 = 1, 21810 q0 = 1, 21811 q1 = 0, 21812 p2, q2; 21813 21814 var c, k, t; 21815 21816 while (true) { 21817 c = (a).$ceil(); 21818 21819 if ($rb_le(c, b)) { 21820 break; 21821 } 21822 21823 k = c - 1; 21824 p2 = k * p1 + p0; 21825 q2 = k * q1 + q0; 21826 t = $rb_divide(1, $rb_minus(b, k)); 21827 b = $rb_divide(1, $rb_minus(a, k)); 21828 a = t; 21829 21830 p0 = p1; 21831 q0 = q1; 21832 p1 = p2; 21833 q1 = q2; 21834 } 21835 21836 return $Kernel.$Rational(c * p1 + p0, c * q1 + q0); 21837 ; 21838 }, -1); 21839 21840 $def(self, '$round', function $$round(precision) { 21841 var self = this, num = nil, den = nil, approx = nil; 21842 21843 21844 if (precision == null) precision = 0; 21845 if (!$eqeq(precision, 0)) { 21846 return self.$with_precision("round", precision) 21847 }; 21848 if ($eqeq(self.num, 0)) { 21849 return 0 21850 }; 21851 if ($eqeq(self.den, 1)) { 21852 return self.num 21853 }; 21854 num = $rb_plus($rb_times(self.num.$abs(), 2), self.den); 21855 den = $rb_times(self.den, 2); 21856 approx = $rb_divide(num, den).$truncate(); 21857 if ($truthy($rb_lt(self.num, 0))) { 21858 return approx['$-@']() 21859 } else { 21860 return approx 21861 }; 21862 }, -1); 21863 21864 $def(self, '$to_f', function $$to_f() { 21865 var self = this; 21866 21867 return $rb_divide(self.num, self.den) 21868 }); 21869 21870 $def(self, '$to_i', function $$to_i() { 21871 var self = this; 21872 21873 return self.$truncate() 21874 }); 21875 21876 $def(self, '$to_r', $return_self); 21877 21878 $def(self, '$to_s', function $$to_s() { 21879 var self = this; 21880 21881 return "" + (self.num) + "/" + (self.den) 21882 }); 21883 21884 $def(self, '$truncate', function $$truncate(precision) { 21885 var self = this; 21886 21887 21888 if (precision == null) precision = 0; 21889 if ($eqeq(precision, 0)) { 21890 if ($truthy($rb_lt(self.num, 0))) { 21891 return self.$ceil() 21892 } else { 21893 return self.$floor() 21894 } 21895 } else { 21896 return self.$with_precision("truncate", precision) 21897 }; 21898 }, -1); 21899 21900 $def(self, '$with_precision', function $$with_precision(method, precision) { 21901 var self = this, p = nil, s = nil; 21902 21903 21904 if (!$eqeqeq($$$('Integer'), precision)) { 21905 $Kernel.$raise($$$('TypeError'), "not an Integer") 21906 }; 21907 p = (10)['$**'](precision); 21908 s = $rb_times(self, p); 21909 if ($truthy($rb_lt(precision, 1))) { 21910 return $rb_divide(s.$send(method), p).$to_i() 21911 } else { 21912 return $Kernel.$Rational(s.$send(method), p) 21913 }; 21914 }); 21915 $defs(self, '$from_string', function $$from_string(string) { 21916 21917 21918 var str = string.trimLeft(), 21919 re = /^[+-]?[\d_]+(\.[\d_]+)?/, 21920 match = str.match(re), 21921 numerator, denominator; 21922 21923 function isFloat() { 21924 return re.test(str); 21925 } 21926 21927 function cutFloat() { 21928 var match = str.match(re); 21929 var number = match[0]; 21930 str = str.slice(number.length); 21931 return number.replace(/_/g, ''); 21932 } 21933 21934 if (isFloat()) { 21935 numerator = parseFloat(cutFloat()); 21936 21937 if (str[0] === '/') { 21938 // rational real part 21939 str = str.slice(1); 21940 21941 if (isFloat()) { 21942 denominator = parseFloat(cutFloat()); 21943 return $Kernel.$Rational(numerator, denominator); 21944 } else { 21945 return $Kernel.$Rational(numerator, 1); 21946 } 21947 } else { 21948 return $Kernel.$Rational(numerator, 1); 21949 } 21950 } else { 21951 return $Kernel.$Rational(0, 1); 21952 } 21953 21954 }); 21955 $alias(self, "divide", "/"); 21956 return $alias(self, "quo", "/"); 21957 })('::', $$$('Numeric')); 21958}; 21959 21960Opal.modules["corelib/time"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21961 var $slice = Opal.slice, $deny_frozen_access = Opal.deny_frozen_access, $klass = Opal.klass, $Kernel = Opal.Kernel, $Opal = Opal.Opal, $defs = Opal.defs, $eqeqeq = Opal.eqeqeq, $def = Opal.def, $truthy = Opal.truthy, $rb_gt = Opal.rb_gt, $rb_lt = Opal.rb_lt, $send = Opal.send, $rb_plus = Opal.rb_plus, $rb_divide = Opal.rb_divide, $rb_minus = Opal.rb_minus, $range = Opal.range, $neqeq = Opal.neqeq, $rb_le = Opal.rb_le, $eqeq = Opal.eqeq, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 21962 21963 Opal.add_stubs('require,include,===,raise,coerce_to!,respond_to?,to_str,to_i,_parse_offset,new,<=>,to_f,nil?,>,<,strftime,each,define_method,year,month,day,+,round,/,-,copy_instance_variables,initialize_dup,is_a?,zero?,wday,utc?,mon,yday,hour,min,sec,rjust,ljust,zone,to_s,[],cweek_cyear,jd,to_date,format,isdst,!=,<=,==,ceil,local,gm,asctime,getgm,gmt_offset,inspect,usec,gmtime,gmt?'); 21964 21965 self.$require("corelib/comparable"); 21966 return (function($base, $super, $parent_nesting) { 21967 var self = $klass($base, $super, 'Time'); 21968 21969 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 21970 21971 21972 self.$include($$$('Comparable')); 21973 21974 var days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], 21975 short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], 21976 short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], 21977 long_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 21978 ; 21979 $defs(self, '$at', function $$at(seconds, frac) { 21980 21981 21982 ; 21983 21984 var result; 21985 21986 if ($$$('Time')['$==='](seconds)) { 21987 if (frac !== undefined) { 21988 $Kernel.$raise($$$('TypeError'), "can't convert Time into an exact number") 21989 } 21990 result = new Date(seconds.getTime()); 21991 result.timezone = seconds.timezone; 21992 return result; 21993 } 21994 21995 if (!seconds.$$is_number) { 21996 seconds = $Opal['$coerce_to!'](seconds, $$$('Integer'), "to_int"); 21997 } 21998 21999 if (frac === undefined) { 22000 return new Date(seconds * 1000); 22001 } 22002 22003 if (!frac.$$is_number) { 22004 frac = $Opal['$coerce_to!'](frac, $$$('Integer'), "to_int"); 22005 } 22006 22007 return new Date(seconds * 1000 + (frac / 1000)); 22008 ; 22009 }, -2); 22010 22011 function time_params(year, month, day, hour, min, sec) { 22012 if (year.$$is_string) { 22013 year = parseInt(year, 10); 22014 } else { 22015 year = $Opal['$coerce_to!'](year, $$$('Integer'), "to_int"); 22016 } 22017 22018 if (month === nil) { 22019 month = 1; 22020 } else if (!month.$$is_number) { 22021 if ((month)['$respond_to?']("to_str")) { 22022 month = (month).$to_str(); 22023 switch (month.toLowerCase()) { 22024 case 'jan': month = 1; break; 22025 case 'feb': month = 2; break; 22026 case 'mar': month = 3; break; 22027 case 'apr': month = 4; break; 22028 case 'may': month = 5; break; 22029 case 'jun': month = 6; break; 22030 case 'jul': month = 7; break; 22031 case 'aug': month = 8; break; 22032 case 'sep': month = 9; break; 22033 case 'oct': month = 10; break; 22034 case 'nov': month = 11; break; 22035 case 'dec': month = 12; break; 22036 default: month = (month).$to_i(); 22037 } 22038 } else { 22039 month = $Opal['$coerce_to!'](month, $$$('Integer'), "to_int"); 22040 } 22041 } 22042 22043 if (month < 1 || month > 12) { 22044 $Kernel.$raise($$$('ArgumentError'), "month out of range: " + (month)) 22045 } 22046 month = month - 1; 22047 22048 if (day === nil) { 22049 day = 1; 22050 } else if (day.$$is_string) { 22051 day = parseInt(day, 10); 22052 } else { 22053 day = $Opal['$coerce_to!'](day, $$$('Integer'), "to_int"); 22054 } 22055 22056 if (day < 1 || day > 31) { 22057 $Kernel.$raise($$$('ArgumentError'), "day out of range: " + (day)) 22058 } 22059 22060 if (hour === nil) { 22061 hour = 0; 22062 } else if (hour.$$is_string) { 22063 hour = parseInt(hour, 10); 22064 } else { 22065 hour = $Opal['$coerce_to!'](hour, $$$('Integer'), "to_int"); 22066 } 22067 22068 if (hour < 0 || hour > 24) { 22069 $Kernel.$raise($$$('ArgumentError'), "hour out of range: " + (hour)) 22070 } 22071 22072 if (min === nil) { 22073 min = 0; 22074 } else if (min.$$is_string) { 22075 min = parseInt(min, 10); 22076 } else { 22077 min = $Opal['$coerce_to!'](min, $$$('Integer'), "to_int"); 22078 } 22079 22080 if (min < 0 || min > 59) { 22081 $Kernel.$raise($$$('ArgumentError'), "min out of range: " + (min)) 22082 } 22083 22084 if (sec === nil) { 22085 sec = 0; 22086 } else if (!sec.$$is_number) { 22087 if (sec.$$is_string) { 22088 sec = parseInt(sec, 10); 22089 } else { 22090 sec = $Opal['$coerce_to!'](sec, $$$('Integer'), "to_int"); 22091 } 22092 } 22093 22094 if (sec < 0 || sec > 60) { 22095 $Kernel.$raise($$$('ArgumentError'), "sec out of range: " + (sec)) 22096 } 22097 22098 return [year, month, day, hour, min, sec]; 22099 } 22100 ; 22101 $defs(self, '$new', function $Time_new$1(year, month, day, hour, min, sec, utc_offset) { 22102 var self = this; 22103 22104 22105 ; 22106 if (month == null) month = nil; 22107 if (day == null) day = nil; 22108 if (hour == null) hour = nil; 22109 if (min == null) min = nil; 22110 if (sec == null) sec = nil; 22111 if (utc_offset == null) utc_offset = nil; 22112 22113 var args, result, timezone, utc_date; 22114 22115 if (year === undefined) { 22116 return new Date(); 22117 } 22118 22119 args = time_params(year, month, day, hour, min, sec); 22120 year = args[0]; 22121 month = args[1]; 22122 day = args[2]; 22123 hour = args[3]; 22124 min = args[4]; 22125 sec = args[5]; 22126 22127 if (utc_offset === nil) { 22128 result = new Date(year, month, day, hour, min, 0, sec * 1000); 22129 if (year < 100) { 22130 result.setFullYear(year); 22131 } 22132 return result; 22133 } 22134 22135 timezone = self.$_parse_offset(utc_offset); 22136 utc_date = new Date(Date.UTC(year, month, day, hour, min, 0, sec * 1000)); 22137 if (year < 100) { 22138 utc_date.setUTCFullYear(year); 22139 } 22140 22141 result = new Date(utc_date.getTime() - timezone * 3600000); 22142 result.timezone = timezone; 22143 22144 return result; 22145 ; 22146 }, -1); 22147 $defs(self, '$_parse_offset', function $$_parse_offset(utc_offset) { 22148 22149 22150 var timezone; 22151 if (utc_offset.$$is_string) { 22152 if (utc_offset == 'UTC') { 22153 timezone = 0; 22154 } 22155 else if(/^[+-]\d\d:[0-5]\d$/.test(utc_offset)) { 22156 var sign, hours, minutes; 22157 sign = utc_offset[0]; 22158 hours = +(utc_offset[1] + utc_offset[2]); 22159 minutes = +(utc_offset[4] + utc_offset[5]); 22160 22161 timezone = (sign == '-' ? -1 : 1) * (hours + minutes / 60); 22162 } 22163 else { 22164 // Unsupported: "A".."I","K".."Z" 22165 $Kernel.$raise($$$('ArgumentError'), "\"+HH:MM\", \"-HH:MM\", \"UTC\" expected for utc_offset: " + (utc_offset)) 22166 } 22167 } 22168 else if (utc_offset.$$is_number) { 22169 timezone = utc_offset / 3600; 22170 } 22171 else { 22172 $Kernel.$raise($$$('ArgumentError'), "Opal doesn't support other types for a timezone argument than Integer and String") 22173 } 22174 return timezone; 22175 22176 }); 22177 $defs(self, '$local', function $$local(year, month, day, hour, min, sec, millisecond, _dummy1, _dummy2, _dummy3) { 22178 22179 22180 if (month == null) month = nil; 22181 if (day == null) day = nil; 22182 if (hour == null) hour = nil; 22183 if (min == null) min = nil; 22184 if (sec == null) sec = nil; 22185 if (millisecond == null) millisecond = nil; 22186 if (_dummy1 == null) _dummy1 = nil; 22187 if (_dummy2 == null) _dummy2 = nil; 22188 if (_dummy3 == null) _dummy3 = nil; 22189 22190 var args, result; 22191 22192 if (arguments.length === 10) { 22193 args = $slice(arguments); 22194 year = args[5]; 22195 month = args[4]; 22196 day = args[3]; 22197 hour = args[2]; 22198 min = args[1]; 22199 sec = args[0]; 22200 } 22201 22202 args = time_params(year, month, day, hour, min, sec); 22203 year = args[0]; 22204 month = args[1]; 22205 day = args[2]; 22206 hour = args[3]; 22207 min = args[4]; 22208 sec = args[5]; 22209 22210 result = new Date(year, month, day, hour, min, 0, sec * 1000); 22211 if (year < 100) { 22212 result.setFullYear(year); 22213 } 22214 return result; 22215 ; 22216 }, -2); 22217 $defs(self, '$gm', function $$gm(year, month, day, hour, min, sec, millisecond, _dummy1, _dummy2, _dummy3) { 22218 22219 22220 if (month == null) month = nil; 22221 if (day == null) day = nil; 22222 if (hour == null) hour = nil; 22223 if (min == null) min = nil; 22224 if (sec == null) sec = nil; 22225 if (millisecond == null) millisecond = nil; 22226 if (_dummy1 == null) _dummy1 = nil; 22227 if (_dummy2 == null) _dummy2 = nil; 22228 if (_dummy3 == null) _dummy3 = nil; 22229 22230 var args, result; 22231 22232 if (arguments.length === 10) { 22233 args = $slice(arguments); 22234 year = args[5]; 22235 month = args[4]; 22236 day = args[3]; 22237 hour = args[2]; 22238 min = args[1]; 22239 sec = args[0]; 22240 } 22241 22242 args = time_params(year, month, day, hour, min, sec); 22243 year = args[0]; 22244 month = args[1]; 22245 day = args[2]; 22246 hour = args[3]; 22247 min = args[4]; 22248 sec = args[5]; 22249 22250 result = new Date(Date.UTC(year, month, day, hour, min, 0, sec * 1000)); 22251 if (year < 100) { 22252 result.setUTCFullYear(year); 22253 } 22254 result.timezone = 0; 22255 return result; 22256 ; 22257 }, -2); 22258 $defs(self, '$now', function $$now() { 22259 var self = this; 22260 22261 return self.$new() 22262 }); 22263 22264 $def(self, '$+', function $Time_$plus$2(other) { 22265 var self = this; 22266 22267 22268 if ($eqeqeq($$$('Time'), other)) { 22269 $Kernel.$raise($$$('TypeError'), "time + time?") 22270 }; 22271 22272 if (!other.$$is_number) { 22273 other = $Opal['$coerce_to!'](other, $$$('Integer'), "to_int"); 22274 } 22275 var result = new Date(self.getTime() + (other * 1000)); 22276 result.timezone = self.timezone; 22277 return result; 22278 ; 22279 }); 22280 22281 $def(self, '$-', function $Time_$minus$3(other) { 22282 var self = this; 22283 22284 22285 if ($eqeqeq($$$('Time'), other)) { 22286 return (self.getTime() - other.getTime()) / 1000 22287 }; 22288 22289 if (!other.$$is_number) { 22290 other = $Opal['$coerce_to!'](other, $$$('Integer'), "to_int"); 22291 } 22292 var result = new Date(self.getTime() - (other * 1000)); 22293 result.timezone = self.timezone; 22294 return result; 22295 ; 22296 }); 22297 22298 $def(self, '$<=>', function $Time_$lt_eq_gt$4(other) { 22299 var self = this, r = nil; 22300 22301 if ($eqeqeq($$$('Time'), other)) { 22302 return self.$to_f()['$<=>'](other.$to_f()) 22303 } else { 22304 22305 r = other['$<=>'](self); 22306 if ($truthy(r['$nil?']())) { 22307 return nil 22308 } else if ($truthy($rb_gt(r, 0))) { 22309 return -1 22310 } else if ($truthy($rb_lt(r, 0))) { 22311 return 1 22312 } else { 22313 return 0 22314 }; 22315 } 22316 }); 22317 22318 $def(self, '$==', function $Time_$eq_eq$5(other) { 22319 var self = this, $ret_or_1 = nil; 22320 22321 if ($truthy(($ret_or_1 = $$$('Time')['$==='](other)))) { 22322 return self.$to_f() === other.$to_f() 22323 } else { 22324 return $ret_or_1 22325 } 22326 }); 22327 22328 $def(self, '$asctime', function $$asctime() { 22329 var self = this; 22330 22331 return self.$strftime("%a %b %e %H:%M:%S %Y") 22332 }); 22333 $send([["year", "getFullYear", "getUTCFullYear"], ["mon", "getMonth", "getUTCMonth", 1], ["wday", "getDay", "getUTCDay"], ["day", "getDate", "getUTCDate"], ["hour", "getHours", "getUTCHours"], ["min", "getMinutes", "getUTCMinutes"], ["sec", "getSeconds", "getUTCSeconds"]], 'each', [], function $Time$6(method, getter, utcgetter, difference){var self = $Time$6.$$s == null ? this : $Time$6.$$s; 22334 22335 22336 if (method == null) method = nil; 22337 if (getter == null) getter = nil; 22338 if (utcgetter == null) utcgetter = nil; 22339 if (difference == null) difference = 0; 22340 return $send(self, 'define_method', [method], function $$7(){var self = $$7.$$s == null ? this : $$7.$$s; 22341 22342 22343 return difference + ((self.timezone != null) ? 22344 (new Date(self.getTime() + self.timezone * 3600000))[utcgetter]() : 22345 self[getter]()) 22346 }, {$$s: self});}, {$$arity: -4, $$s: self}); 22347 22348 $def(self, '$yday', function $$yday() { 22349 var self = this, start_of_year = nil, start_of_day = nil, one_day = nil; 22350 22351 22352 start_of_year = $$('Time').$new(self.$year()).$to_i(); 22353 start_of_day = $$('Time').$new(self.$year(), self.$month(), self.$day()).$to_i(); 22354 one_day = 86400; 22355 return $rb_plus($rb_divide($rb_minus(start_of_day, start_of_year), one_day).$round(), 1); 22356 }); 22357 22358 $def(self, '$isdst', function $$isdst() { 22359 var self = this; 22360 22361 22362 var jan = new Date(self.getFullYear(), 0, 1), 22363 jul = new Date(self.getFullYear(), 6, 1); 22364 return self.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); 22365 22366 }); 22367 22368 $def(self, '$dup', function $$dup() { 22369 var self = this, copy = nil; 22370 22371 22372 copy = new Date(self.getTime()); 22373 copy.$copy_instance_variables(self); 22374 copy.$initialize_dup(self); 22375 return copy; 22376 }); 22377 22378 $def(self, '$eql?', function $Time_eql$ques$8(other) { 22379 var self = this, $ret_or_1 = nil; 22380 22381 if ($truthy(($ret_or_1 = other['$is_a?']($$$('Time'))))) { 22382 return self['$<=>'](other)['$zero?']() 22383 } else { 22384 return $ret_or_1 22385 } 22386 }); 22387 $send([["sunday?", 0], ["monday?", 1], ["tuesday?", 2], ["wednesday?", 3], ["thursday?", 4], ["friday?", 5], ["saturday?", 6]], 'each', [], function $Time$9(method, weekday){var self = $Time$9.$$s == null ? this : $Time$9.$$s; 22388 22389 22390 if (method == null) method = nil; 22391 if (weekday == null) weekday = nil; 22392 return $send(self, 'define_method', [method], function $$10(){var self = $$10.$$s == null ? this : $$10.$$s; 22393 22394 return self.$wday() === weekday}, {$$s: self});}, {$$s: self}); 22395 22396 $def(self, '$hash', function $$hash() { 22397 var self = this; 22398 22399 return 'Time:' + self.getTime(); 22400 }); 22401 22402 $def(self, '$inspect', function $$inspect() { 22403 var self = this; 22404 22405 if ($truthy(self['$utc?']())) { 22406 return self.$strftime("%Y-%m-%d %H:%M:%S UTC") 22407 } else { 22408 return self.$strftime("%Y-%m-%d %H:%M:%S %z") 22409 } 22410 }); 22411 22412 $def(self, '$succ', function $$succ() { 22413 var self = this; 22414 22415 22416 var result = new Date(self.getTime() + 1000); 22417 result.timezone = self.timezone; 22418 return result; 22419 22420 }); 22421 22422 $def(self, '$usec', function $$usec() { 22423 var self = this; 22424 22425 return self.getMilliseconds() * 1000; 22426 }); 22427 22428 $def(self, '$zone', function $$zone() { 22429 var self = this; 22430 22431 22432 if (self.timezone === 0) return "UTC"; 22433 else if (self.timezone != null) return nil; 22434 22435 var string = self.toString(), 22436 result; 22437 22438 if (string.indexOf('(') == -1) { 22439 result = string.match(/[A-Z]{3,4}/)[0]; 22440 } 22441 else { 22442 result = string.match(/\((.+)\)(?:\s|$)/)[1] 22443 } 22444 22445 if (result == "GMT" && /(GMT\W*\d{4})/.test(string)) { 22446 return RegExp.$1; 22447 } 22448 else { 22449 return result; 22450 } 22451 22452 }); 22453 22454 $def(self, '$getgm', function $$getgm() { 22455 var self = this; 22456 22457 22458 var result = new Date(self.getTime()); 22459 result.timezone = 0; 22460 return result; 22461 22462 }); 22463 22464 $def(self, '$gmtime', function $$gmtime() { 22465 var self = this; 22466 22467 22468 if (self.timezone !== 0) { 22469 $deny_frozen_access(self); 22470 self.timezone = 0; 22471 } 22472 return self; 22473 22474 }); 22475 22476 $def(self, '$gmt?', function $Time_gmt$ques$11() { 22477 var self = this; 22478 22479 return self.timezone === 0; 22480 }); 22481 22482 $def(self, '$gmt_offset', function $$gmt_offset() { 22483 var self = this; 22484 22485 return (self.timezone != null) ? self.timezone * 60 : -self.getTimezoneOffset() * 60; 22486 }); 22487 22488 $def(self, '$strftime', function $$strftime(format) { 22489 var self = this; 22490 22491 22492 return format.replace(/%([\-_#^0]*:{0,2})(\d+)?([EO]*)(.)/g, function(full, flags, width, _, conv) { 22493 var result = "", jd, c, s, 22494 zero = flags.indexOf('0') !== -1, 22495 pad = flags.indexOf('-') === -1, 22496 blank = flags.indexOf('_') !== -1, 22497 upcase = flags.indexOf('^') !== -1, 22498 invert = flags.indexOf('#') !== -1, 22499 colons = (flags.match(':') || []).length; 22500 22501 width = parseInt(width, 10); 22502 22503 if (zero && blank) { 22504 if (flags.indexOf('0') < flags.indexOf('_')) { 22505 zero = false; 22506 } 22507 else { 22508 blank = false; 22509 } 22510 } 22511 22512 switch (conv) { 22513 case 'Y': 22514 result += self.$year(); 22515 break; 22516 22517 case 'C': 22518 zero = !blank; 22519 result += Math.round(self.$year() / 100); 22520 break; 22521 22522 case 'y': 22523 zero = !blank; 22524 result += (self.$year() % 100); 22525 break; 22526 22527 case 'm': 22528 zero = !blank; 22529 result += self.$mon(); 22530 break; 22531 22532 case 'B': 22533 result += long_months[self.$mon() - 1]; 22534 break; 22535 22536 case 'b': 22537 case 'h': 22538 blank = !zero; 22539 result += short_months[self.$mon() - 1]; 22540 break; 22541 22542 case 'd': 22543 zero = !blank 22544 result += self.$day(); 22545 break; 22546 22547 case 'e': 22548 blank = !zero 22549 result += self.$day(); 22550 break; 22551 22552 case 'j': 22553 zero = !blank; 22554 width = isNaN(width) ? 3 : width; 22555 result += self.$yday(); 22556 break; 22557 22558 case 'H': 22559 zero = !blank; 22560 result += self.$hour(); 22561 break; 22562 22563 case 'k': 22564 blank = !zero; 22565 result += self.$hour(); 22566 break; 22567 22568 case 'I': 22569 zero = !blank; 22570 result += (self.$hour() % 12 || 12); 22571 break; 22572 22573 case 'l': 22574 blank = !zero; 22575 result += (self.$hour() % 12 || 12); 22576 break; 22577 22578 case 'P': 22579 result += (self.$hour() >= 12 ? "pm" : "am"); 22580 break; 22581 22582 case 'p': 22583 result += (self.$hour() >= 12 ? "PM" : "AM"); 22584 break; 22585 22586 case 'M': 22587 zero = !blank; 22588 result += self.$min(); 22589 break; 22590 22591 case 'S': 22592 zero = !blank; 22593 result += self.$sec() 22594 break; 22595 22596 case 'L': 22597 zero = !blank; 22598 width = isNaN(width) ? 3 : width; 22599 result += self.getMilliseconds(); 22600 break; 22601 22602 case 'N': 22603 width = isNaN(width) ? 9 : width; 22604 result += (self.getMilliseconds().toString()).$rjust(3, "0"); 22605 result = (result).$ljust(width, "0"); 22606 break; 22607 22608 case 'z': 22609 var offset = (self.timezone == null) ? self.getTimezoneOffset() : (-self.timezone * 60), 22610 hours = Math.floor(Math.abs(offset) / 60), 22611 minutes = Math.abs(offset) % 60; 22612 22613 result += offset < 0 ? "+" : "-"; 22614 result += hours < 10 ? "0" : ""; 22615 result += hours; 22616 22617 if (colons > 0) { 22618 result += ":"; 22619 } 22620 22621 result += minutes < 10 ? "0" : ""; 22622 result += minutes; 22623 22624 if (colons > 1) { 22625 result += ":00"; 22626 } 22627 22628 break; 22629 22630 case 'Z': 22631 result += self.$zone(); 22632 break; 22633 22634 case 'A': 22635 result += days_of_week[self.$wday()]; 22636 break; 22637 22638 case 'a': 22639 result += short_days[self.$wday()]; 22640 break; 22641 22642 case 'u': 22643 result += (self.$wday() + 1); 22644 break; 22645 22646 case 'w': 22647 result += self.$wday(); 22648 break; 22649 22650 case 'V': 22651 result += self.$cweek_cyear()['$[]'](0).$to_s().$rjust(2, "0"); 22652 break; 22653 22654 case 'G': 22655 result += self.$cweek_cyear()['$[]'](1); 22656 break; 22657 22658 case 'g': 22659 result += self.$cweek_cyear()['$[]'](1)['$[]']($range(-2, -1, false)); 22660 break; 22661 22662 case 's': 22663 result += self.$to_i(); 22664 break; 22665 22666 case 'n': 22667 result += "\n"; 22668 break; 22669 22670 case 't': 22671 result += "\t"; 22672 break; 22673 22674 case '%': 22675 result += "%"; 22676 break; 22677 22678 case 'c': 22679 result += self.$strftime("%a %b %e %T %Y"); 22680 break; 22681 22682 case 'D': 22683 case 'x': 22684 result += self.$strftime("%m/%d/%y"); 22685 break; 22686 22687 case 'F': 22688 result += self.$strftime("%Y-%m-%d"); 22689 break; 22690 22691 case 'v': 22692 result += self.$strftime("%e-%^b-%4Y"); 22693 break; 22694 22695 case 'r': 22696 result += self.$strftime("%I:%M:%S %p"); 22697 break; 22698 22699 case 'R': 22700 result += self.$strftime("%H:%M"); 22701 break; 22702 22703 case 'T': 22704 case 'X': 22705 result += self.$strftime("%H:%M:%S"); 22706 break; 22707 22708 // Non-standard: JIS X 0301 date format 22709 case 'J': 22710 jd = self.$to_date().$jd(); 22711 if (jd < 2405160) { 22712 result += self.$strftime("%Y-%m-%d"); 22713 break; 22714 } 22715 else if (jd < 2419614) 22716 c = 'M', s = 1867; 22717 else if (jd < 2424875) 22718 c = 'T', s = 1911; 22719 else if (jd < 2447535) 22720 c = 'S', s = 1925; 22721 else if (jd < 2458605) 22722 c = 'H', s = 1988; 22723 else 22724 c = 'R', s = 2018; 22725 22726 result += self.$format("%c%02d", c, $rb_minus(self.$year(), s)); 22727 result += self.$strftime("-%m-%d"); 22728 break; 22729 22730 default: 22731 return full; 22732 } 22733 22734 if (upcase) { 22735 result = result.toUpperCase(); 22736 } 22737 22738 if (invert) { 22739 result = result.replace(/[A-Z]/, function(c) { c.toLowerCase() }). 22740 replace(/[a-z]/, function(c) { c.toUpperCase() }); 22741 } 22742 22743 if (pad && (zero || blank)) { 22744 result = (result).$rjust(isNaN(width) ? 2 : width, blank ? " " : "0"); 22745 } 22746 22747 return result; 22748 }); 22749 22750 }); 22751 22752 $def(self, '$to_a', function $$to_a() { 22753 var self = this; 22754 22755 return [self.$sec(), self.$min(), self.$hour(), self.$day(), self.$month(), self.$year(), self.$wday(), self.$yday(), self.$isdst(), self.$zone()] 22756 }); 22757 22758 $def(self, '$to_f', function $$to_f() { 22759 var self = this; 22760 22761 return self.getTime() / 1000; 22762 }); 22763 22764 $def(self, '$to_i', function $$to_i() { 22765 var self = this; 22766 22767 return parseInt(self.getTime() / 1000, 10); 22768 }); 22769 22770 $def(self, '$cweek_cyear', function $$cweek_cyear() { 22771 var self = this, jan01 = nil, jan01_wday = nil, first_monday = nil, year = nil, offset = nil, week = nil, dec31 = nil, dec31_wday = nil; 22772 22773 22774 jan01 = $$$('Time').$new(self.$year(), 1, 1); 22775 jan01_wday = jan01.$wday(); 22776 first_monday = 0; 22777 year = self.$year(); 22778 if (($truthy($rb_le(jan01_wday, 4)) && ($neqeq(jan01_wday, 0)))) { 22779 offset = $rb_minus(jan01_wday, 1) 22780 } else { 22781 22782 offset = $rb_minus($rb_minus(jan01_wday, 7), 1); 22783 if ($eqeq(offset, -8)) { 22784 offset = -1 22785 }; 22786 }; 22787 week = $rb_divide($rb_plus(self.$yday(), offset), 7.0).$ceil(); 22788 if ($truthy($rb_le(week, 0))) { 22789 return $$$('Time').$new($rb_minus(self.$year(), 1), 12, 31).$cweek_cyear() 22790 } else if ($eqeq(week, 53)) { 22791 22792 dec31 = $$$('Time').$new(self.$year(), 12, 31); 22793 dec31_wday = dec31.$wday(); 22794 if (($truthy($rb_le(dec31_wday, 3)) && ($neqeq(dec31_wday, 0)))) { 22795 22796 week = 1; 22797 year = $rb_plus(year, 1); 22798 }; 22799 }; 22800 return [week, year]; 22801 }); 22802 (function(self, $parent_nesting) { 22803 22804 22805 $alias(self, "mktime", "local"); 22806 return $alias(self, "utc", "gm"); 22807 })(Opal.get_singleton_class(self), $nesting); 22808 $alias(self, "ctime", "asctime"); 22809 $alias(self, "dst?", "isdst"); 22810 $alias(self, "getutc", "getgm"); 22811 $alias(self, "gmtoff", "gmt_offset"); 22812 $alias(self, "mday", "day"); 22813 $alias(self, "month", "mon"); 22814 $alias(self, "to_s", "inspect"); 22815 $alias(self, "tv_sec", "to_i"); 22816 $alias(self, "tv_usec", "usec"); 22817 $alias(self, "utc", "gmtime"); 22818 $alias(self, "utc?", "gmt?"); 22819 return $alias(self, "utc_offset", "gmt_offset"); 22820 })('::', Date, $nesting); 22821}; 22822 22823Opal.modules["corelib/struct"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22824 var $klass = Opal.klass, $slice = Opal.slice, $extract_kwargs = Opal.extract_kwargs, $ensure_kwargs = Opal.ensure_kwargs, $truthy = Opal.truthy, $neqeq = Opal.neqeq, $eqeq = Opal.eqeq, $Opal = Opal.Opal, $send = Opal.send, $Class = Opal.Class, $to_a = Opal.to_a, $def = Opal.def, $defs = Opal.defs, $Kernel = Opal.Kernel, $hash2 = Opal.hash2, $rb_gt = Opal.rb_gt, $rb_minus = Opal.rb_minus, $eqeqeq = Opal.eqeqeq, $rb_lt = Opal.rb_lt, $rb_ge = Opal.rb_ge, $rb_plus = Opal.rb_plus, $alias = Opal.alias, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 22825 22826 Opal.add_stubs('require,include,!=,upcase,[],==,class,unshift,const_name!,map,coerce_to!,new,each,define_struct_attribute,allocate,initialize,alias_method,module_eval,to_proc,const_set,raise,<<,members,define_method,instance_eval,last,>,length,-,keys,any?,join,[]=,each_with_index,hash,===,<,-@,size,>=,include?,to_sym,instance_of?,__id__,eql?,enum_for,+,name,each_pair,inspect,to_h,args,each_with_object,flatten,to_a,respond_to?,dig'); 22827 22828 self.$require("corelib/enumerable"); 22829 return (function($base, $super, $parent_nesting) { 22830 var self = $klass($base, $super, 'Struct'); 22831 22832 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 22833 22834 22835 self.$include($$$('Enumerable')); 22836 $defs(self, '$new', function $Struct_new$1(const_name, $a, $b) { 22837 var block = $Struct_new$1.$$p || nil, $post_args, $kwargs, args, keyword_init, self = this, klass = nil; 22838 22839 $Struct_new$1.$$p = null; 22840 22841 ; 22842 $post_args = $slice(arguments, 1); 22843 $kwargs = $extract_kwargs($post_args); 22844 $kwargs = $ensure_kwargs($kwargs); 22845 args = $post_args; 22846 22847 keyword_init = $kwargs.$$smap["keyword_init"];if (keyword_init == null) keyword_init = false; 22848 if ($truthy(const_name)) { 22849 if (($eqeq(const_name.$class(), $$$('String')) && ($neqeq(const_name['$[]'](0).$upcase(), const_name['$[]'](0))))) { 22850 22851 args.$unshift(const_name); 22852 const_name = nil; 22853 } else { 22854 22855 try { 22856 const_name = $Opal['$const_name!'](const_name) 22857 } catch ($err) { 22858 if (Opal.rescue($err, [$$$('TypeError'), $$$('NameError')])) { 22859 try { 22860 22861 args.$unshift(const_name); 22862 const_name = nil; 22863 } finally { Opal.pop_exception(); } 22864 } else { throw $err; } 22865 }; 22866 } 22867 }; 22868 $send(args, 'map', [], function $$2(arg){ 22869 22870 if (arg == null) arg = nil; 22871 return $Opal['$coerce_to!'](arg, $$$('String'), "to_str");}); 22872 klass = $send($Class, 'new', [self], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 22873 22874 22875 $send(args, 'each', [], function $$4(arg){var self = $$4.$$s == null ? this : $$4.$$s; 22876 22877 22878 if (arg == null) arg = nil; 22879 return self.$define_struct_attribute(arg);}, {$$s: self}); 22880 return (function(self, $parent_nesting) { 22881 22882 22883 22884 $def(self, '$new', function $new$5($a) { 22885 var $post_args, args, self = this, instance = nil; 22886 22887 22888 $post_args = $slice(arguments); 22889 args = $post_args; 22890 instance = self.$allocate(); 22891 instance.$$data = {}; 22892 $send(instance, 'initialize', $to_a(args)); 22893 return instance; 22894 }, -1); 22895 return self.$alias_method("[]", "new"); 22896 })(Opal.get_singleton_class(self), $nesting);}, {$$s: self}); 22897 if ($truthy(block)) { 22898 $send(klass, 'module_eval', [], block.$to_proc()) 22899 }; 22900 klass.$$keyword_init = keyword_init; 22901 if ($truthy(const_name)) { 22902 $$$('Struct').$const_set(const_name, klass) 22903 }; 22904 return klass; 22905 }, -2); 22906 $defs(self, '$define_struct_attribute', function $$define_struct_attribute(name) { 22907 var self = this; 22908 22909 22910 if ($eqeq(self, $$$('Struct'))) { 22911 $Kernel.$raise($$$('ArgumentError'), "you cannot define attributes to the Struct class") 22912 }; 22913 self.$members()['$<<'](name); 22914 $send(self, 'define_method', [name], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s; 22915 22916 return self.$$data[name];}, {$$s: self}); 22917 return $send(self, 'define_method', ["" + (name) + "="], function $$7(value){var self = $$7.$$s == null ? this : $$7.$$s; 22918 22919 22920 if (value == null) value = nil; 22921 return self.$$data[name] = value;;}, {$$s: self}); 22922 }); 22923 $defs(self, '$members', function $$members() { 22924 var self = this, $ret_or_1 = nil; 22925 if (self.members == null) self.members = nil; 22926 22927 22928 if ($eqeq(self, $$$('Struct'))) { 22929 $Kernel.$raise($$$('ArgumentError'), "the Struct class has no members") 22930 }; 22931 return (self.members = ($truthy(($ret_or_1 = self.members)) ? ($ret_or_1) : ([]))); 22932 }); 22933 $defs(self, '$inherited', function $$inherited(klass) { 22934 var self = this, members = nil; 22935 if (self.members == null) self.members = nil; 22936 22937 22938 members = self.members; 22939 return $send(klass, 'instance_eval', [], function $$8(){var self = $$8.$$s == null ? this : $$8.$$s; 22940 22941 return (self.members = members)}, {$$s: self}); 22942 }); 22943 22944 $def(self, '$initialize', function $$initialize($a) { 22945 var $post_args, args, self = this, kwargs = nil, $ret_or_1 = nil, extra = nil; 22946 22947 22948 $post_args = $slice(arguments); 22949 args = $post_args; 22950 if ($truthy(self.$class().$$keyword_init)) { 22951 22952 kwargs = ($truthy(($ret_or_1 = args.$last())) ? ($ret_or_1) : ($hash2([], {}))); 22953 if (($truthy($rb_gt(args.$length(), 1)) || ($truthy((args.length === 1 && !kwargs.$$is_hash))))) { 22954 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (given " + (args.$length()) + ", expected 0)") 22955 }; 22956 extra = $rb_minus(kwargs.$keys(), self.$class().$members()); 22957 if ($truthy(extra['$any?']())) { 22958 $Kernel.$raise($$$('ArgumentError'), "unknown keywords: " + (extra.$join(", "))) 22959 }; 22960 return $send(self.$class().$members(), 'each', [], function $$9(name){var $b, self = $$9.$$s == null ? this : $$9.$$s; 22961 22962 22963 if (name == null) name = nil; 22964 return ($b = [name, kwargs['$[]'](name)], $send(self, '[]=', $b), $b[$b.length - 1]);}, {$$s: self}); 22965 } else { 22966 22967 if ($truthy($rb_gt(args.$length(), self.$class().$members().$length()))) { 22968 $Kernel.$raise($$$('ArgumentError'), "struct size differs") 22969 }; 22970 return $send(self.$class().$members(), 'each_with_index', [], function $$10(name, index){var $b, self = $$10.$$s == null ? this : $$10.$$s; 22971 22972 22973 if (name == null) name = nil; 22974 if (index == null) index = nil; 22975 return ($b = [name, args['$[]'](index)], $send(self, '[]=', $b), $b[$b.length - 1]);}, {$$s: self}); 22976 }; 22977 }, -1); 22978 22979 $def(self, '$initialize_copy', function $$initialize_copy(from) { 22980 var self = this; 22981 22982 22983 self.$$data = {} 22984 var keys = Object.keys(from.$$data), i, max, name; 22985 for (i = 0, max = keys.length; i < max; i++) { 22986 name = keys[i]; 22987 self.$$data[name] = from.$$data[name]; 22988 } 22989 22990 }); 22991 $defs(self, '$keyword_init?', function $Struct_keyword_init$ques$11() { 22992 var self = this; 22993 22994 return self.$$keyword_init; 22995 }); 22996 22997 $def(self, '$members', function $$members() { 22998 var self = this; 22999 23000 return self.$class().$members() 23001 }); 23002 23003 $def(self, '$hash', function $$hash() { 23004 var self = this; 23005 23006 return $$('Hash').$new(self.$$data).$hash() 23007 }); 23008 23009 $def(self, '$[]', function $Struct_$$$12(name) { 23010 var self = this; 23011 23012 23013 if ($eqeqeq($$$('Integer'), name)) { 23014 23015 if ($truthy($rb_lt(name, self.$class().$members().$size()['$-@']()))) { 23016 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too small for struct(size:" + (self.$class().$members().$size()) + ")") 23017 }; 23018 if ($truthy($rb_ge(name, self.$class().$members().$size()))) { 23019 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too large for struct(size:" + (self.$class().$members().$size()) + ")") 23020 }; 23021 name = self.$class().$members()['$[]'](name); 23022 } else if ($eqeqeq($$$('String'), name)) { 23023 23024 if(!self.$$data.hasOwnProperty(name)) { 23025 $Kernel.$raise($$$('NameError').$new("no member '" + (name) + "' in struct", name)) 23026 } 23027 23028 } else { 23029 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + (name.$class()) + " into Integer") 23030 }; 23031 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 23032 return self.$$data[name];; 23033 }); 23034 23035 $def(self, '$[]=', function $Struct_$$$eq$13(name, value) { 23036 var self = this; 23037 23038 23039 if ($eqeqeq($$$('Integer'), name)) { 23040 23041 if ($truthy($rb_lt(name, self.$class().$members().$size()['$-@']()))) { 23042 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too small for struct(size:" + (self.$class().$members().$size()) + ")") 23043 }; 23044 if ($truthy($rb_ge(name, self.$class().$members().$size()))) { 23045 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too large for struct(size:" + (self.$class().$members().$size()) + ")") 23046 }; 23047 name = self.$class().$members()['$[]'](name); 23048 } else if ($eqeqeq($$$('String'), name)) { 23049 if (!$truthy(self.$class().$members()['$include?'](name.$to_sym()))) { 23050 $Kernel.$raise($$$('NameError').$new("no member '" + (name) + "' in struct", name)) 23051 } 23052 } else { 23053 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + (name.$class()) + " into Integer") 23054 }; 23055 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 23056 return self.$$data[name] = value;; 23057 }); 23058 23059 $def(self, '$==', function $Struct_$eq_eq$14(other) { 23060 var self = this; 23061 23062 23063 if (!$truthy(other['$instance_of?'](self.$class()))) { 23064 return false 23065 }; 23066 23067 var recursed1 = {}, recursed2 = {}; 23068 23069 function _eqeq(struct, other) { 23070 var key, a, b; 23071 23072 recursed1[(struct).$__id__()] = true; 23073 recursed2[(other).$__id__()] = true; 23074 23075 for (key in struct.$$data) { 23076 a = struct.$$data[key]; 23077 b = other.$$data[key]; 23078 23079 if ($$$('Struct')['$==='](a)) { 23080 if (!recursed1.hasOwnProperty((a).$__id__()) || !recursed2.hasOwnProperty((b).$__id__())) { 23081 if (!_eqeq(a, b)) { 23082 return false; 23083 } 23084 } 23085 } else { 23086 if (!(a)['$=='](b)) { 23087 return false; 23088 } 23089 } 23090 } 23091 23092 return true; 23093 } 23094 23095 return _eqeq(self, other); 23096 ; 23097 }); 23098 23099 $def(self, '$eql?', function $Struct_eql$ques$15(other) { 23100 var self = this; 23101 23102 23103 if (!$truthy(other['$instance_of?'](self.$class()))) { 23104 return false 23105 }; 23106 23107 var recursed1 = {}, recursed2 = {}; 23108 23109 function _eqeq(struct, other) { 23110 var key, a, b; 23111 23112 recursed1[(struct).$__id__()] = true; 23113 recursed2[(other).$__id__()] = true; 23114 23115 for (key in struct.$$data) { 23116 a = struct.$$data[key]; 23117 b = other.$$data[key]; 23118 23119 if ($$$('Struct')['$==='](a)) { 23120 if (!recursed1.hasOwnProperty((a).$__id__()) || !recursed2.hasOwnProperty((b).$__id__())) { 23121 if (!_eqeq(a, b)) { 23122 return false; 23123 } 23124 } 23125 } else { 23126 if (!(a)['$eql?'](b)) { 23127 return false; 23128 } 23129 } 23130 } 23131 23132 return true; 23133 } 23134 23135 return _eqeq(self, other); 23136 ; 23137 }); 23138 23139 $def(self, '$each', function $$each() { 23140 var $yield = $$each.$$p || nil, self = this; 23141 23142 $$each.$$p = null; 23143 23144 if (!($yield !== nil)) { 23145 return $send(self, 'enum_for', ["each"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 23146 23147 return self.$size()}, {$$s: self}) 23148 }; 23149 $send(self.$class().$members(), 'each', [], function $$17(name){var self = $$17.$$s == null ? this : $$17.$$s; 23150 23151 23152 if (name == null) name = nil; 23153 return Opal.yield1($yield, self['$[]'](name));;}, {$$s: self}); 23154 return self; 23155 }); 23156 23157 $def(self, '$each_pair', function $$each_pair() { 23158 var $yield = $$each_pair.$$p || nil, self = this; 23159 23160 $$each_pair.$$p = null; 23161 23162 if (!($yield !== nil)) { 23163 return $send(self, 'enum_for', ["each_pair"], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 23164 23165 return self.$size()}, {$$s: self}) 23166 }; 23167 $send(self.$class().$members(), 'each', [], function $$19(name){var self = $$19.$$s == null ? this : $$19.$$s; 23168 23169 23170 if (name == null) name = nil; 23171 return Opal.yield1($yield, [name, self['$[]'](name)]);;}, {$$s: self}); 23172 return self; 23173 }); 23174 23175 $def(self, '$length', function $$length() { 23176 var self = this; 23177 23178 return self.$class().$members().$length() 23179 }); 23180 23181 $def(self, '$to_a', function $$to_a() { 23182 var self = this; 23183 23184 return $send(self.$class().$members(), 'map', [], function $$20(name){var self = $$20.$$s == null ? this : $$20.$$s; 23185 23186 23187 if (name == null) name = nil; 23188 return self['$[]'](name);}, {$$s: self}) 23189 }); 23190 var inspect_stack = []; 23191 23192 $def(self, '$inspect', function $$inspect() { 23193 var self = this, result = nil, pushed = nil; 23194 23195 return (function() { try { 23196 23197 result = "#<struct "; 23198 if ($truthy((inspect_stack)['$include?'](self.$__id__()))) { 23199 return $rb_plus(result, ":...>") 23200 } else { 23201 23202 (inspect_stack)['$<<'](self.$__id__()); 23203 pushed = true; 23204 if (($eqeqeq($$$('Struct'), self) && ($truthy(self.$class().$name())))) { 23205 result = $rb_plus(result, "" + (self.$class()) + " ") 23206 }; 23207 result = $rb_plus(result, $send(self.$each_pair(), 'map', [], function $$21(name, value){ 23208 23209 if (name == null) name = nil; 23210 if (value == null) value = nil; 23211 return "" + (name) + "=" + ($$('Opal').$inspect(value));}).$join(", ")); 23212 result = $rb_plus(result, ">"); 23213 return result; 23214 }; 23215 } finally { 23216 ($truthy(pushed) ? (inspect_stack.pop()) : nil) 23217 }; })() 23218 }); 23219 23220 $def(self, '$to_h', function $$to_h() { 23221 var block = $$to_h.$$p || nil, self = this; 23222 23223 $$to_h.$$p = null; 23224 23225 ; 23226 if ((block !== nil)) { 23227 return $send($send(self, 'map', [], block.$to_proc()), 'to_h', $to_a(self.$args())) 23228 }; 23229 return $send(self.$class().$members(), 'each_with_object', [$hash2([], {})], function $$22(name, h){var $a, self = $$22.$$s == null ? this : $$22.$$s; 23230 23231 23232 if (name == null) name = nil; 23233 if (h == null) h = nil; 23234 return ($a = [name, self['$[]'](name)], $send(h, '[]=', $a), $a[$a.length - 1]);}, {$$s: self}); 23235 }); 23236 23237 $def(self, '$values_at', function $$values_at($a) { 23238 var $post_args, args, self = this; 23239 23240 23241 $post_args = $slice(arguments); 23242 args = $post_args; 23243 args = $send(args, 'map', [], function $$23(arg){ 23244 23245 if (arg == null) arg = nil; 23246 return arg.$$is_range ? arg.$to_a() : arg;}).$flatten(); 23247 23248 var result = []; 23249 for (var i = 0, len = args.length; i < len; i++) { 23250 if (!args[i].$$is_number) { 23251 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + ((args[i]).$class()) + " into Integer") 23252 } 23253 result.push(self['$[]'](args[i])); 23254 } 23255 return result; 23256 ; 23257 }, -1); 23258 23259 $def(self, '$dig', function $$dig(key, $a) { 23260 var $post_args, keys, self = this, item = nil; 23261 23262 23263 $post_args = $slice(arguments, 1); 23264 keys = $post_args; 23265 item = ($truthy(key.$$is_string && self.$$data.hasOwnProperty(key)) ? (self.$$data[key] || nil) : nil); 23266 23267 if (item === nil || keys.length === 0) { 23268 return item; 23269 } 23270 ; 23271 if (!$truthy(item['$respond_to?']("dig"))) { 23272 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method") 23273 }; 23274 return $send(item, 'dig', $to_a(keys)); 23275 }, -2); 23276 $alias(self, "size", "length"); 23277 $alias(self, "to_s", "inspect"); 23278 return $alias(self, "values", "to_a"); 23279 })('::', null, $nesting); 23280}; 23281 23282Opal.modules["corelib/set"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23283 var $freeze = Opal.freeze, $klass = Opal.klass, $slice = Opal.slice, $defs = Opal.defs, $hash2 = Opal.hash2, $truthy = Opal.truthy, $eqeqeq = Opal.eqeqeq, $Kernel = Opal.Kernel, $send = Opal.send, $def = Opal.def, $eqeq = Opal.eqeq, $rb_lt = Opal.rb_lt, $rb_le = Opal.rb_le, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 23284 23285 Opal.add_stubs('include,new,nil?,===,raise,each,add,merge,class,respond_to?,subtract,dup,join,to_a,equal?,instance_of?,==,instance_variable_get,size,is_a?,all?,include?,[]=,enum_for,[],<<,replace,compare_by_identity,name,compare_by_identity?,delete,select,frozen?,freeze,reject,delete_if,to_proc,keep_if,each_key,empty?,eql?,instance_eval,clear,<,<=,any?,!,intersect?,keys,|,proper_subset?,subset?,proper_superset?,superset?,-,select!,collect!'); 23286 return (function($base, $super, $parent_nesting) { 23287 var self = $klass($base, $super, 'Set'); 23288 23289 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $ret_or_1 = nil, $proto = self.$$prototype; 23290 23291 $proto.hash = nil; 23292 23293 self.$include($$$('Enumerable')); 23294 $defs(self, '$[]', function $Set_$$$1($a) { 23295 var $post_args, ary, self = this; 23296 23297 23298 $post_args = $slice(arguments); 23299 ary = $post_args; 23300 return self.$new(ary); 23301 }, -1); 23302 23303 $def(self, '$initialize', function $$initialize(enum$) { 23304 var block = $$initialize.$$p || nil, self = this; 23305 23306 $$initialize.$$p = null; 23307 23308 ; 23309 if (enum$ == null) enum$ = nil; 23310 self.hash = $hash2([], {}); 23311 if ($truthy(enum$['$nil?']())) { 23312 return nil 23313 }; 23314 if (!$eqeqeq($$$('Enumerable'), enum$)) { 23315 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable") 23316 }; 23317 if ($truthy(block)) { 23318 return $send(enum$, 'each', [], function $$2(item){var self = $$2.$$s == null ? this : $$2.$$s; 23319 23320 23321 if (item == null) item = nil; 23322 return self.$add(Opal.yield1(block, item));}, {$$s: self}) 23323 } else { 23324 return self.$merge(enum$) 23325 }; 23326 }, -1); 23327 23328 $def(self, '$dup', function $$dup() { 23329 var self = this, result = nil; 23330 23331 23332 result = self.$class().$new(); 23333 return result.$merge(self); 23334 }); 23335 23336 $def(self, '$-', function $Set_$minus$3(enum$) { 23337 var self = this; 23338 23339 23340 if (!$truthy(enum$['$respond_to?']("each"))) { 23341 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable") 23342 }; 23343 return self.$dup().$subtract(enum$); 23344 }); 23345 23346 $def(self, '$inspect', function $$inspect() { 23347 var self = this; 23348 23349 return "#<Set: {" + (self.$to_a().$join(",")) + "}>" 23350 }); 23351 23352 $def(self, '$==', function $Set_$eq_eq$4(other) { 23353 var self = this; 23354 23355 if ($truthy(self['$equal?'](other))) { 23356 return true 23357 } else if ($truthy(other['$instance_of?'](self.$class()))) { 23358 return self.hash['$=='](other.$instance_variable_get("@hash")) 23359 } else if (($truthy(other['$is_a?']($$$('Set'))) && ($eqeq(self.$size(), other.$size())))) { 23360 return $send(other, 'all?', [], function $$5(o){var self = $$5.$$s == null ? this : $$5.$$s; 23361 if (self.hash == null) self.hash = nil; 23362 23363 23364 if (o == null) o = nil; 23365 return self.hash['$include?'](o);}, {$$s: self}) 23366 } else { 23367 return false 23368 } 23369 }); 23370 23371 $def(self, '$add', function $$add(o) { 23372 var self = this; 23373 23374 23375 self.hash['$[]='](o, true); 23376 return self; 23377 }); 23378 23379 $def(self, '$classify', function $$classify() { 23380 var block = $$classify.$$p || nil, self = this, result = nil; 23381 23382 $$classify.$$p = null; 23383 23384 ; 23385 if (!(block !== nil)) { 23386 return self.$enum_for("classify") 23387 }; 23388 result = $send($$$('Hash'), 'new', [], function $$6(h, k){var $a, self = $$6.$$s == null ? this : $$6.$$s; 23389 23390 23391 if (h == null) h = nil; 23392 if (k == null) k = nil; 23393 return ($a = [k, self.$class().$new()], $send(h, '[]=', $a), $a[$a.length - 1]);}, {$$s: self}); 23394 $send(self, 'each', [], function $$7(item){ 23395 23396 if (item == null) item = nil; 23397 return result['$[]'](Opal.yield1(block, item)).$add(item);}); 23398 return result; 23399 }); 23400 23401 $def(self, '$collect!', function $Set_collect$excl$8() { 23402 var block = $Set_collect$excl$8.$$p || nil, self = this, result = nil; 23403 23404 $Set_collect$excl$8.$$p = null; 23405 23406 ; 23407 if (!(block !== nil)) { 23408 return self.$enum_for("collect!") 23409 }; 23410 result = self.$class().$new(); 23411 $send(self, 'each', [], function $$9(item){ 23412 23413 if (item == null) item = nil; 23414 return result['$<<'](Opal.yield1(block, item));}); 23415 return self.$replace(result); 23416 }); 23417 23418 $def(self, '$compare_by_identity', function $$compare_by_identity() { 23419 var self = this; 23420 23421 if ($truthy(self.hash['$respond_to?']("compare_by_identity"))) { 23422 23423 self.hash.$compare_by_identity(); 23424 return self; 23425 } else { 23426 return self.$raise($$('NotImplementedError'), "" + (self.$class().$name()) + "#" + ("compare_by_identity") + " is not implemented") 23427 } 23428 }); 23429 23430 $def(self, '$compare_by_identity?', function $Set_compare_by_identity$ques$10() { 23431 var self = this, $ret_or_1 = nil; 23432 23433 if ($truthy(($ret_or_1 = self.hash['$respond_to?']("compare_by_identity?")))) { 23434 return self.hash['$compare_by_identity?']() 23435 } else { 23436 return $ret_or_1 23437 } 23438 }); 23439 23440 $def(self, '$delete', function $Set_delete$11(o) { 23441 var self = this; 23442 23443 23444 self.hash.$delete(o); 23445 return self; 23446 }); 23447 23448 $def(self, '$delete?', function $Set_delete$ques$12(o) { 23449 var self = this; 23450 23451 if ($truthy(self['$include?'](o))) { 23452 23453 self.$delete(o); 23454 return self; 23455 } else { 23456 return nil 23457 } 23458 }); 23459 23460 $def(self, '$delete_if', function $$delete_if() { 23461 var $yield = $$delete_if.$$p || nil, self = this; 23462 23463 $$delete_if.$$p = null; 23464 23465 if (!($yield !== nil)) { 23466 return self.$enum_for("delete_if") 23467 }; 23468 $send($send(self, 'select', [], function $$13(o){ 23469 23470 if (o == null) o = nil; 23471 return Opal.yield1($yield, o);;}), 'each', [], function $$14(o){var self = $$14.$$s == null ? this : $$14.$$s; 23472 if (self.hash == null) self.hash = nil; 23473 23474 23475 if (o == null) o = nil; 23476 return self.hash.$delete(o);}, {$$s: self}); 23477 return self; 23478 }); 23479 23480 $def(self, '$freeze', function $$freeze() { 23481 var self = this; 23482 23483 23484 if ($truthy(self['$frozen?']())) { 23485 return self 23486 }; 23487 self.hash.$freeze(); 23488 return $freeze(self);; 23489 }); 23490 23491 $def(self, '$keep_if', function $$keep_if() { 23492 var $yield = $$keep_if.$$p || nil, self = this; 23493 23494 $$keep_if.$$p = null; 23495 23496 if (!($yield !== nil)) { 23497 return self.$enum_for("keep_if") 23498 }; 23499 $send($send(self, 'reject', [], function $$15(o){ 23500 23501 if (o == null) o = nil; 23502 return Opal.yield1($yield, o);;}), 'each', [], function $$16(o){var self = $$16.$$s == null ? this : $$16.$$s; 23503 if (self.hash == null) self.hash = nil; 23504 23505 23506 if (o == null) o = nil; 23507 return self.hash.$delete(o);}, {$$s: self}); 23508 return self; 23509 }); 23510 23511 $def(self, '$reject!', function $Set_reject$excl$17() { 23512 var block = $Set_reject$excl$17.$$p || nil, self = this, before = nil; 23513 23514 $Set_reject$excl$17.$$p = null; 23515 23516 ; 23517 if (!(block !== nil)) { 23518 return self.$enum_for("reject!") 23519 }; 23520 before = self.$size(); 23521 $send(self, 'delete_if', [], block.$to_proc()); 23522 if ($eqeq(self.$size(), before)) { 23523 return nil 23524 } else { 23525 return self 23526 }; 23527 }); 23528 23529 $def(self, '$select!', function $Set_select$excl$18() { 23530 var block = $Set_select$excl$18.$$p || nil, self = this, before = nil; 23531 23532 $Set_select$excl$18.$$p = null; 23533 23534 ; 23535 if (!(block !== nil)) { 23536 return self.$enum_for("select!") 23537 }; 23538 before = self.$size(); 23539 $send(self, 'keep_if', [], block.$to_proc()); 23540 if ($eqeq(self.$size(), before)) { 23541 return nil 23542 } else { 23543 return self 23544 }; 23545 }); 23546 23547 $def(self, '$add?', function $Set_add$ques$19(o) { 23548 var self = this; 23549 23550 if ($truthy(self['$include?'](o))) { 23551 return nil 23552 } else { 23553 return self.$add(o) 23554 } 23555 }); 23556 23557 $def(self, '$each', function $$each() { 23558 var block = $$each.$$p || nil, self = this; 23559 23560 $$each.$$p = null; 23561 23562 ; 23563 if (!(block !== nil)) { 23564 return self.$enum_for("each") 23565 }; 23566 $send(self.hash, 'each_key', [], block.$to_proc()); 23567 return self; 23568 }); 23569 23570 $def(self, '$empty?', function $Set_empty$ques$20() { 23571 var self = this; 23572 23573 return self.hash['$empty?']() 23574 }); 23575 23576 $def(self, '$eql?', function $Set_eql$ques$21(other) { 23577 var self = this; 23578 23579 return self.hash['$eql?']($send(other, 'instance_eval', [], function $$22(){var self = $$22.$$s == null ? this : $$22.$$s; 23580 if (self.hash == null) self.hash = nil; 23581 23582 return self.hash}, {$$s: self})) 23583 }); 23584 23585 $def(self, '$clear', function $$clear() { 23586 var self = this; 23587 23588 23589 self.hash.$clear(); 23590 return self; 23591 }); 23592 23593 $def(self, '$include?', function $Set_include$ques$23(o) { 23594 var self = this; 23595 23596 return self.hash['$include?'](o) 23597 }); 23598 23599 $def(self, '$merge', function $$merge(enum$) { 23600 var self = this; 23601 23602 23603 $send(enum$, 'each', [], function $$24(item){var self = $$24.$$s == null ? this : $$24.$$s; 23604 23605 23606 if (item == null) item = nil; 23607 return self.$add(item);}, {$$s: self}); 23608 return self; 23609 }); 23610 23611 $def(self, '$replace', function $$replace(enum$) { 23612 var self = this; 23613 23614 23615 self.$clear(); 23616 self.$merge(enum$); 23617 return self; 23618 }); 23619 23620 $def(self, '$size', function $$size() { 23621 var self = this; 23622 23623 return self.hash.$size() 23624 }); 23625 23626 $def(self, '$subtract', function $$subtract(enum$) { 23627 var self = this; 23628 23629 23630 $send(enum$, 'each', [], function $$25(item){var self = $$25.$$s == null ? this : $$25.$$s; 23631 23632 23633 if (item == null) item = nil; 23634 return self.$delete(item);}, {$$s: self}); 23635 return self; 23636 }); 23637 23638 $def(self, '$|', function $Set_$$26(enum$) { 23639 var self = this; 23640 23641 23642 if (!$truthy(enum$['$respond_to?']("each"))) { 23643 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable") 23644 }; 23645 return self.$dup().$merge(enum$); 23646 }); 23647 23648 function is_set(set) { 23649 ($truthy(($ret_or_1 = (set)['$is_a?']($$$('Set')))) ? ($ret_or_1) : ($Kernel.$raise($$$('ArgumentError'), "value must be a set"))) 23650 } 23651 ; 23652 23653 $def(self, '$superset?', function $Set_superset$ques$27(set) { 23654 var self = this; 23655 23656 23657 is_set(set); 23658 if ($truthy($rb_lt(self.$size(), set.$size()))) { 23659 return false 23660 }; 23661 return $send(set, 'all?', [], function $$28(o){var self = $$28.$$s == null ? this : $$28.$$s; 23662 23663 23664 if (o == null) o = nil; 23665 return self['$include?'](o);}, {$$s: self}); 23666 }); 23667 23668 $def(self, '$proper_superset?', function $Set_proper_superset$ques$29(set) { 23669 var self = this; 23670 23671 23672 is_set(set); 23673 if ($truthy($rb_le(self.$size(), set.$size()))) { 23674 return false 23675 }; 23676 return $send(set, 'all?', [], function $$30(o){var self = $$30.$$s == null ? this : $$30.$$s; 23677 23678 23679 if (o == null) o = nil; 23680 return self['$include?'](o);}, {$$s: self}); 23681 }); 23682 23683 $def(self, '$subset?', function $Set_subset$ques$31(set) { 23684 var self = this; 23685 23686 23687 is_set(set); 23688 if ($truthy($rb_lt(set.$size(), self.$size()))) { 23689 return false 23690 }; 23691 return $send(self, 'all?', [], function $$32(o){ 23692 23693 if (o == null) o = nil; 23694 return set['$include?'](o);}); 23695 }); 23696 23697 $def(self, '$proper_subset?', function $Set_proper_subset$ques$33(set) { 23698 var self = this; 23699 23700 23701 is_set(set); 23702 if ($truthy($rb_le(set.$size(), self.$size()))) { 23703 return false 23704 }; 23705 return $send(self, 'all?', [], function $$34(o){ 23706 23707 if (o == null) o = nil; 23708 return set['$include?'](o);}); 23709 }); 23710 23711 $def(self, '$intersect?', function $Set_intersect$ques$35(set) { 23712 var self = this; 23713 23714 23715 is_set(set); 23716 if ($truthy($rb_lt(self.$size(), set.$size()))) { 23717 return $send(self, 'any?', [], function $$36(o){ 23718 23719 if (o == null) o = nil; 23720 return set['$include?'](o);}) 23721 } else { 23722 return $send(set, 'any?', [], function $$37(o){var self = $$37.$$s == null ? this : $$37.$$s; 23723 23724 23725 if (o == null) o = nil; 23726 return self['$include?'](o);}, {$$s: self}) 23727 }; 23728 }); 23729 23730 $def(self, '$disjoint?', function $Set_disjoint$ques$38(set) { 23731 var self = this; 23732 23733 return self['$intersect?'](set)['$!']() 23734 }); 23735 23736 $def(self, '$to_a', function $$to_a() { 23737 var self = this; 23738 23739 return self.hash.$keys() 23740 }); 23741 $alias(self, "+", "|"); 23742 $alias(self, "<", "proper_subset?"); 23743 $alias(self, "<<", "add"); 23744 $alias(self, "<=", "subset?"); 23745 $alias(self, ">", "proper_superset?"); 23746 $alias(self, ">=", "superset?"); 23747 $alias(self, "difference", "-"); 23748 $alias(self, "filter!", "select!"); 23749 $alias(self, "length", "size"); 23750 $alias(self, "map!", "collect!"); 23751 $alias(self, "member?", "include?"); 23752 return $alias(self, "union", "|"); 23753 })('::', null, $nesting) 23754}; 23755 23756Opal.modules["corelib/dir"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23757 var $klass = Opal.klass, $def = Opal.def, $truthy = Opal.truthy, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 23758 23759 Opal.add_stubs('[],pwd'); 23760 return (function($base, $super, $parent_nesting) { 23761 var self = $klass($base, $super, 'Dir'); 23762 23763 var $nesting = [self].concat($parent_nesting); 23764 23765 return (function(self, $parent_nesting) { 23766 23767 23768 23769 $def(self, '$chdir', function $$chdir(dir) { 23770 var $yield = $$chdir.$$p || nil, prev_cwd = nil; 23771 23772 $$chdir.$$p = null; 23773 return (function() { try { 23774 23775 prev_cwd = Opal.current_dir; 23776 Opal.current_dir = dir; 23777 return Opal.yieldX($yield, []);; 23778 } finally { 23779 Opal.current_dir = prev_cwd 23780 }; })() 23781 }); 23782 23783 $def(self, '$pwd', function $$pwd() { 23784 23785 return Opal.current_dir || '.'; 23786 }); 23787 23788 $def(self, '$home', function $$home() { 23789 var $ret_or_1 = nil; 23790 23791 if ($truthy(($ret_or_1 = $$$('ENV')['$[]']("HOME")))) { 23792 return $ret_or_1 23793 } else { 23794 return "." 23795 } 23796 }); 23797 return $alias(self, "getwd", "pwd"); 23798 })(Opal.get_singleton_class(self), $nesting) 23799 })('::', null, $nesting) 23800}; 23801 23802Opal.modules["corelib/file"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23803 var $truthy = Opal.truthy, $klass = Opal.klass, $const_set = Opal.const_set, $Opal = Opal.Opal, $regexp = Opal.regexp, $rb_plus = Opal.rb_plus, $def = Opal.def, $Kernel = Opal.Kernel, $eqeq = Opal.eqeq, $rb_lt = Opal.rb_lt, $rb_minus = Opal.rb_minus, $range = Opal.range, $send = Opal.send, $slice = Opal.slice, $alias = Opal.alias, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 23804 23805 Opal.add_stubs('respond_to?,to_path,coerce_to!,pwd,split,sub,+,unshift,join,home,raise,start_with?,absolute_path,==,<,dirname,-,basename,empty?,rindex,[],length,nil?,gsub,find,=~,map,each_with_index,flatten,reject,to_proc,end_with?,expand_path,exist?'); 23806 return (function($base, $super, $parent_nesting) { 23807 var self = $klass($base, $super, 'File'); 23808 23809 var $nesting = [self].concat($parent_nesting), windows_root_rx = nil; 23810 23811 23812 $const_set($nesting[0], 'Separator', $const_set($nesting[0], 'SEPARATOR', "/")); 23813 $const_set($nesting[0], 'ALT_SEPARATOR', nil); 23814 $const_set($nesting[0], 'PATH_SEPARATOR', ":"); 23815 $const_set($nesting[0], 'FNM_SYSCASE', 0); 23816 windows_root_rx = /^[a-zA-Z]:(?:\\|\/)/; 23817 return (function(self, $parent_nesting) { 23818 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 23819 23820 23821 23822 $def(self, '$absolute_path', function $$absolute_path(path, basedir) { 23823 var sep = nil, sep_chars = nil, new_parts = nil, $ret_or_1 = nil, path_abs = nil, basedir_abs = nil, parts = nil, leading_sep = nil, abs = nil, new_path = nil; 23824 23825 23826 if (basedir == null) basedir = nil; 23827 sep = $$('SEPARATOR'); 23828 sep_chars = $sep_chars(); 23829 new_parts = []; 23830 path = ($truthy(path['$respond_to?']("to_path")) ? (path.$to_path()) : (path)); 23831 path = $Opal['$coerce_to!'](path, $$$('String'), "to_str"); 23832 basedir = ($truthy(($ret_or_1 = basedir)) ? ($ret_or_1) : ($$$('Dir').$pwd())); 23833 path_abs = path.substr(0, sep.length) === sep || windows_root_rx.test(path); 23834 basedir_abs = basedir.substr(0, sep.length) === sep || windows_root_rx.test(basedir); 23835 if ($truthy(path_abs)) { 23836 23837 parts = path.$split($regexp(["[", sep_chars, "]"])); 23838 leading_sep = windows_root_rx.test(path) ? '' : path.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 23839 abs = true; 23840 } else { 23841 23842 parts = $rb_plus(basedir.$split($regexp(["[", sep_chars, "]"])), path.$split($regexp(["[", sep_chars, "]"]))); 23843 leading_sep = windows_root_rx.test(basedir) ? '' : basedir.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 23844 abs = basedir_abs; 23845 }; 23846 23847 var part; 23848 for (var i = 0, ii = parts.length; i < ii; i++) { 23849 part = parts[i]; 23850 23851 if ( 23852 (part === nil) || 23853 (part === '' && ((new_parts.length === 0) || abs)) || 23854 (part === '.' && ((new_parts.length === 0) || abs)) 23855 ) { 23856 continue; 23857 } 23858 if (part === '..') { 23859 new_parts.pop(); 23860 } else { 23861 new_parts.push(part); 23862 } 23863 } 23864 23865 if (!abs && parts[0] !== '.') { 23866 new_parts.$unshift(".") 23867 } 23868 ; 23869 new_path = new_parts.$join(sep); 23870 if ($truthy(abs)) { 23871 new_path = $rb_plus(leading_sep, new_path) 23872 }; 23873 return new_path; 23874 }, -2); 23875 23876 $def(self, '$expand_path', function $$expand_path(path, basedir) { 23877 var self = this, sep = nil, sep_chars = nil, home = nil, leading_sep = nil, home_path_regexp = nil; 23878 23879 23880 if (basedir == null) basedir = nil; 23881 sep = $$('SEPARATOR'); 23882 sep_chars = $sep_chars(); 23883 if ($truthy(path[0] === '~' || (basedir && basedir[0] === '~'))) { 23884 23885 home = $$('Dir').$home(); 23886 if (!$truthy(home)) { 23887 $Kernel.$raise($$$('ArgumentError'), "couldn't find HOME environment -- expanding `~'") 23888 }; 23889 leading_sep = windows_root_rx.test(home) ? '' : home.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 23890 if (!$truthy(home['$start_with?'](leading_sep))) { 23891 $Kernel.$raise($$$('ArgumentError'), "non-absolute home") 23892 }; 23893 home = $rb_plus(home, sep); 23894 home_path_regexp = $regexp(["^\\~(?:", sep, "|$)"]); 23895 path = path.$sub(home_path_regexp, home); 23896 if ($truthy(basedir)) { 23897 basedir = basedir.$sub(home_path_regexp, home) 23898 }; 23899 }; 23900 return self.$absolute_path(path, basedir); 23901 }, -2); 23902 23903 // Coerce a given path to a path string using #to_path and #to_str 23904 function $coerce_to_path(path) { 23905 if ($truthy((path)['$respond_to?']("to_path"))) { 23906 path = path.$to_path(); 23907 } 23908 23909 path = $Opal['$coerce_to!'](path, $$$('String'), "to_str"); 23910 23911 return path; 23912 } 23913 23914 // Return a RegExp compatible char class 23915 function $sep_chars() { 23916 if ($$('ALT_SEPARATOR') === nil) { 23917 return Opal.escape_regexp($$('SEPARATOR')); 23918 } else { 23919 return Opal.escape_regexp($rb_plus($$('SEPARATOR'), $$('ALT_SEPARATOR'))); 23920 } 23921 } 23922 ; 23923 23924 $def(self, '$dirname', function $$dirname(path, level) { 23925 var self = this, sep_chars = nil; 23926 23927 23928 if (level == null) level = 1; 23929 if ($eqeq(level, 0)) { 23930 return path 23931 }; 23932 if ($truthy($rb_lt(level, 0))) { 23933 $Kernel.$raise($$$('ArgumentError'), "level can't be negative") 23934 }; 23935 sep_chars = $sep_chars(); 23936 path = $coerce_to_path(path); 23937 23938 var absolute = path.match(new RegExp("^[" + (sep_chars) + "]")), out; 23939 23940 path = path.replace(new RegExp("[" + (sep_chars) + "]+$"), ''); // remove trailing separators 23941 path = path.replace(new RegExp("[^" + (sep_chars) + "]+$"), ''); // remove trailing basename 23942 path = path.replace(new RegExp("[" + (sep_chars) + "]+$"), ''); // remove final trailing separators 23943 23944 if (path === '') { 23945 out = absolute ? '/' : '.'; 23946 } 23947 else { 23948 out = path; 23949 } 23950 23951 if (level == 1) { 23952 return out; 23953 } 23954 else { 23955 return self.$dirname(out, $rb_minus(level, 1)) 23956 } 23957 ; 23958 }, -2); 23959 23960 $def(self, '$basename', function $$basename(name, suffix) { 23961 var sep_chars = nil; 23962 23963 23964 if (suffix == null) suffix = nil; 23965 sep_chars = $sep_chars(); 23966 name = $coerce_to_path(name); 23967 23968 if (name.length == 0) { 23969 return name; 23970 } 23971 23972 if (suffix !== nil) { 23973 suffix = $Opal['$coerce_to!'](suffix, $$$('String'), "to_str") 23974 } else { 23975 suffix = null; 23976 } 23977 23978 name = name.replace(new RegExp("(.)[" + (sep_chars) + "]*$"), '$1'); 23979 name = name.replace(new RegExp("^(?:.*[" + (sep_chars) + "])?([^" + (sep_chars) + "]+)$"), '$1'); 23980 23981 if (suffix === ".*") { 23982 name = name.replace(/\.[^\.]+$/, ''); 23983 } else if(suffix !== null) { 23984 suffix = Opal.escape_regexp(suffix); 23985 name = name.replace(new RegExp("" + (suffix) + "$"), ''); 23986 } 23987 23988 return name; 23989 ; 23990 }, -2); 23991 23992 $def(self, '$extname', function $$extname(path) { 23993 var self = this, filename = nil, last_dot_idx = nil; 23994 23995 23996 path = $coerce_to_path(path); 23997 filename = self.$basename(path); 23998 if ($truthy(filename['$empty?']())) { 23999 return "" 24000 }; 24001 last_dot_idx = filename['$[]']($range(1, -1, false)).$rindex("."); 24002 if (($truthy(last_dot_idx['$nil?']()) || ($eqeq($rb_plus(last_dot_idx, 1), $rb_minus(filename.$length(), 1))))) { 24003 return "" 24004 } else { 24005 return filename['$[]'](Opal.Range.$new($rb_plus(last_dot_idx, 1), -1, false)) 24006 }; 24007 }); 24008 24009 $def(self, '$exist?', function $exist$ques$1(path) { 24010 24011 return Opal.modules[path] != null 24012 }); 24013 24014 $def(self, '$directory?', function $directory$ques$2(path) { 24015 var files = nil; 24016 24017 24018 files = []; 24019 24020 for (var key in Opal.modules) { 24021 files.push(key) 24022 } 24023 ; 24024 path = path.$gsub($regexp(["(^.", $$('SEPARATOR'), "+|", $$('SEPARATOR'), "+$)"])); 24025 return $send(files, 'find', [], function $$3(f){ 24026 24027 if (f == null) f = nil; 24028 return f['$=~']($regexp(["^", path]));}); 24029 }); 24030 24031 $def(self, '$join', function $$join($a) { 24032 var $post_args, paths, result = nil; 24033 24034 24035 $post_args = $slice(arguments); 24036 paths = $post_args; 24037 if ($truthy(paths['$empty?']())) { 24038 return "" 24039 }; 24040 result = ""; 24041 paths = $send(paths.$flatten().$each_with_index(), 'map', [], function $$4(item, index){ 24042 24043 if (item == null) item = nil; 24044 if (index == null) index = nil; 24045 if (($eqeq(index, 0) && ($truthy(item['$empty?']())))) { 24046 return $$('SEPARATOR') 24047 } else if (($eqeq(paths.$length(), $rb_plus(index, 1)) && ($truthy(item['$empty?']())))) { 24048 return $$('SEPARATOR') 24049 } else { 24050 return item 24051 };}); 24052 paths = $send(paths, 'reject', [], "empty?".$to_proc()); 24053 $send(paths, 'each_with_index', [], function $$5(item, index){var next_item = nil; 24054 24055 24056 if (item == null) item = nil; 24057 if (index == null) index = nil; 24058 next_item = paths['$[]']($rb_plus(index, 1)); 24059 if ($truthy(next_item['$nil?']())) { 24060 return (result = "" + (result) + (item)) 24061 } else { 24062 24063 if (($truthy(item['$end_with?']($$('SEPARATOR'))) && ($truthy(next_item['$start_with?']($$('SEPARATOR')))))) { 24064 item = item.$sub($regexp([$$('SEPARATOR'), "+$"]), "") 24065 }; 24066 return (result = (($truthy(item['$end_with?']($$('SEPARATOR'))) || ($truthy(next_item['$start_with?']($$('SEPARATOR'))))) ? ("" + (result) + (item)) : ("" + (result) + (item) + ($$('SEPARATOR'))))); 24067 };}); 24068 return result; 24069 }, -1); 24070 24071 $def(self, '$split', function $$split(path) { 24072 24073 return path.$split($$('SEPARATOR')) 24074 }); 24075 $alias(self, "realpath", "expand_path"); 24076 return $alias(self, "exists?", "exist?"); 24077 })(Opal.get_singleton_class(self), $nesting); 24078 })('::', $$$('IO'), $nesting) 24079}; 24080 24081Opal.modules["corelib/process/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24082 var $klass = Opal.klass, $slice = Opal.slice, $defs = Opal.defs, $return_val = Opal.return_val, nil = Opal.nil; 24083 24084 24085 (function($base, $super) { 24086 var self = $klass($base, $super, 'Signal'); 24087 24088 24089 return $defs(self, '$trap', function $$trap($a) { 24090 var $post_args, $fwd_rest; 24091 24092 24093 $post_args = $slice(arguments); 24094 $fwd_rest = $post_args; 24095 return nil; 24096 }, -1) 24097 })('::', null); 24098 return (function($base, $super) { 24099 var self = $klass($base, $super, 'GC'); 24100 24101 24102 return $defs(self, '$start', $return_val(nil)) 24103 })('::', null); 24104}; 24105 24106Opal.modules["corelib/process"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24107 var $module = Opal.module, $defs = Opal.defs, $truthy = Opal.truthy, $return_val = Opal.return_val, $Kernel = Opal.Kernel, nil = Opal.nil, $$$ = Opal.$$$; 24108 24109 Opal.add_stubs('const_set,size,<<,__register_clock__,to_f,now,new,[],raise'); 24110 return (function($base) { 24111 var self = $module($base, 'Process'); 24112 24113 var monotonic = nil; 24114 24115 24116 self.__clocks__ = []; 24117 $defs(self, '$__register_clock__', function $$__register_clock__(name, func) { 24118 var self = this; 24119 if (self.__clocks__ == null) self.__clocks__ = nil; 24120 24121 24122 self.$const_set(name, self.__clocks__.$size()); 24123 return self.__clocks__['$<<'](func); 24124 }); 24125 self.$__register_clock__("CLOCK_REALTIME", function() { return Date.now() }); 24126 monotonic = false; 24127 24128 if (Opal.global.performance) { 24129 monotonic = function() { 24130 return performance.now() 24131 }; 24132 } 24133 else if (Opal.global.process && process.hrtime) { 24134 // let now be the base to get smaller numbers 24135 var hrtime_base = process.hrtime(); 24136 24137 monotonic = function() { 24138 var hrtime = process.hrtime(hrtime_base); 24139 var us = (hrtime[1] / 1000) | 0; // cut below microsecs; 24140 return ((hrtime[0] * 1000) + (us / 1000)); 24141 }; 24142 } 24143 ; 24144 if ($truthy(monotonic)) { 24145 self.$__register_clock__("CLOCK_MONOTONIC", monotonic) 24146 }; 24147 $defs(self, '$pid', $return_val(0)); 24148 $defs(self, '$times', function $$times() { 24149 var t = nil; 24150 24151 24152 t = $$$('Time').$now().$to_f(); 24153 return $$$($$$('Benchmark'), 'Tms').$new(t, t, t, t, t); 24154 }); 24155 return $defs(self, '$clock_gettime', function $$clock_gettime(clock_id, unit) { 24156 var self = this, $ret_or_1 = nil, clock = nil; 24157 if (self.__clocks__ == null) self.__clocks__ = nil; 24158 24159 24160 if (unit == null) unit = "float_second"; 24161 if ($truthy(($ret_or_1 = (clock = self.__clocks__['$[]'](clock_id))))) { 24162 $ret_or_1 24163 } else { 24164 $Kernel.$raise($$$($$$('Errno'), 'EINVAL'), "clock_gettime(" + (clock_id) + ") " + (self.__clocks__['$[]'](clock_id))) 24165 }; 24166 24167 var ms = clock(); 24168 switch (unit) { 24169 case 'float_second': return (ms / 1000); // number of seconds as a float (default) 24170 case 'float_millisecond': return (ms / 1); // number of milliseconds as a float 24171 case 'float_microsecond': return (ms * 1000); // number of microseconds as a float 24172 case 'second': return ((ms / 1000) | 0); // number of seconds as an integer 24173 case 'millisecond': return ((ms / 1) | 0); // number of milliseconds as an integer 24174 case 'microsecond': return ((ms * 1000) | 0); // number of microseconds as an integer 24175 case 'nanosecond': return ((ms * 1000000) | 0); // number of nanoseconds as an integer 24176 default: $Kernel.$raise($$$('ArgumentError'), "unexpected unit: " + (unit)) 24177 } 24178 ; 24179 }, -2); 24180 })('::') 24181}; 24182 24183Opal.modules["corelib/random/formatter"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24184 var $klass = Opal.klass, $module = Opal.module, $def = Opal.def, $range = Opal.range, $send = Opal.send, $rb_divide = Opal.rb_divide, $Kernel = Opal.Kernel, $Opal = Opal.Opal, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 24185 24186 Opal.add_stubs('_verify_count,bytes,encode,strict_encode64,random_bytes,urlsafe_encode64,split,hex,[]=,[],map,to_proc,join,times,<<,|,ord,/,abs,random_float,raise,coerce_to!,flatten,new,random_number,length,include,extend'); 24187 return (function($base, $super, $parent_nesting) { 24188 var self = $klass($base, $super, 'Random'); 24189 24190 var $nesting = [self].concat($parent_nesting); 24191 24192 24193 (function($base, $parent_nesting) { 24194 var self = $module($base, 'Formatter'); 24195 24196 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 24197 24198 24199 24200 $def(self, '$hex', function $$hex(count) { 24201 var self = this; 24202 24203 24204 if (count == null) count = nil; 24205 count = $$$('Random').$_verify_count(count); 24206 24207 var bytes = self.$bytes(count); 24208 var out = ""; 24209 for (var i = 0; i < count; i++) { 24210 out += bytes.charCodeAt(i).toString(16).padStart(2, '0'); 24211 } 24212 return (out).$encode("US-ASCII"); 24213 ; 24214 }, -1); 24215 24216 $def(self, '$random_bytes', function $$random_bytes(count) { 24217 var self = this; 24218 24219 24220 if (count == null) count = nil; 24221 return self.$bytes(count); 24222 }, -1); 24223 24224 $def(self, '$base64', function $$base64(count) { 24225 var self = this; 24226 24227 24228 if (count == null) count = nil; 24229 return $$$('Base64').$strict_encode64(self.$random_bytes(count)).$encode("US-ASCII"); 24230 }, -1); 24231 24232 $def(self, '$urlsafe_base64', function $$urlsafe_base64(count, padding) { 24233 var self = this; 24234 24235 24236 if (count == null) count = nil; 24237 if (padding == null) padding = false; 24238 return $$$('Base64').$urlsafe_encode64(self.$random_bytes(count), padding).$encode("US-ASCII"); 24239 }, -1); 24240 24241 $def(self, '$uuid', function $$uuid() { 24242 var self = this, str = nil; 24243 24244 24245 str = self.$hex(16).$split(""); 24246 str['$[]='](12, "4"); 24247 str['$[]='](16, (parseInt(str['$[]'](16), 16) & 3 | 8).toString(16)); 24248 str = [str['$[]']($range(0, 8, true)), str['$[]']($range(8, 12, true)), str['$[]']($range(12, 16, true)), str['$[]']($range(16, 20, true)), str['$[]']($range(20, 32, true))]; 24249 str = $send(str, 'map', [], "join".$to_proc()); 24250 return str.$join("-"); 24251 }); 24252 24253 $def(self, '$random_float', function $$random_float() { 24254 var self = this, bs = nil, num = nil; 24255 24256 24257 bs = self.$bytes(4); 24258 num = 0; 24259 $send((4), 'times', [], function $$1(i){ 24260 24261 if (i == null) i = nil; 24262 num = num['$<<'](8); 24263 return (num = num['$|'](bs['$[]'](i).$ord()));}); 24264 return $rb_divide(num.$abs(), 2147483647); 24265 }); 24266 24267 $def(self, '$random_number', function $$random_number(limit) { 24268 var self = this; 24269 24270 24271 ; 24272 24273 function randomFloat() { 24274 return self.$random_float(); 24275 } 24276 24277 function randomInt(max) { 24278 return Math.floor(randomFloat() * max); 24279 } 24280 24281 function randomRange() { 24282 var min = limit.begin, 24283 max = limit.end; 24284 24285 if (min === nil || max === nil) { 24286 return nil; 24287 } 24288 24289 var length = max - min; 24290 24291 if (length < 0) { 24292 return nil; 24293 } 24294 24295 if (length === 0) { 24296 return min; 24297 } 24298 24299 if (max % 1 === 0 && min % 1 === 0 && !limit.excl) { 24300 length++; 24301 } 24302 24303 return randomInt(length) + min; 24304 } 24305 24306 if (limit == null) { 24307 return randomFloat(); 24308 } else if (limit.$$is_range) { 24309 return randomRange(); 24310 } else if (limit.$$is_number) { 24311 if (limit <= 0) { 24312 $Kernel.$raise($$$('ArgumentError'), "invalid argument - " + (limit)) 24313 } 24314 24315 if (limit % 1 === 0) { 24316 // integer 24317 return randomInt(limit); 24318 } else { 24319 return randomFloat() * limit; 24320 } 24321 } else { 24322 limit = $Opal['$coerce_to!'](limit, $$$('Integer'), "to_int"); 24323 24324 if (limit <= 0) { 24325 $Kernel.$raise($$$('ArgumentError'), "invalid argument - " + (limit)) 24326 } 24327 24328 return randomInt(limit); 24329 } 24330 ; 24331 }, -1); 24332 return $def(self, '$alphanumeric', function $$alphanumeric(count) { 24333 var self = this, map = nil; 24334 24335 24336 if (count == null) count = nil; 24337 count = $$('Random').$_verify_count(count); 24338 map = $send([$range("0", "9", false), $range("a", "z", false), $range("A", "Z", false)], 'map', [], "to_a".$to_proc()).$flatten(); 24339 return $send($$$('Array'), 'new', [count], function $$2(i){var self = $$2.$$s == null ? this : $$2.$$s; 24340 24341 24342 if (i == null) i = nil; 24343 return map['$[]'](self.$random_number(map.$length()));}, {$$s: self}).$join(); 24344 }, -1); 24345 })(self, $nesting); 24346 self.$include($$$($$$('Random'), 'Formatter')); 24347 return self.$extend($$$($$$('Random'), 'Formatter')); 24348 })('::', null, $nesting) 24349}; 24350 24351Opal.modules["corelib/random/mersenne_twister"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24352 var $klass = Opal.klass, $const_set = Opal.const_set, $send = Opal.send, nil = Opal.nil, $$$ = Opal.$$$, mersenne_twister = nil; 24353 24354 Opal.add_stubs('generator='); 24355 24356 mersenne_twister = (function() { 24357 /* Period parameters */ 24358 var N = 624; 24359 var M = 397; 24360 var MATRIX_A = 0x9908b0df; /* constant vector a */ 24361 var UMASK = 0x80000000; /* most significant w-r bits */ 24362 var LMASK = 0x7fffffff; /* least significant r bits */ 24363 var MIXBITS = function(u,v) { return ( ((u) & UMASK) | ((v) & LMASK) ); }; 24364 var TWIST = function(u,v) { return (MIXBITS((u),(v)) >>> 1) ^ ((v & 0x1) ? MATRIX_A : 0x0); }; 24365 24366 function init(s) { 24367 var mt = {left: 0, next: N, state: new Array(N)}; 24368 init_genrand(mt, s); 24369 return mt; 24370 } 24371 24372 /* initializes mt[N] with a seed */ 24373 function init_genrand(mt, s) { 24374 var j, i; 24375 mt.state[0] = s >>> 0; 24376 for (j=1; j<N; j++) { 24377 mt.state[j] = (1812433253 * ((mt.state[j-1] ^ (mt.state[j-1] >> 30) >>> 0)) + j); 24378 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 24379 /* In the previous versions, MSBs of the seed affect */ 24380 /* only MSBs of the array state[]. */ 24381 /* 2002/01/09 modified by Makoto Matsumoto */ 24382 mt.state[j] &= 0xffffffff; /* for >32 bit machines */ 24383 } 24384 mt.left = 1; 24385 mt.next = N; 24386 } 24387 24388 /* generate N words at one time */ 24389 function next_state(mt) { 24390 var p = 0, _p = mt.state; 24391 var j; 24392 24393 mt.left = N; 24394 mt.next = 0; 24395 24396 for (j=N-M+1; --j; p++) 24397 _p[p] = _p[p+(M)] ^ TWIST(_p[p+(0)], _p[p+(1)]); 24398 24399 for (j=M; --j; p++) 24400 _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[p+(1)]); 24401 24402 _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[0]); 24403 } 24404 24405 /* generates a random number on [0,0xffffffff]-interval */ 24406 function genrand_int32(mt) { 24407 /* mt must be initialized */ 24408 var y; 24409 24410 if (--mt.left <= 0) next_state(mt); 24411 y = mt.state[mt.next++]; 24412 24413 /* Tempering */ 24414 y ^= (y >>> 11); 24415 y ^= (y << 7) & 0x9d2c5680; 24416 y ^= (y << 15) & 0xefc60000; 24417 y ^= (y >>> 18); 24418 24419 return y >>> 0; 24420 } 24421 24422 function int_pair_to_real_exclusive(a, b) { 24423 a >>>= 5; 24424 b >>>= 6; 24425 return(a*67108864.0+b)*(1.0/9007199254740992.0); 24426 } 24427 24428 // generates a random number on [0,1) with 53-bit resolution 24429 function genrand_real(mt) { 24430 /* mt must be initialized */ 24431 var a = genrand_int32(mt), b = genrand_int32(mt); 24432 return int_pair_to_real_exclusive(a, b); 24433 } 24434 24435 return { genrand_real: genrand_real, init: init }; 24436})(); 24437 return (function($base, $super) { 24438 var self = $klass($base, $super, 'Random'); 24439 24440 var $a; 24441 24442 24443 var MAX_INT = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; 24444 $const_set(self, 'MERSENNE_TWISTER_GENERATOR', { 24445 new_seed: function() { return Math.round(Math.random() * MAX_INT); }, 24446 reseed: function(seed) { return mersenne_twister.init(seed); }, 24447 rand: function(mt) { return mersenne_twister.genrand_real(mt); } 24448 }); 24449 return ($a = [$$$(self, 'MERSENNE_TWISTER_GENERATOR')], $send(self, 'generator=', $a), $a[$a.length - 1]); 24450 })('::', null); 24451}; 24452 24453Opal.modules["corelib/random"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24454 var $truthy = Opal.truthy, $klass = Opal.klass, $Kernel = Opal.Kernel, $defs = Opal.defs, $Opal = Opal.Opal, $def = Opal.def, $eqeqeq = Opal.eqeqeq, $send = Opal.send, self = Opal.top, nil = Opal.nil, $$$ = Opal.$$$; 24455 24456 Opal.add_stubs('require,attr_reader,to_int,raise,new_seed,coerce_to!,reseed,rand,seed,bytes,===,==,state,_verify_count,encode,join,new,chr,random_number,random_float,const_defined?,const_set'); 24457 24458 self.$require("corelib/random/formatter"); 24459 (function($base, $super) { 24460 var self = $klass($base, $super, 'Random'); 24461 24462 24463 24464 self.$attr_reader("seed", "state"); 24465 $defs(self, '$_verify_count', function $$_verify_count(count) { 24466 24467 24468 if (!$truthy(count)) count = 16; 24469 if (typeof count !== "number") count = (count).$to_int(); 24470 if (count < 0) $Kernel.$raise($$$('ArgumentError'), "negative string size (or size too big)"); 24471 count = Math.floor(count); 24472 return count; 24473 24474 }); 24475 24476 $def(self, '$initialize', function $$initialize(seed) { 24477 var self = this; 24478 24479 24480 if (seed == null) seed = $$$('Random').$new_seed(); 24481 seed = $Opal['$coerce_to!'](seed, $$$('Integer'), "to_int"); 24482 self.state = seed; 24483 return self.$reseed(seed); 24484 }, -1); 24485 24486 $def(self, '$reseed', function $$reseed(seed) { 24487 var self = this; 24488 24489 24490 self.seed = seed; 24491 return self.$rng = Opal.$$rand.reseed(seed);; 24492 }); 24493 $defs(self, '$new_seed', function $$new_seed() { 24494 24495 return Opal.$$rand.new_seed(); 24496 }); 24497 $defs(self, '$rand', function $$rand(limit) { 24498 var self = this; 24499 24500 24501 ; 24502 return $$$(self, 'DEFAULT').$rand(limit); 24503 }, -1); 24504 $defs(self, '$srand', function $$srand(n) { 24505 var self = this, previous_seed = nil; 24506 24507 24508 if (n == null) n = $$$('Random').$new_seed(); 24509 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 24510 previous_seed = $$$(self, 'DEFAULT').$seed(); 24511 $$$(self, 'DEFAULT').$reseed(n); 24512 return previous_seed; 24513 }, -1); 24514 $defs(self, '$urandom', function $$urandom(size) { 24515 24516 return $$$('SecureRandom').$bytes(size) 24517 }); 24518 24519 $def(self, '$==', function $Random_$eq_eq$1(other) { 24520 var self = this, $ret_or_1 = nil; 24521 24522 24523 if (!$eqeqeq($$$('Random'), other)) { 24524 return false 24525 }; 24526 if ($truthy(($ret_or_1 = self.$seed()['$=='](other.$seed())))) { 24527 return self.$state()['$=='](other.$state()) 24528 } else { 24529 return $ret_or_1 24530 }; 24531 }); 24532 24533 $def(self, '$bytes', function $$bytes(length) { 24534 var self = this; 24535 24536 24537 length = $$$('Random').$_verify_count(length); 24538 return $send($$$('Array'), 'new', [length], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 24539 24540 return self.$rand(255).$chr()}, {$$s: self}).$join().$encode("ASCII-8BIT"); 24541 }); 24542 $defs(self, '$bytes', function $$bytes(length) { 24543 var self = this; 24544 24545 return $$$(self, 'DEFAULT').$bytes(length) 24546 }); 24547 24548 $def(self, '$rand', function $$rand(limit) { 24549 var self = this; 24550 24551 24552 ; 24553 return self.$random_number(limit); 24554 }, -1); 24555 24556 $def(self, '$random_float', function $$random_float() { 24557 var self = this; 24558 24559 24560 self.state++; 24561 return Opal.$$rand.rand(self.$rng); 24562 24563 }); 24564 $defs(self, '$random_float', function $$random_float() { 24565 var self = this; 24566 24567 return $$$(self, 'DEFAULT').$random_float() 24568 }); 24569 return $defs(self, '$generator=', function $Random_generator$eq$3(generator) { 24570 var self = this; 24571 24572 24573 Opal.$$rand = generator; 24574 if ($truthy(self['$const_defined?']("DEFAULT"))) { 24575 return $$$(self, 'DEFAULT').$reseed() 24576 } else { 24577 return self.$const_set("DEFAULT", self.$new(self.$new_seed())) 24578 }; 24579 }); 24580 })('::', null); 24581 return self.$require("corelib/random/mersenne_twister"); 24582}; 24583 24584Opal.modules["corelib/unsupported"] = function(Opal) {/* Generated by Opal 1.7.3 */ 24585 var $Kernel = Opal.Kernel, $klass = Opal.klass, $send = Opal.send, $slice = Opal.slice, $module = Opal.module, $def = Opal.def, $return_val = Opal.return_val, $alias = Opal.alias, $defs = Opal.defs, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 24586 24587 Opal.add_stubs('raise,warn,each,define_method,%,public,private_method_defined?,private_class_method,instance_method,instance_methods,method_defined?,private_methods'); 24588 24589 24590 var warnings = {}; 24591 24592 function handle_unsupported_feature(message) { 24593 switch (Opal.config.unsupported_features_severity) { 24594 case 'error': 24595 $Kernel.$raise($$$('NotImplementedError'), message) 24596 break; 24597 case 'warning': 24598 warn(message) 24599 break; 24600 default: // ignore 24601 // noop 24602 } 24603 } 24604 24605 function warn(string) { 24606 if (warnings[string]) { 24607 return; 24608 } 24609 24610 warnings[string] = true; 24611 self.$warn(string); 24612 } 24613; 24614 (function($base, $super) { 24615 var self = $klass($base, $super, 'String'); 24616 24617 24618 24619 var ERROR = "String#%s not supported. Mutable String methods are not supported in Opal."; 24620 return $send(["<<", "capitalize!", "chomp!", "chop!", "downcase!", "gsub!", "lstrip!", "next!", "reverse!", "slice!", "squeeze!", "strip!", "sub!", "succ!", "swapcase!", "tr!", "tr_s!", "upcase!", "prepend", "[]=", "clear", "encode!", "unicode_normalize!"], 'each', [], function $String$1(method_name){var self = $String$1.$$s == null ? this : $String$1.$$s; 24621 24622 24623 if (method_name == null) method_name = nil; 24624 return $send(self, 'define_method', [method_name], function $$2($a){var $post_args, $fwd_rest; 24625 24626 24627 $post_args = $slice(arguments); 24628 $fwd_rest = $post_args; 24629 return $Kernel.$raise($$$('NotImplementedError'), (ERROR)['$%'](method_name));}, -1);}, {$$s: self}); 24630 })('::', null); 24631 (function($base) { 24632 var self = $module($base, 'Kernel'); 24633 24634 24635 24636 var ERROR = "Object tainting is not supported by Opal"; 24637 24638 $def(self, '$taint', function $$taint() { 24639 var self = this; 24640 24641 24642 handle_unsupported_feature(ERROR); 24643 return self; 24644 }); 24645 24646 $def(self, '$untaint', function $$untaint() { 24647 var self = this; 24648 24649 24650 handle_unsupported_feature(ERROR); 24651 return self; 24652 }); 24653 return $def(self, '$tainted?', function $Kernel_tainted$ques$3() { 24654 24655 24656 handle_unsupported_feature(ERROR); 24657 return false; 24658 }); 24659 })('::'); 24660 (function($base, $super) { 24661 var self = $klass($base, $super, 'Module'); 24662 24663 24664 24665 24666 $def(self, '$public', function $Module_public$4($a) { 24667 var $post_args, methods, self = this; 24668 24669 24670 $post_args = $slice(arguments); 24671 methods = $post_args; 24672 24673 if (methods.length === 0) { 24674 self.$$module_function = false; 24675 return nil; 24676 } 24677 return (methods.length === 1) ? methods[0] : methods; 24678 ; 24679 }, -1); 24680 24681 $def(self, '$private_class_method', function $$private_class_method($a) { 24682 var $post_args, methods; 24683 24684 24685 $post_args = $slice(arguments); 24686 methods = $post_args; 24687 return (methods.length === 1) ? methods[0] : methods;; 24688 }, -1); 24689 24690 $def(self, '$private_method_defined?', $return_val(false)); 24691 24692 $def(self, '$private_constant', function $$private_constant($a) { 24693 var $post_args, $fwd_rest; 24694 24695 24696 $post_args = $slice(arguments); 24697 $fwd_rest = $post_args; 24698 return nil; 24699 }, -1); 24700 $alias(self, "nesting", "public"); 24701 $alias(self, "private", "public"); 24702 $alias(self, "protected", "public"); 24703 $alias(self, "protected_method_defined?", "private_method_defined?"); 24704 $alias(self, "public_class_method", "private_class_method"); 24705 $alias(self, "public_instance_method", "instance_method"); 24706 $alias(self, "public_instance_methods", "instance_methods"); 24707 return $alias(self, "public_method_defined?", "method_defined?"); 24708 })('::', null); 24709 (function($base) { 24710 var self = $module($base, 'Kernel'); 24711 24712 24713 24714 24715 $def(self, '$private_methods', function $$private_methods($a) { 24716 var $post_args, methods; 24717 24718 24719 $post_args = $slice(arguments); 24720 methods = $post_args; 24721 return []; 24722 }, -1); 24723 $alias(self, "protected_methods", "private_methods"); 24724 $alias(self, "private_instance_methods", "private_methods"); 24725 return $alias(self, "protected_instance_methods", "private_methods"); 24726 })('::'); 24727 (function($base, $parent_nesting) { 24728 var self = $module($base, 'Kernel'); 24729 24730 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 24731 24732 return $def(self, '$eval', function $Kernel_eval$5($a) { 24733 var $post_args, $fwd_rest; 24734 24735 24736 $post_args = $slice(arguments); 24737 $fwd_rest = $post_args; 24738 return $Kernel.$raise($$$('NotImplementedError'), "To use Kernel#eval, you must first require 'opal-parser'. " + ("See https://github.com/opal/opal/blob/" + ($$('RUBY_ENGINE_VERSION')) + "/docs/opal_parser.md for details.")); 24739 }, -1) 24740 })('::', $nesting); 24741 $defs(self, '$public', function $public$6($a) { 24742 var $post_args, methods; 24743 24744 24745 $post_args = $slice(arguments); 24746 methods = $post_args; 24747 return (methods.length === 1) ? methods[0] : methods;; 24748 }, -1); 24749 return $defs(self, '$private', function $private$7($a) { 24750 var $post_args, methods; 24751 24752 24753 $post_args = $slice(arguments); 24754 methods = $post_args; 24755 return (methods.length === 1) ? methods[0] : methods;; 24756 }, -1); 24757}; 24758 24759Opal.queue(function(Opal) {/* Generated by Opal 1.7.3 */ 24760 var $Object = Opal.Object, nil = Opal.nil; 24761 24762 Opal.add_stubs('require,autoload'); 24763 24764 $Object.$require("opal/base"); 24765 $Object.$require("opal/mini"); 24766 $Object.$require("corelib/kernel/format"); 24767 $Object.$require("corelib/string/encoding"); 24768 $Object.$autoload("Math", "corelib/math"); 24769 $Object.$require("corelib/complex/base"); 24770 $Object.$autoload("Complex", "corelib/complex"); 24771 $Object.$require("corelib/rational/base"); 24772 $Object.$autoload("Rational", "corelib/rational"); 24773 $Object.$require("corelib/time"); 24774 $Object.$autoload("Struct", "corelib/struct"); 24775 $Object.$autoload("Set", "corelib/set"); 24776 $Object.$autoload("Dir", "corelib/dir"); 24777 $Object.$autoload("File", "corelib/file"); 24778 $Object.$require("corelib/process/base"); 24779 $Object.$autoload("Process", "corelib/process"); 24780 $Object.$autoload("Random", "corelib/random"); 24781 return $Object.$require("corelib/unsupported"); 24782}); 24783 24784 24785export default Opal 24786