1(function(global_object) { 2 3 // @note 4 // A few conventions for the documentation of this file: 5 // 1. Always use "//" (in contrast with "/**/") 6 // 2. The syntax used is Yardoc (yardoc.org), which is intended for Ruby (se below) 7 // 3. `@param` and `@return` types should be preceded by `JS.` when referring to 8 // JavaScript constructors (e.g. `JS.Function`) otherwise Ruby is assumed. 9 // 4. `nil` and `null` being unambiguous refer to the respective 10 // objects/values in Ruby and JavaScript 11 // 5. This is still WIP :) so please give feedback and suggestions on how 12 // to improve or for alternative solutions 13 // 14 // The way the code is digested before going through Yardoc is a secret kept 15 // in the docs repo (https://github.com/opal/docs/tree/master). 16 17 var console; 18 19 // Detect the global object 20 if (typeof(globalThis) !== 'undefined') { global_object = globalThis; } 21 else if (typeof(global) !== 'undefined') { global_object = global; } 22 else if (typeof(window) !== 'undefined') { global_object = window; } 23 24 // Setup a dummy console object if missing 25 if (global_object.console == null) { 26 global_object.console = {}; 27 } 28 29 if (typeof(global_object.console) === 'object') { 30 console = global_object.console; 31 } else { 32 console = {}; 33 } 34 35 if (!('log' in console)) { console.log = function () {}; } 36 if (!('warn' in console)) { console.warn = console.log; } 37 38 if (typeof(global_object.Opal) !== 'undefined') { 39 console.warn('Opal already loaded. Loading twice can cause troubles, please fix your setup.'); 40 return global_object.Opal; 41 } 42 43 var nil; 44 45 // The actual class for BasicObject 46 var BasicObject; 47 48 // The actual Object class. 49 // The leading underscore is to avoid confusion with window.Object() 50 var _Object; 51 52 // The actual Module class 53 var Module; 54 55 // The actual Class class 56 var Class; 57 58 // The Opal.Opal class (helpers etc.) 59 var _Opal; 60 61 // The Kernel module 62 var Kernel; 63 64 // The Opal object that is exposed globally 65 var Opal = global_object.Opal = {}; 66 67 // This is a useful reference to global object inside ruby files 68 Opal.global = global_object; 69 70 // Configure runtime behavior with regards to require and unsupported features 71 Opal.config = { 72 missing_require_severity: 'error', // error, warning, ignore 73 unsupported_features_severity: 'warning', // error, warning, ignore 74 experimental_features_severity: 'warning',// warning, ignore 75 enable_stack_trace: true // true, false 76 }; 77 78 // Minify common function calls 79 var $call = Function.prototype.call; 80 var $bind = Function.prototype.bind; 81 var $has_own = Object.hasOwn || $call.bind(Object.prototype.hasOwnProperty); 82 var $set_proto = Object.setPrototypeOf; 83 var $slice = $call.bind(Array.prototype.slice); 84 var $splice = $call.bind(Array.prototype.splice); 85 86 // Nil object id is always 4 87 var nil_id = 4; 88 89 // Generates even sequential numbers greater than 4 90 // (nil_id) to serve as unique ids for ruby objects 91 var unique_id = nil_id; 92 93 // Return next unique id 94 function $uid() { 95 unique_id += 2; 96 return unique_id; 97 } Opal.uid = $uid; 98 99 // Retrieve or assign the id of an object 100 Opal.id = function(obj) { 101 if (obj.$$is_number) return (obj * 2)+1; 102 if (obj.$$id == null) { 103 $prop(obj, '$$id', $uid()); 104 } 105 return obj.$$id; 106 }; 107 108 // Globals table 109 var $gvars = Opal.gvars = {}; 110 111 // Exit function, this should be replaced by platform specific implementation 112 // (See nodejs and chrome for examples) 113 Opal.exit = function(status) { if ($gvars.DEBUG) console.log('Exited with status '+status); }; 114 115 // keeps track of exceptions for $! 116 Opal.exceptions = []; 117 118 // @private 119 // Pops an exception from the stack and updates `$!`. 120 Opal.pop_exception = function() { 121 var exception = Opal.exceptions.pop(); 122 if (exception) { 123 $gvars["!"] = exception; 124 $gvars["@"] = exception.$backtrace(); 125 } 126 else { 127 $gvars["!"] = $gvars["@"] = nil; 128 } 129 }; 130 131 // A helper function for raising things, that gracefully degrades if necessary 132 // functionality is not yet loaded. 133 function $raise(klass, message) { 134 // Raise Exception, so we can know that something wrong is going on. 135 if (!klass) klass = Opal.Exception || Error; 136 137 if (Kernel && Kernel.$raise) { 138 if (arguments.length > 2) { 139 Kernel.$raise(klass.$new.apply(klass, $slice(arguments, 1))); 140 } 141 else { 142 Kernel.$raise(klass, message); 143 } 144 } 145 else if (!klass.$new) { 146 throw new klass(message); 147 } 148 else { 149 throw klass.$new(message); 150 } 151 } 152 153 function $prop(object, name, initialValue) { 154 if (typeof(object) === "string") { 155 // Special case for: 156 // s = "string" 157 // def s.m; end 158 // String class is the only class that: 159 // + compiles to JS primitive 160 // + allows method definition directly on instances 161 // numbers, true, false and null do not support it. 162 object[name] = initialValue; 163 } else { 164 Object.defineProperty(object, name, { 165 value: initialValue, 166 enumerable: false, 167 configurable: true, 168 writable: true 169 }); 170 } 171 } 172 173 Opal.prop = $prop; 174 175 // @deprecated 176 Opal.defineProperty = Opal.prop; 177 178 Opal.slice = $slice; 179 180 // Helpers 181 // ----- 182 183 var $truthy = Opal.truthy = function(val) { 184 return false !== val && nil !== val && undefined !== val && null !== val && (!(val instanceof Boolean) || true === val.valueOf()); 185 }; 186 187 Opal.falsy = function(val) { 188 return !$truthy(val); 189 }; 190 191 Opal.type_error = function(object, type, method, coerced) { 192 object = object.$$class; 193 194 if (coerced && method) { 195 coerced = coerced.$$class; 196 $raise(Opal.TypeError, 197 "can't convert " + object + " into " + type + 198 " (" + object + "#" + method + " gives " + coerced + ")" 199 ); 200 } else { 201 $raise(Opal.TypeError, 202 "no implicit conversion of " + object + " into " + type 203 ); 204 } 205 }; 206 207 Opal.coerce_to = function(object, type, method, args) { 208 var body; 209 210 if (method === 'to_int' && type === Opal.Integer && object.$$is_number) 211 return object < 0 ? Math.ceil(object) : Math.floor(object); 212 213 if (method === 'to_str' && type === Opal.String && object.$$is_string) 214 return object; 215 216 if (Opal.is_a(object, type)) return object; 217 218 // Fast path for the most common situation 219 if (object['$respond_to?'].$$pristine && object.$method_missing.$$pristine) { 220 body = object[$jsid(method)]; 221 if (body == null || body.$$stub) Opal.type_error(object, type); 222 return body.apply(object, args); 223 } 224 225 if (!object['$respond_to?'](method)) { 226 Opal.type_error(object, type); 227 } 228 229 if (args == null) args = []; 230 return Opal.send(object, method, args); 231 }; 232 233 Opal.respond_to = function(obj, jsid, include_all) { 234 if (obj == null || !obj.$$class) return false; 235 include_all = !!include_all; 236 var body = obj[jsid]; 237 238 if (obj['$respond_to?'].$$pristine) { 239 if (typeof(body) === "function" && !body.$$stub) { 240 return true; 241 } 242 if (!obj['$respond_to_missing?'].$$pristine) { 243 return Opal.send(obj, obj['$respond_to_missing?'], [jsid.substr(1), include_all]); 244 } 245 } else { 246 return Opal.send(obj, obj['$respond_to?'], [jsid.substr(1), include_all]); 247 } 248 }; 249 250 // TracePoint support 251 // ------------------ 252 // 253 // Support for `TracePoint.trace(:class) do ... end` 254 Opal.trace_class = false; 255 Opal.tracers_for_class = []; 256 257 function invoke_tracers_for_class(klass_or_module) { 258 var i, ii, tracer; 259 260 for(i = 0, ii = Opal.tracers_for_class.length; i < ii; i++) { 261 tracer = Opal.tracers_for_class[i]; 262 tracer.trace_object = klass_or_module; 263 tracer.block.$call(tracer); 264 } 265 } 266 267 function handle_autoload(cref, name) { 268 if (!cref.$$autoload[name].loaded) { 269 cref.$$autoload[name].loaded = true; 270 try { 271 Opal.Kernel.$require(cref.$$autoload[name].path); 272 } catch (e) { 273 cref.$$autoload[name].exception = e; 274 throw e; 275 } 276 cref.$$autoload[name].required = true; 277 if (cref.$$const[name] != null) { 278 cref.$$autoload[name].success = true; 279 return cref.$$const[name]; 280 } 281 } else if (cref.$$autoload[name].loaded && !cref.$$autoload[name].required) { 282 if (cref.$$autoload[name].exception) { throw cref.$$autoload[name].exception; } 283 } 284 } 285 286 // Constants 287 // --------- 288 // 289 // For future reference: 290 // - The Rails autoloading guide (http://guides.rubyonrails.org/v5.0/autoloading_and_reloading_constants.html) 291 // - @ConradIrwin's 2012 post on “Everything you ever wanted to know about constant lookup in Ruby” (http://cirw.in/blog/constant-lookup.html) 292 // 293 // Legend of MRI concepts/names: 294 // - constant reference (cref): the module/class that acts as a namespace 295 // - nesting: the namespaces wrapping the current scope, e.g. nesting inside 296 // `module A; module B::C; end; end` is `[B::C, A]` 297 298 // Get the constant in the scope of the current cref 299 function const_get_name(cref, name) { 300 if (cref) { 301 if (cref.$$const[name] != null) { return cref.$$const[name]; } 302 if (cref.$$autoload && cref.$$autoload[name]) { 303 return handle_autoload(cref, name); 304 } 305 } 306 } 307 308 // Walk up the nesting array looking for the constant 309 function const_lookup_nesting(nesting, name) { 310 var i, ii, constant; 311 312 if (nesting.length === 0) return; 313 314 // If the nesting is not empty the constant is looked up in its elements 315 // and in order. The ancestors of those elements are ignored. 316 for (i = 0, ii = nesting.length; i < ii; i++) { 317 constant = nesting[i].$$const[name]; 318 if (constant != null) { 319 return constant; 320 } else if (nesting[i].$$autoload && nesting[i].$$autoload[name]) { 321 return handle_autoload(nesting[i], name); 322 } 323 } 324 } 325 326 // Walk up the ancestors chain looking for the constant 327 function const_lookup_ancestors(cref, name) { 328 var i, ii, ancestors; 329 330 if (cref == null) return; 331 332 ancestors = $ancestors(cref); 333 334 for (i = 0, ii = ancestors.length; i < ii; i++) { 335 if (ancestors[i].$$const && $has_own(ancestors[i].$$const, name)) { 336 return ancestors[i].$$const[name]; 337 } else if (ancestors[i].$$autoload && ancestors[i].$$autoload[name]) { 338 return handle_autoload(ancestors[i], name); 339 } 340 } 341 } 342 343 // Walk up Object's ancestors chain looking for the constant, 344 // but only if cref is missing or a module. 345 function const_lookup_Object(cref, name) { 346 if (cref == null || cref.$$is_module) { 347 return const_lookup_ancestors(_Object, name); 348 } 349 } 350 351 // Call const_missing if nothing else worked 352 function const_missing(cref, name) { 353 return (cref || _Object).$const_missing(name); 354 } 355 356 // Look for the constant just in the current cref or call `#const_missing` 357 Opal.const_get_local = function(cref, name, skip_missing) { 358 var result; 359 360 if (cref == null) return; 361 362 if (cref === '::') cref = _Object; 363 364 if (!cref.$$is_module && !cref.$$is_class) { 365 $raise(Opal.TypeError, cref.toString() + " is not a class/module"); 366 } 367 368 result = const_get_name(cref, name); 369 return result != null || skip_missing ? result : const_missing(cref, name); 370 }; 371 372 // Look for the constant relative to a cref or call `#const_missing` (when the 373 // constant is prefixed by `::`). 374 Opal.const_get_qualified = function(cref, name, skip_missing) { 375 var result, cache, cached, current_version = Opal.const_cache_version; 376 377 if (name == null) { 378 // A shortpath for calls like ::String => $$$("String") 379 result = const_get_name(_Object, cref); 380 381 if (result != null) return result; 382 return Opal.const_get_qualified(_Object, cref, skip_missing); 383 } 384 385 if (cref == null) return; 386 387 if (cref === '::') cref = _Object; 388 389 if (!cref.$$is_module && !cref.$$is_class) { 390 $raise(Opal.TypeError, cref.toString() + " is not a class/module"); 391 } 392 393 if ((cache = cref.$$const_cache) == null) { 394 $prop(cref, '$$const_cache', Object.create(null)); 395 cache = cref.$$const_cache; 396 } 397 cached = cache[name]; 398 399 if (cached == null || cached[0] !== current_version) { 400 ((result = const_get_name(cref, name)) != null) || 401 ((result = const_lookup_ancestors(cref, name)) != null); 402 cache[name] = [current_version, result]; 403 } else { 404 result = cached[1]; 405 } 406 407 return result != null || skip_missing ? result : const_missing(cref, name); 408 }; 409 410 // Initialize the top level constant cache generation counter 411 Opal.const_cache_version = 1; 412 413 // Look for the constant in the open using the current nesting and the nearest 414 // cref ancestors or call `#const_missing` (when the constant has no :: prefix). 415 Opal.const_get_relative = function(nesting, name, skip_missing) { 416 var cref = nesting[0], result, current_version = Opal.const_cache_version, cache, cached; 417 418 if ((cache = nesting.$$const_cache) == null) { 419 $prop(nesting, '$$const_cache', Object.create(null)); 420 cache = nesting.$$const_cache; 421 } 422 cached = cache[name]; 423 424 if (cached == null || cached[0] !== current_version) { 425 ((result = const_get_name(cref, name)) != null) || 426 ((result = const_lookup_nesting(nesting, name)) != null) || 427 ((result = const_lookup_ancestors(cref, name)) != null) || 428 ((result = const_lookup_Object(cref, name)) != null); 429 430 cache[name] = [current_version, result]; 431 } else { 432 result = cached[1]; 433 } 434 435 return result != null || skip_missing ? result : const_missing(cref, name); 436 }; 437 438 // Register the constant on a cref and opportunistically set the name of 439 // unnamed classes/modules. 440 function $const_set(cref, name, value) { 441 var new_const = true; 442 443 if (cref == null || cref === '::') cref = _Object; 444 445 if (value.$$is_a_module) { 446 if (value.$$name == null || value.$$name === nil) value.$$name = name; 447 if (value.$$base_module == null) value.$$base_module = cref; 448 } 449 450 cref.$$const = (cref.$$const || Object.create(null)); 451 452 if (name in cref.$$const || ("$$autoload" in cref && name in cref.$$autoload)) { 453 new_const = false; 454 } 455 456 cref.$$const[name] = value; 457 458 // Add a short helper to navigate constants manually. 459 // @example 460 // Opal.$$.Regexp.$$.IGNORECASE 461 cref.$$ = cref.$$const; 462 463 Opal.const_cache_version++; 464 465 // Expose top level constants onto the Opal object 466 if (cref === _Object) Opal[name] = value; 467 468 // Name new class directly onto current scope (Opal.Foo.Baz = klass) 469 $prop(cref, name, value); 470 471 if (new_const && cref.$const_added && !cref.$const_added.$$pristine) { 472 cref.$const_added(name); 473 } 474 475 return value; 476 } 477 Opal.const_set = $const_set; 478 479 // Get all the constants reachable from a given cref, by default will include 480 // inherited constants. 481 Opal.constants = function(cref, inherit) { 482 if (inherit == null) inherit = true; 483 484 var module, modules = [cref], i, ii, constants = {}, constant; 485 486 if (inherit) modules = modules.concat($ancestors(cref)); 487 if (inherit && cref.$$is_module) modules = modules.concat([Opal.Object]).concat($ancestors(Opal.Object)); 488 489 for (i = 0, ii = modules.length; i < ii; i++) { 490 module = modules[i]; 491 492 // Do not show Objects constants unless we're querying Object itself 493 if (cref !== _Object && module == _Object) break; 494 495 for (constant in module.$$const) { 496 constants[constant] = true; 497 } 498 if (module.$$autoload) { 499 for (constant in module.$$autoload) { 500 constants[constant] = true; 501 } 502 } 503 } 504 505 return Object.keys(constants); 506 }; 507 508 // Remove a constant from a cref. 509 Opal.const_remove = function(cref, name) { 510 Opal.const_cache_version++; 511 512 if (cref.$$const[name] != null) { 513 var old = cref.$$const[name]; 514 delete cref.$$const[name]; 515 return old; 516 } 517 518 if (cref.$$autoload && cref.$$autoload[name]) { 519 delete cref.$$autoload[name]; 520 return nil; 521 } 522 523 $raise(Opal.NameError, "constant "+cref+"::"+cref.$name()+" not defined"); 524 }; 525 526 // Generates a function that is a curried const_get_relative. 527 Opal.const_get_relative_factory = function(nesting) { 528 return function(name, skip_missing) { 529 return Opal.$$(nesting, name, skip_missing); 530 } 531 }; 532 533 // Setup some shortcuts to reduce compiled size 534 Opal.$$ = Opal.const_get_relative; 535 Opal.$$$ = Opal.const_get_qualified; 536 Opal.$r = Opal.const_get_relative_factory; 537 538 // Modules & Classes 539 // ----------------- 540 541 // A `class Foo; end` expression in ruby is compiled to call this runtime 542 // method which either returns an existing class of the given name, or creates 543 // a new class in the given `base` scope. 544 // 545 // If a constant with the given name exists, then we check to make sure that 546 // it is a class and also that the superclasses match. If either of these 547 // fail, then we raise a `TypeError`. Note, `superclass` may be null if one 548 // was not specified in the ruby code. 549 // 550 // We pass a constructor to this method of the form `function ClassName() {}` 551 // simply so that classes show up with nicely formatted names inside debuggers 552 // in the web browser (or node/sprockets). 553 // 554 // The `scope` is the current `self` value where the class is being created 555 // from. We use this to get the scope for where the class should be created. 556 // If `scope` is an object (not a class/module), we simple get its class and 557 // use that as the scope instead. 558 // 559 // @param scope [Object] where the class is being created 560 // @param superclass [Class,null] superclass of the new class (may be null) 561 // @param singleton [Boolean,null] a true value denotes we want to allocate 562 // a singleton 563 // 564 // @return new [Class] or existing ruby class 565 // 566 function $allocate_class(name, superclass, singleton) { 567 var klass; 568 569 if (superclass != null && superclass.$$bridge) { 570 // Inheritance from bridged classes requires 571 // calling original JS constructors 572 klass = function() { 573 var args = $slice(arguments), 574 self = new ($bind.apply(superclass.$$constructor, [null].concat(args)))(); 575 576 // and replacing a __proto__ manually 577 $set_proto(self, klass.$$prototype); 578 return self; 579 }; 580 } else { 581 klass = function(){}; 582 } 583 584 if (name && name !== nil) { 585 $prop(klass, 'displayName', '::'+name); 586 } 587 588 $prop(klass, '$$name', name); 589 $prop(klass, '$$constructor', klass); 590 $prop(klass, '$$prototype', klass.prototype); 591 $prop(klass, '$$const', {}); 592 $prop(klass, '$$is_class', true); 593 $prop(klass, '$$is_a_module', true); 594 $prop(klass, '$$super', superclass); 595 $prop(klass, '$$cvars', {}); 596 $prop(klass, '$$own_included_modules', []); 597 $prop(klass, '$$own_prepended_modules', []); 598 $prop(klass, '$$ancestors', []); 599 $prop(klass, '$$ancestors_cache_version', null); 600 $prop(klass, '$$subclasses', []); 601 602 $prop(klass.$$prototype, '$$class', klass); 603 604 // By default if there are no singleton class methods 605 // __proto__ is Class.prototype 606 // Later singleton methods generate a singleton_class 607 // and inject it into ancestors chain 608 if (Opal.Class) { 609 $set_proto(klass, Opal.Class.prototype); 610 } 611 612 if (superclass != null) { 613 $set_proto(klass.$$prototype, superclass.$$prototype); 614 615 if (singleton !== true) { 616 // Let's not forbid GC from cleaning up our 617 // subclasses. 618 if (typeof WeakRef !== 'undefined') { 619 // First, let's clean up our array from empty objects. 620 var i, subclass, rebuilt_subclasses = []; 621 for (i = 0; i < superclass.$$subclasses.length; i++) { 622 subclass = superclass.$$subclasses[i]; 623 if (subclass.deref() !== undefined) { 624 rebuilt_subclasses.push(subclass); 625 } 626 } 627 // Now, let's add our class. 628 rebuilt_subclasses.push(new WeakRef(klass)); 629 superclass.$$subclasses = rebuilt_subclasses; 630 } 631 else { 632 superclass.$$subclasses.push(klass); 633 } 634 } 635 636 if (superclass.$$meta) { 637 // If superclass has metaclass then we have explicitely inherit it. 638 Opal.build_class_singleton_class(klass); 639 } 640 } 641 642 return klass; 643 } Opal.allocate_class = $allocate_class; 644 645 646 function find_existing_class(scope, name) { 647 // Try to find the class in the current scope 648 var klass = const_get_name(scope, name); 649 650 // If the class exists in the scope, then we must use that 651 if (klass) { 652 // Make sure the existing constant is a class, or raise error 653 if (!klass.$$is_class) { 654 $raise(Opal.TypeError, name + " is not a class"); 655 } 656 657 return klass; 658 } 659 } 660 661 function ensureSuperclassMatch(klass, superclass) { 662 if (klass.$$super !== superclass) { 663 $raise(Opal.TypeError, "superclass mismatch for class " + klass.$$name); 664 } 665 } 666 667 Opal.klass = function(scope, superclass, name) { 668 var bridged; 669 670 if (scope == null || scope == '::') { 671 // Global scope 672 scope = _Object; 673 } else if (!scope.$$is_class && !scope.$$is_module) { 674 // Scope is an object, use its class 675 scope = scope.$$class; 676 } 677 678 // If the superclass is not an Opal-generated class then we're bridging a native JS class 679 if ( 680 superclass != null && (!superclass.hasOwnProperty || ( 681 superclass.hasOwnProperty && !superclass.hasOwnProperty('$$is_class') 682 )) 683 ) { 684 if (superclass.constructor && superclass.constructor.name == "Function") { 685 bridged = superclass; 686 superclass = _Object; 687 } else { 688 $raise(Opal.TypeError, "superclass must be a Class (" + ( 689 (superclass.constructor && (superclass.constructor.name || superclass.constructor.$$name)) || 690 typeof(superclass) 691 ) + " given)"); 692 } 693 } 694 695 var klass = find_existing_class(scope, name); 696 697 if (klass != null) { 698 if (superclass) { 699 // Make sure existing class has same superclass 700 ensureSuperclassMatch(klass, superclass); 701 } 702 } 703 else { 704 // Class doesn't exist, create a new one with given superclass... 705 706 // Not specifying a superclass means we can assume it to be Object 707 if (superclass == null) { 708 superclass = _Object; 709 } 710 711 // Create the class object (instance of Class) 712 klass = $allocate_class(name, superclass); 713 $const_set(scope, name, klass); 714 715 // Call .inherited() hook with new class on the superclass 716 if (superclass.$inherited) { 717 superclass.$inherited(klass); 718 } 719 720 if (bridged) { 721 Opal.bridge(bridged, klass); 722 } 723 } 724 725 if (Opal.trace_class) { invoke_tracers_for_class(klass); } 726 727 return klass; 728 }; 729 730 // Define new module (or return existing module). The given `scope` is basically 731 // the current `self` value the `module` statement was defined in. If this is 732 // a ruby module or class, then it is used, otherwise if the scope is a ruby 733 // object then that objects real ruby class is used (e.g. if the scope is the 734 // main object, then the top level `Object` class is used as the scope). 735 // 736 // If a module of the given name is already defined in the scope, then that 737 // instance is just returned. 738 // 739 // If there is a class of the given name in the scope, then an error is 740 // generated instead (cannot have a class and module of same name in same scope). 741 // 742 // Otherwise, a new module is created in the scope with the given name, and that 743 // new instance is returned back (to be referenced at runtime). 744 // 745 // @param scope [Module, Class] class or module this definition is inside 746 // @param id [String] the name of the new (or existing) module 747 // 748 // @return [Module] 749 function $allocate_module(name) { 750 var constructor = function(){}; 751 var module = constructor; 752 753 if (name) 754 $prop(constructor, 'displayName', name+'.constructor'); 755 756 $prop(module, '$$name', name); 757 $prop(module, '$$prototype', constructor.prototype); 758 $prop(module, '$$const', {}); 759 $prop(module, '$$is_module', true); 760 $prop(module, '$$is_a_module', true); 761 $prop(module, '$$cvars', {}); 762 $prop(module, '$$iclasses', []); 763 $prop(module, '$$own_included_modules', []); 764 $prop(module, '$$own_prepended_modules', []); 765 $prop(module, '$$ancestors', [module]); 766 $prop(module, '$$ancestors_cache_version', null); 767 768 $set_proto(module, Opal.Module.prototype); 769 770 return module; 771 } Opal.allocate_module = $allocate_module; 772 773 function find_existing_module(scope, name) { 774 var module = const_get_name(scope, name); 775 if (module == null && scope === _Object) module = const_lookup_ancestors(_Object, name); 776 777 if (module) { 778 if (!module.$$is_module && module !== _Object) { 779 $raise(Opal.TypeError, name + " is not a module"); 780 } 781 } 782 783 return module; 784 } 785 786 Opal.module = function(scope, name) { 787 var module; 788 789 if (scope == null || scope == '::') { 790 // Global scope 791 scope = _Object; 792 } else if (!scope.$$is_class && !scope.$$is_module) { 793 // Scope is an object, use its class 794 scope = scope.$$class; 795 } 796 797 module = find_existing_module(scope, name); 798 799 if (module == null) { 800 // Module doesnt exist, create a new one... 801 module = $allocate_module(name); 802 $const_set(scope, name, module); 803 } 804 805 if (Opal.trace_class) { invoke_tracers_for_class(module); } 806 807 return module; 808 }; 809 810 // Return the singleton class for the passed object. 811 // 812 // If the given object alredy has a singleton class, then it will be stored on 813 // the object as the `$$meta` property. If this exists, then it is simply 814 // returned back. 815 // 816 // Otherwise, a new singleton object for the class or object is created, set on 817 // the object at `$$meta` for future use, and then returned. 818 // 819 // @param object [Object] the ruby object 820 // @return [Class] the singleton class for object 821 Opal.get_singleton_class = function(object) { 822 if (object.$$is_number) { 823 $raise(Opal.TypeError, "can't define singleton"); 824 } 825 if (object.$$meta) { 826 return object.$$meta; 827 } 828 829 if (object.hasOwnProperty('$$is_class')) { 830 return Opal.build_class_singleton_class(object); 831 } else if (object.hasOwnProperty('$$is_module')) { 832 return Opal.build_module_singleton_class(object); 833 } else { 834 return Opal.build_object_singleton_class(object); 835 } 836 }; 837 838 // helper to set $$meta on klass, module or instance 839 function set_meta(obj, meta) { 840 if (obj.hasOwnProperty('$$meta')) { 841 obj.$$meta = meta; 842 } else { 843 $prop(obj, '$$meta', meta); 844 } 845 if (obj.$$frozen) { 846 // If a object is frozen (sealed), freeze $$meta too. 847 // No need to inject $$meta.$$prototype in the prototype chain, 848 // as $$meta cannot be modified anyway. 849 obj.$$meta.$freeze(); 850 } else { 851 $set_proto(obj, meta.$$prototype); 852 } 853 } 854 // Build the singleton class for an existing class. Class object are built 855 // with their singleton class already in the prototype chain and inheriting 856 // from their superclass object (up to `Class` itself). 857 // 858 // NOTE: Actually in MRI a class' singleton class inherits from its 859 // superclass' singleton class which in turn inherits from Class. 860 // 861 // @param klass [Class] 862 // @return [Class] 863 Opal.build_class_singleton_class = function(klass) { 864 if (klass.$$meta) { 865 return klass.$$meta; 866 } 867 868 // The singleton_class superclass is the singleton_class of its superclass; 869 // but BasicObject has no superclass (its `$$super` is null), thus we 870 // fallback on `Class`. 871 var superclass = klass === BasicObject ? Class : Opal.get_singleton_class(klass.$$super); 872 873 var meta = $allocate_class(null, superclass, true); 874 875 $prop(meta, '$$is_singleton', true); 876 $prop(meta, '$$singleton_of', klass); 877 set_meta(klass, meta); 878 // Restoring ClassName.class 879 $prop(klass, '$$class', Opal.Class); 880 881 return meta; 882 }; 883 884 Opal.build_module_singleton_class = function(mod) { 885 if (mod.$$meta) { 886 return mod.$$meta; 887 } 888 889 var meta = $allocate_class(null, Opal.Module, true); 890 891 $prop(meta, '$$is_singleton', true); 892 $prop(meta, '$$singleton_of', mod); 893 set_meta(mod, meta); 894 // Restoring ModuleName.class 895 $prop(mod, '$$class', Opal.Module); 896 897 return meta; 898 }; 899 900 // Build the singleton class for a Ruby (non class) Object. 901 // 902 // @param object [Object] 903 // @return [Class] 904 Opal.build_object_singleton_class = function(object) { 905 var superclass = object.$$class, 906 klass = $allocate_class(nil, superclass, true); 907 908 $prop(klass, '$$is_singleton', true); 909 $prop(klass, '$$singleton_of', object); 910 911 delete klass.$$prototype.$$class; 912 913 set_meta(object, klass); 914 915 return klass; 916 }; 917 918 Opal.is_method = function(prop) { 919 return (prop[0] === '$' && prop[1] !== '$'); 920 }; 921 922 Opal.instance_methods = function(mod) { 923 var exclude = [], results = [], ancestors = $ancestors(mod); 924 925 for (var i = 0, l = ancestors.length; i < l; i++) { 926 var ancestor = ancestors[i], 927 proto = ancestor.$$prototype; 928 929 if (proto.hasOwnProperty('$$dummy')) { 930 proto = proto.$$define_methods_on; 931 } 932 933 var props = Object.getOwnPropertyNames(proto); 934 935 for (var j = 0, ll = props.length; j < ll; j++) { 936 var prop = props[j]; 937 938 if (Opal.is_method(prop)) { 939 var method_name = prop.slice(1), 940 method = proto[prop]; 941 942 if (method.$$stub && exclude.indexOf(method_name) === -1) { 943 exclude.push(method_name); 944 } 945 946 if (!method.$$stub && results.indexOf(method_name) === -1 && exclude.indexOf(method_name) === -1) { 947 results.push(method_name); 948 } 949 } 950 } 951 } 952 953 return results; 954 }; 955 956 Opal.own_instance_methods = function(mod) { 957 var results = [], 958 proto = mod.$$prototype; 959 960 if (proto.hasOwnProperty('$$dummy')) { 961 proto = proto.$$define_methods_on; 962 } 963 964 var props = Object.getOwnPropertyNames(proto); 965 966 for (var i = 0, length = props.length; i < length; i++) { 967 var prop = props[i]; 968 969 if (Opal.is_method(prop)) { 970 var method = proto[prop]; 971 972 if (!method.$$stub) { 973 var method_name = prop.slice(1); 974 results.push(method_name); 975 } 976 } 977 } 978 979 return results; 980 }; 981 982 Opal.methods = function(obj) { 983 return Opal.instance_methods(obj.$$meta || obj.$$class); 984 }; 985 986 Opal.own_methods = function(obj) { 987 return obj.$$meta ? Opal.own_instance_methods(obj.$$meta) : []; 988 }; 989 990 Opal.receiver_methods = function(obj) { 991 var mod = Opal.get_singleton_class(obj); 992 var singleton_methods = Opal.own_instance_methods(mod); 993 var instance_methods = Opal.own_instance_methods(mod.$$super); 994 return singleton_methods.concat(instance_methods); 995 }; 996 997 // Returns an object containing all pairs of names/values 998 // for all class variables defined in provided +module+ 999 // and its ancestors. 1000 // 1001 // @param module [Module] 1002 // @return [Object] 1003 Opal.class_variables = function(module) { 1004 var ancestors = $ancestors(module), 1005 i, length = ancestors.length, 1006 result = {}; 1007 1008 for (i = length - 1; i >= 0; i--) { 1009 var ancestor = ancestors[i]; 1010 1011 for (var cvar in ancestor.$$cvars) { 1012 result[cvar] = ancestor.$$cvars[cvar]; 1013 } 1014 } 1015 1016 return result; 1017 }; 1018 1019 // Sets class variable with specified +name+ to +value+ 1020 // in provided +module+ 1021 // 1022 // @param module [Module] 1023 // @param name [String] 1024 // @param value [Object] 1025 Opal.class_variable_set = function(module, name, value) { 1026 var ancestors = $ancestors(module), 1027 i, length = ancestors.length; 1028 1029 for (i = length - 2; i >= 0; i--) { 1030 var ancestor = ancestors[i]; 1031 1032 if ($has_own(ancestor.$$cvars, name)) { 1033 ancestor.$$cvars[name] = value; 1034 return value; 1035 } 1036 } 1037 1038 module.$$cvars[name] = value; 1039 1040 return value; 1041 }; 1042 1043 // Gets class variable with specified +name+ from provided +module+ 1044 // 1045 // @param module [Module] 1046 // @param name [String] 1047 Opal.class_variable_get = function(module, name, tolerant) { 1048 if ($has_own(module.$$cvars, name)) 1049 return module.$$cvars[name]; 1050 1051 var ancestors = $ancestors(module), 1052 i, length = ancestors.length; 1053 1054 for (i = 0; i < length; i++) { 1055 var ancestor = ancestors[i]; 1056 1057 if ($has_own(ancestor.$$cvars, name)) { 1058 return ancestor.$$cvars[name]; 1059 } 1060 } 1061 1062 if (!tolerant) 1063 $raise(Opal.NameError, 'uninitialized class variable '+name+' in '+module.$name()); 1064 1065 return nil; 1066 }; 1067 1068 function isRoot(proto) { 1069 return proto.hasOwnProperty('$$iclass') && proto.hasOwnProperty('$$root'); 1070 } 1071 1072 function own_included_modules(module) { 1073 var result = [], mod, proto = Object.getPrototypeOf(module.$$prototype); 1074 1075 while (proto) { 1076 if (proto.hasOwnProperty('$$class')) { 1077 // superclass 1078 break; 1079 } 1080 mod = protoToModule(proto); 1081 if (mod) { 1082 result.push(mod); 1083 } 1084 proto = Object.getPrototypeOf(proto); 1085 } 1086 1087 return result; 1088 } 1089 1090 function own_prepended_modules(module) { 1091 var result = [], mod, proto = Object.getPrototypeOf(module.$$prototype); 1092 1093 if (module.$$prototype.hasOwnProperty('$$dummy')) { 1094 while (proto) { 1095 if (proto === module.$$prototype.$$define_methods_on) { 1096 break; 1097 } 1098 1099 mod = protoToModule(proto); 1100 if (mod) { 1101 result.push(mod); 1102 } 1103 1104 proto = Object.getPrototypeOf(proto); 1105 } 1106 } 1107 1108 return result; 1109 } 1110 1111 1112 // The actual inclusion of a module into a class. 1113 // 1114 // ## Class `$$parent` and `iclass` 1115 // 1116 // To handle `super` calls, every class has a `$$parent`. This parent is 1117 // used to resolve the next class for a super call. A normal class would 1118 // have this point to its superclass. However, if a class includes a module 1119 // then this would need to take into account the module. The module would 1120 // also have to then point its `$$parent` to the actual superclass. We 1121 // cannot modify modules like this, because it might be included in more 1122 // then one class. To fix this, we actually insert an `iclass` as the class' 1123 // `$$parent` which can then point to the superclass. The `iclass` acts as 1124 // a proxy to the actual module, so the `super` chain can then search it for 1125 // the required method. 1126 // 1127 // @param module [Module] the module to include 1128 // @param includer [Module] the target class to include module into 1129 // @return [null] 1130 Opal.append_features = function(module, includer) { 1131 var module_ancestors = $ancestors(module); 1132 var iclasses = []; 1133 1134 if (module_ancestors.indexOf(includer) !== -1) { 1135 $raise(Opal.ArgumentError, 'cyclic include detected'); 1136 } 1137 1138 for (var i = 0, length = module_ancestors.length; i < length; i++) { 1139 var ancestor = module_ancestors[i], iclass = create_iclass(ancestor); 1140 $prop(iclass, '$$included', true); 1141 iclasses.push(iclass); 1142 } 1143 var includer_ancestors = $ancestors(includer), 1144 chain = chain_iclasses(iclasses), 1145 start_chain_after, 1146 end_chain_on; 1147 1148 if (includer_ancestors.indexOf(module) === -1) { 1149 // first time include 1150 1151 // includer -> chain.first -> ...chain... -> chain.last -> includer.parent 1152 start_chain_after = includer.$$prototype; 1153 end_chain_on = Object.getPrototypeOf(includer.$$prototype); 1154 } else { 1155 // The module has been already included, 1156 // we don't need to put it into the ancestors chain again, 1157 // but this module may have new included modules. 1158 // If it's true we need to copy them. 1159 // 1160 // The simplest way is to replace ancestors chain from 1161 // parent 1162 // | 1163 // `module` iclass (has a $$root flag) 1164 // | 1165 // ...previos chain of module.included_modules ... 1166 // | 1167 // "next ancestor" (has a $$root flag or is a real class) 1168 // 1169 // to 1170 // parent 1171 // | 1172 // `module` iclass (has a $$root flag) 1173 // | 1174 // ...regenerated chain of module.included_modules 1175 // | 1176 // "next ancestor" (has a $$root flag or is a real class) 1177 // 1178 // because there are no intermediate classes between `parent` and `next ancestor`. 1179 // It doesn't break any prototypes of other objects as we don't change class references. 1180 1181 var parent = includer.$$prototype, module_iclass = Object.getPrototypeOf(parent); 1182 1183 while (module_iclass != null) { 1184 if (module_iclass.$$module === module && isRoot(module_iclass)) { 1185 break; 1186 } 1187 1188 parent = module_iclass; 1189 module_iclass = Object.getPrototypeOf(module_iclass); 1190 } 1191 1192 if (module_iclass) { 1193 // module has been directly included 1194 var next_ancestor = Object.getPrototypeOf(module_iclass); 1195 1196 // skip non-root iclasses (that were recursively included) 1197 while (next_ancestor.hasOwnProperty('$$iclass') && !isRoot(next_ancestor)) { 1198 next_ancestor = Object.getPrototypeOf(next_ancestor); 1199 } 1200 1201 start_chain_after = parent; 1202 end_chain_on = next_ancestor; 1203 } else { 1204 // module has not been directly included but was in ancestor chain because it was included by another module 1205 // include it directly 1206 start_chain_after = includer.$$prototype; 1207 end_chain_on = Object.getPrototypeOf(includer.$$prototype); 1208 } 1209 } 1210 1211 $set_proto(start_chain_after, chain.first); 1212 $set_proto(chain.last, end_chain_on); 1213 1214 // recalculate own_included_modules cache 1215 includer.$$own_included_modules = own_included_modules(includer); 1216 1217 Opal.const_cache_version++; 1218 }; 1219 1220 Opal.prepend_features = function(module, prepender) { 1221 // Here we change the ancestors chain from 1222 // 1223 // prepender 1224 // | 1225 // parent 1226 // 1227 // to: 1228 // 1229 // dummy(prepender) 1230 // | 1231 // iclass(module) 1232 // | 1233 // iclass(prepender) 1234 // | 1235 // parent 1236 var module_ancestors = $ancestors(module); 1237 var iclasses = []; 1238 1239 if (module_ancestors.indexOf(prepender) !== -1) { 1240 $raise(Opal.ArgumentError, 'cyclic prepend detected'); 1241 } 1242 1243 for (var i = 0, length = module_ancestors.length; i < length; i++) { 1244 var ancestor = module_ancestors[i], iclass = create_iclass(ancestor); 1245 $prop(iclass, '$$prepended', true); 1246 iclasses.push(iclass); 1247 } 1248 1249 var chain = chain_iclasses(iclasses), 1250 dummy_prepender = prepender.$$prototype, 1251 previous_parent = Object.getPrototypeOf(dummy_prepender), 1252 prepender_iclass, 1253 start_chain_after, 1254 end_chain_on; 1255 1256 if (dummy_prepender.hasOwnProperty('$$dummy')) { 1257 // The module already has some prepended modules 1258 // which means that we don't need to make it "dummy" 1259 prepender_iclass = dummy_prepender.$$define_methods_on; 1260 } else { 1261 // Making the module "dummy" 1262 prepender_iclass = create_dummy_iclass(prepender); 1263 flush_methods_in(prepender); 1264 $prop(dummy_prepender, '$$dummy', true); 1265 $prop(dummy_prepender, '$$define_methods_on', prepender_iclass); 1266 1267 // Converting 1268 // dummy(prepender) -> previous_parent 1269 // to 1270 // dummy(prepender) -> iclass(prepender) -> previous_parent 1271 $set_proto(dummy_prepender, prepender_iclass); 1272 $set_proto(prepender_iclass, previous_parent); 1273 } 1274 1275 var prepender_ancestors = $ancestors(prepender); 1276 1277 if (prepender_ancestors.indexOf(module) === -1) { 1278 // first time prepend 1279 1280 start_chain_after = dummy_prepender; 1281 1282 // next $$root or prepender_iclass or non-$$iclass 1283 end_chain_on = Object.getPrototypeOf(dummy_prepender); 1284 while (end_chain_on != null) { 1285 if ( 1286 end_chain_on.hasOwnProperty('$$root') || 1287 end_chain_on === prepender_iclass || 1288 !end_chain_on.hasOwnProperty('$$iclass') 1289 ) { 1290 break; 1291 } 1292 1293 end_chain_on = Object.getPrototypeOf(end_chain_on); 1294 } 1295 } else { 1296 $raise(Opal.RuntimeError, "Prepending a module multiple times is not supported"); 1297 } 1298 1299 $set_proto(start_chain_after, chain.first); 1300 $set_proto(chain.last, end_chain_on); 1301 1302 // recalculate own_prepended_modules cache 1303 prepender.$$own_prepended_modules = own_prepended_modules(prepender); 1304 1305 Opal.const_cache_version++; 1306 }; 1307 1308 function flush_methods_in(module) { 1309 var proto = module.$$prototype, 1310 props = Object.getOwnPropertyNames(proto); 1311 1312 for (var i = 0; i < props.length; i++) { 1313 var prop = props[i]; 1314 if (Opal.is_method(prop)) { 1315 delete proto[prop]; 1316 } 1317 } 1318 } 1319 1320 function create_iclass(module) { 1321 var iclass = create_dummy_iclass(module); 1322 1323 if (module.$$is_module) { 1324 module.$$iclasses.push(iclass); 1325 } 1326 1327 return iclass; 1328 } 1329 1330 // Dummy iclass doesn't receive updates when the module gets a new method. 1331 function create_dummy_iclass(module) { 1332 var iclass = {}, 1333 proto = module.$$prototype; 1334 1335 if (proto.hasOwnProperty('$$dummy')) { 1336 proto = proto.$$define_methods_on; 1337 } 1338 1339 var props = Object.getOwnPropertyNames(proto), 1340 length = props.length, i; 1341 1342 for (i = 0; i < length; i++) { 1343 var prop = props[i]; 1344 $prop(iclass, prop, proto[prop]); 1345 } 1346 1347 $prop(iclass, '$$iclass', true); 1348 $prop(iclass, '$$module', module); 1349 1350 return iclass; 1351 } 1352 1353 function chain_iclasses(iclasses) { 1354 var length = iclasses.length, first = iclasses[0]; 1355 1356 $prop(first, '$$root', true); 1357 1358 if (length === 1) { 1359 return { first: first, last: first }; 1360 } 1361 1362 var previous = first; 1363 1364 for (var i = 1; i < length; i++) { 1365 var current = iclasses[i]; 1366 $set_proto(previous, current); 1367 previous = current; 1368 } 1369 1370 1371 return { first: iclasses[0], last: iclasses[length - 1] }; 1372 } 1373 1374 // For performance, some core Ruby classes are toll-free bridged to their 1375 // native JavaScript counterparts (e.g. a Ruby Array is a JavaScript Array). 1376 // 1377 // This method is used to setup a native constructor (e.g. Array), to have 1378 // its prototype act like a normal Ruby class. Firstly, a new Ruby class is 1379 // created using the native constructor so that its prototype is set as the 1380 // target for the new class. Note: all bridged classes are set to inherit 1381 // from Object. 1382 // 1383 // Example: 1384 // 1385 // Opal.bridge(self, Function); 1386 // 1387 // @param klass [Class] the Ruby class to bridge 1388 // @param constructor [JS.Function] native JavaScript constructor to use 1389 // @return [Class] returns the passed Ruby class 1390 // 1391 Opal.bridge = function(native_klass, klass) { 1392 if (native_klass.hasOwnProperty('$$bridge')) { 1393 $raise(Opal.ArgumentError, "already bridged"); 1394 } 1395 1396 // constructor is a JS function with a prototype chain like: 1397 // - constructor 1398 // - super 1399 // 1400 // What we need to do is to inject our class (with its prototype chain) 1401 // between constructor and super. For example, after injecting ::Object 1402 // into JS String we get: 1403 // 1404 // - constructor (window.String) 1405 // - Opal.Object 1406 // - Opal.Kernel 1407 // - Opal.BasicObject 1408 // - super (window.Object) 1409 // - null 1410 // 1411 $prop(native_klass, '$$bridge', klass); 1412 $set_proto(native_klass.prototype, (klass.$$super || Opal.Object).$$prototype); 1413 $prop(klass, '$$prototype', native_klass.prototype); 1414 1415 $prop(klass.$$prototype, '$$class', klass); 1416 $prop(klass, '$$constructor', native_klass); 1417 $prop(klass, '$$bridge', true); 1418 }; 1419 1420 function protoToModule(proto) { 1421 if (proto.hasOwnProperty('$$dummy')) { 1422 return; 1423 } else if (proto.hasOwnProperty('$$iclass')) { 1424 return proto.$$module; 1425 } else if (proto.hasOwnProperty('$$class')) { 1426 return proto.$$class; 1427 } 1428 } 1429 1430 function own_ancestors(module) { 1431 return module.$$own_prepended_modules.concat([module]).concat(module.$$own_included_modules); 1432 } 1433 1434 // The Array of ancestors for a given module/class 1435 function $ancestors(module) { 1436 if (!module) { return []; } 1437 1438 if (module.$$ancestors_cache_version === Opal.const_cache_version) { 1439 return module.$$ancestors; 1440 } 1441 1442 var result = [], i, mods, length; 1443 1444 for (i = 0, mods = own_ancestors(module), length = mods.length; i < length; i++) { 1445 result.push(mods[i]); 1446 } 1447 1448 if (module.$$super) { 1449 for (i = 0, mods = $ancestors(module.$$super), length = mods.length; i < length; i++) { 1450 result.push(mods[i]); 1451 } 1452 } 1453 1454 module.$$ancestors_cache_version = Opal.const_cache_version; 1455 module.$$ancestors = result; 1456 1457 return result; 1458 } Opal.ancestors = $ancestors; 1459 1460 Opal.included_modules = function(module) { 1461 var result = [], mod = null, proto = Object.getPrototypeOf(module.$$prototype); 1462 1463 for (; proto && Object.getPrototypeOf(proto); proto = Object.getPrototypeOf(proto)) { 1464 mod = protoToModule(proto); 1465 if (mod && mod.$$is_module && proto.$$iclass && proto.$$included) { 1466 result.push(mod); 1467 } 1468 } 1469 1470 return result; 1471 }; 1472 1473 1474 // Method Missing 1475 // -------------- 1476 1477 // Methods stubs are used to facilitate method_missing in opal. A stub is a 1478 // placeholder function which just calls `method_missing` on the receiver. 1479 // If no method with the given name is actually defined on an object, then it 1480 // is obvious to say that the stub will be called instead, and then in turn 1481 // method_missing will be called. 1482 // 1483 // When a file in ruby gets compiled to javascript, it includes a call to 1484 // this function which adds stubs for every method name in the compiled file. 1485 // It should then be safe to assume that method_missing will work for any 1486 // method call detected. 1487 // 1488 // Method stubs are added to the BasicObject prototype, which every other 1489 // ruby object inherits, so all objects should handle method missing. A stub 1490 // is only added if the given property name (method name) is not already 1491 // defined. 1492 // 1493 // Note: all ruby methods have a `$` prefix in javascript, so all stubs will 1494 // have this prefix as well (to make this method more performant). 1495 // 1496 // Opal.add_stubs("foo,bar,baz="); 1497 // 1498 // All stub functions will have a private `$$stub` property set to true so 1499 // that other internal methods can detect if a method is just a stub or not. 1500 // `Kernel#respond_to?` uses this property to detect a methods presence. 1501 // 1502 // @param stubs [Array] an array of method stubs to add 1503 // @return [undefined] 1504 Opal.add_stubs = function(stubs) { 1505 var proto = Opal.BasicObject.$$prototype; 1506 var stub, existing_method; 1507 stubs = stubs.split(','); 1508 1509 for (var i = 0, length = stubs.length; i < length; i++) { 1510 stub = $jsid(stubs[i]), existing_method = proto[stub]; 1511 1512 if (existing_method == null || existing_method.$$stub) { 1513 Opal.add_stub_for(proto, stub); 1514 } 1515 } 1516 }; 1517 1518 // Add a method_missing stub function to the given prototype for the 1519 // given name. 1520 // 1521 // @param prototype [Prototype] the target prototype 1522 // @param stub [String] stub name to add (e.g. "$foo") 1523 // @return [undefined] 1524 Opal.add_stub_for = function(prototype, stub) { 1525 // Opal.stub_for(stub) is the method_missing_stub 1526 $prop(prototype, stub, Opal.stub_for(stub)); 1527 }; 1528 1529 // Generate the method_missing stub for a given method name. 1530 // 1531 // @param method_name [String] The js-name of the method to stub (e.g. "$foo") 1532 // @return [undefined] 1533 Opal.stub_for = function(method_name) { 1534 1535 function method_missing_stub() { 1536 // Copy any given block onto the method_missing dispatcher 1537 this.$method_missing.$$p = method_missing_stub.$$p; 1538 1539 // Set block property to null ready for the next call (stop false-positives) 1540 method_missing_stub.$$p = null; 1541 1542 // call method missing with correct args (remove '$' prefix on method name) 1543 var args_ary = new Array(arguments.length); 1544 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = arguments[i]; } 1545 1546 return this.$method_missing.apply(this, [method_name.slice(1)].concat(args_ary)); 1547 } 1548 1549 method_missing_stub.$$stub = true; 1550 1551 return method_missing_stub; 1552 }; 1553 1554 1555 // Methods 1556 // ------- 1557 1558 // Arity count error dispatcher for methods 1559 // 1560 // @param actual [Fixnum] number of arguments given to method 1561 // @param expected [Fixnum] expected number of arguments 1562 // @param object [Object] owner of the method +meth+ 1563 // @param meth [String] method name that got wrong number of arguments 1564 // @raise [ArgumentError] 1565 Opal.ac = function(actual, expected, object, meth) { 1566 var inspect = ''; 1567 if (object.$$is_a_module) { 1568 inspect += object.$$name + '.'; 1569 } 1570 else { 1571 inspect += object.$$class.$$name + '#'; 1572 } 1573 inspect += meth; 1574 1575 $raise(Opal.ArgumentError, '[' + inspect + '] wrong number of arguments (given ' + actual + ', expected ' + expected + ')'); 1576 }; 1577 1578 // Arity count error dispatcher for blocks 1579 // 1580 // @param actual [Fixnum] number of arguments given to block 1581 // @param expected [Fixnum] expected number of arguments 1582 // @param context [Object] context of the block definition 1583 // @raise [ArgumentError] 1584 Opal.block_ac = function(actual, expected, context) { 1585 var inspect = "`block in " + context + "'"; 1586 1587 $raise(Opal.ArgumentError, inspect + ': wrong number of arguments (given ' + actual + ', expected ' + expected + ')'); 1588 }; 1589 1590 function get_ancestors(obj) { 1591 if (obj.hasOwnProperty('$$meta') && obj.$$meta !== null) { 1592 return $ancestors(obj.$$meta); 1593 } else { 1594 return $ancestors(obj.$$class); 1595 } 1596 } 1597 // Super dispatcher 1598 Opal.find_super = function(obj, mid, current_func, defcheck, allow_stubs) { 1599 var jsid = $jsid(mid), ancestors, super_method; 1600 1601 ancestors = get_ancestors(obj); 1602 1603 var current_index = ancestors.indexOf(current_func.$$owner); 1604 1605 for (var i = current_index + 1; i < ancestors.length; i++) { 1606 var ancestor = ancestors[i], 1607 proto = ancestor.$$prototype; 1608 1609 if (proto.hasOwnProperty('$$dummy')) { 1610 proto = proto.$$define_methods_on; 1611 } 1612 1613 if (proto.hasOwnProperty(jsid)) { 1614 super_method = proto[jsid]; 1615 break; 1616 } 1617 } 1618 1619 if (!defcheck && super_method && super_method.$$stub && obj.$method_missing.$$pristine) { 1620 // method_missing hasn't been explicitly defined 1621 $raise(Opal.NoMethodError, 'super: no superclass method `'+mid+"' for "+obj, mid); 1622 } 1623 1624 return (super_method.$$stub && !allow_stubs) ? null : super_method; 1625 }; 1626 1627 // Iter dispatcher for super in a block 1628 Opal.find_block_super = function(obj, jsid, current_func, defcheck, implicit) { 1629 var call_jsid = jsid; 1630 1631 if (!current_func) { 1632 $raise(Opal.RuntimeError, "super called outside of method"); 1633 } 1634 1635 if (implicit && current_func.$$define_meth) { 1636 $raise(Opal.RuntimeError, 1637 "implicit argument passing of super from method defined by define_method() is not supported. " + 1638 "Specify all arguments explicitly" 1639 ); 1640 } 1641 1642 if (current_func.$$def) { 1643 call_jsid = current_func.$$jsid; 1644 } 1645 1646 return Opal.find_super(obj, call_jsid, current_func, defcheck); 1647 }; 1648 1649 // @deprecated 1650 Opal.find_super_dispatcher = Opal.find_super; 1651 1652 // @deprecated 1653 Opal.find_iter_super_dispatcher = Opal.find_block_super; 1654 1655 // handles yield calls for 1 yielded arg 1656 Opal.yield1 = function(block, arg) { 1657 if (typeof(block) !== "function") { 1658 $raise(Opal.LocalJumpError, "no block given"); 1659 } 1660 1661 var has_mlhs = block.$$has_top_level_mlhs_arg, 1662 has_trailing_comma = block.$$has_trailing_comma_in_args; 1663 1664 if (block.length > 1 || ((has_mlhs || has_trailing_comma) && block.length === 1)) { 1665 arg = Opal.to_ary(arg); 1666 } 1667 1668 if ((block.length > 1 || (has_trailing_comma && block.length === 1)) && arg.$$is_array) { 1669 return block.apply(null, arg); 1670 } 1671 else { 1672 return block(arg); 1673 } 1674 }; 1675 1676 // handles yield for > 1 yielded arg 1677 Opal.yieldX = function(block, args) { 1678 if (typeof(block) !== "function") { 1679 $raise(Opal.LocalJumpError, "no block given"); 1680 } 1681 1682 if (block.length > 1 && args.length === 1) { 1683 if (args[0].$$is_array) { 1684 return block.apply(null, args[0]); 1685 } 1686 } 1687 1688 if (!args.$$is_array) { 1689 var args_ary = new Array(args.length); 1690 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } 1691 1692 return block.apply(null, args_ary); 1693 } 1694 1695 return block.apply(null, args); 1696 }; 1697 1698 // Finds the corresponding exception match in candidates. Each candidate can 1699 // be a value, or an array of values. Returns null if not found. 1700 Opal.rescue = function(exception, candidates) { 1701 for (var i = 0; i < candidates.length; i++) { 1702 var candidate = candidates[i]; 1703 1704 if (candidate.$$is_array) { 1705 var result = Opal.rescue(exception, candidate); 1706 1707 if (result) { 1708 return result; 1709 } 1710 } 1711 else if (candidate === Opal.JS.Error || candidate['$==='](exception)) { 1712 return candidate; 1713 } 1714 } 1715 1716 return null; 1717 }; 1718 1719 Opal.is_a = function(object, klass) { 1720 if (klass != null && object.$$meta === klass || object.$$class === klass) { 1721 return true; 1722 } 1723 1724 if (object.$$is_number && klass.$$is_number_class) { 1725 return (klass.$$is_integer_class) ? (object % 1) === 0 : true; 1726 } 1727 1728 var ancestors = $ancestors(object.$$is_class ? Opal.get_singleton_class(object) : (object.$$meta || object.$$class)); 1729 1730 return ancestors.indexOf(klass) !== -1; 1731 }; 1732 1733 // Helpers for extracting kwsplats 1734 // Used for: { **h } 1735 Opal.to_hash = function(value) { 1736 if (value.$$is_hash) { 1737 return value; 1738 } 1739 else if (value['$respond_to?']('to_hash', true)) { 1740 var hash = value.$to_hash(); 1741 if (hash.$$is_hash) { 1742 return hash; 1743 } 1744 else { 1745 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1746 " to Hash (" + value.$$class + "#to_hash gives " + hash.$$class + ")"); 1747 } 1748 } 1749 else { 1750 $raise(Opal.TypeError, "no implicit conversion of " + value.$$class + " into Hash"); 1751 } 1752 }; 1753 1754 // Helpers for implementing multiple assignment 1755 // Our code for extracting the values and assigning them only works if the 1756 // return value is a JS array. 1757 // So if we get an Array subclass, extract the wrapped JS array from it 1758 1759 // Used for: a, b = something (no splat) 1760 Opal.to_ary = function(value) { 1761 if (value.$$is_array) { 1762 return value; 1763 } 1764 else if (value['$respond_to?']('to_ary', true)) { 1765 var ary = value.$to_ary(); 1766 if (ary === nil) { 1767 return [value]; 1768 } 1769 else if (ary.$$is_array) { 1770 return ary; 1771 } 1772 else { 1773 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1774 " to Array (" + value.$$class + "#to_ary gives " + ary.$$class + ")"); 1775 } 1776 } 1777 else { 1778 return [value]; 1779 } 1780 }; 1781 1782 // Used for: a, b = *something (with splat) 1783 Opal.to_a = function(value) { 1784 if (value.$$is_array) { 1785 // A splatted array must be copied 1786 return value.slice(); 1787 } 1788 else if (value['$respond_to?']('to_a', true)) { 1789 var ary = value.$to_a(); 1790 if (ary === nil) { 1791 return [value]; 1792 } 1793 else if (ary.$$is_array) { 1794 return ary; 1795 } 1796 else { 1797 $raise(Opal.TypeError, "Can't convert " + value.$$class + 1798 " to Array (" + value.$$class + "#to_a gives " + ary.$$class + ")"); 1799 } 1800 } 1801 else { 1802 return [value]; 1803 } 1804 }; 1805 1806 // Used for extracting keyword arguments from arguments passed to 1807 // JS function. If provided +arguments+ list doesn't have a Hash 1808 // as a last item, returns a blank Hash. 1809 // 1810 // @param parameters [Array] 1811 // @return [Hash] 1812 // 1813 Opal.extract_kwargs = function(parameters) { 1814 var kwargs = parameters[parameters.length - 1]; 1815 if (kwargs != null && Opal.respond_to(kwargs, '$to_hash', true)) { 1816 $splice(parameters, parameters.length - 1); 1817 return kwargs; 1818 } 1819 }; 1820 1821 // Used to get a list of rest keyword arguments. Method takes the given 1822 // keyword args, i.e. the hash literal passed to the method containing all 1823 // keyword arguemnts passed to method, as well as the used args which are 1824 // the names of required and optional arguments defined. This method then 1825 // just returns all key/value pairs which have not been used, in a new 1826 // hash literal. 1827 // 1828 // @param given_args [Hash] all kwargs given to method 1829 // @param used_args [Object<String: true>] all keys used as named kwargs 1830 // @return [Hash] 1831 // 1832 Opal.kwrestargs = function(given_args, used_args) { 1833 var keys = [], 1834 map = {}, 1835 key , 1836 given_map = given_args.$$smap; 1837 1838 for (key in given_map) { 1839 if (!used_args[key]) { 1840 keys.push(key); 1841 map[key] = given_map[key]; 1842 } 1843 } 1844 1845 return Opal.hash2(keys, map); 1846 }; 1847 1848 function apply_blockopts(block, blockopts) { 1849 if (typeof(blockopts) === 'number') { 1850 block.$$arity = blockopts; 1851 } 1852 else if (typeof(blockopts) === 'object') { 1853 Object.assign(block, blockopts); 1854 } 1855 } 1856 1857 // Optimization for a costly operation of prepending '$' to method names 1858 var jsid_cache = {}; 1859 function $jsid(name) { 1860 return jsid_cache[name] || (jsid_cache[name] = '$' + name); 1861 } 1862 Opal.jsid = $jsid; 1863 1864 // Calls passed method on a ruby object with arguments and block: 1865 // 1866 // Can take a method or a method name. 1867 // 1868 // 1. When method name gets passed it invokes it by its name 1869 // and calls 'method_missing' when object doesn't have this method. 1870 // Used internally by Opal to invoke method that takes a block or a splat. 1871 // 2. When method (i.e. method body) gets passed, it doesn't trigger 'method_missing' 1872 // because it doesn't know the name of the actual method. 1873 // Used internally by Opal to invoke 'super'. 1874 // 1875 // @example 1876 // var my_array = [1, 2, 3, 4] 1877 // Opal.send(my_array, 'length') # => 4 1878 // Opal.send(my_array, my_array.$length) # => 4 1879 // 1880 // Opal.send(my_array, 'reverse!') # => [4, 3, 2, 1] 1881 // Opal.send(my_array, my_array['$reverse!']') # => [4, 3, 2, 1] 1882 // 1883 // @param recv [Object] ruby object 1884 // @param method [Function, String] method body or name of the method 1885 // @param args [Array] arguments that will be passed to the method call 1886 // @param block [Function] ruby block 1887 // @param blockopts [Object, Number] optional properties to set on the block 1888 // @return [Object] returning value of the method call 1889 Opal.send = function(recv, method, args, block, blockopts) { 1890 var body; 1891 1892 if (typeof(method) === 'function') { 1893 body = method; 1894 method = null; 1895 } else if (typeof(method) === 'string') { 1896 body = recv[$jsid(method)]; 1897 } else { 1898 $raise(Opal.NameError, "Passed method should be a string or a function"); 1899 } 1900 1901 return Opal.send2(recv, body, method, args, block, blockopts); 1902 }; 1903 1904 Opal.send2 = function(recv, body, method, args, block, blockopts) { 1905 if (body == null && method != null && recv.$method_missing) { 1906 body = recv.$method_missing; 1907 args = [method].concat(args); 1908 } 1909 1910 apply_blockopts(block, blockopts); 1911 1912 if (typeof block === 'function') body.$$p = block; 1913 return body.apply(recv, args); 1914 }; 1915 1916 Opal.refined_send = function(refinement_groups, recv, method, args, block, blockopts) { 1917 var i, j, k, ancestors, ancestor, refinements, refinement, refine_modules, refine_module, body; 1918 1919 ancestors = get_ancestors(recv); 1920 1921 // For all ancestors that there are, starting from the closest to the furthest... 1922 for (i = 0; i < ancestors.length; i++) { 1923 ancestor = Opal.id(ancestors[i]); 1924 1925 // For all refinement groups there are, starting from the closest scope to the furthest... 1926 for (j = 0; j < refinement_groups.length; j++) { 1927 refinements = refinement_groups[j]; 1928 1929 // For all refinements there are, starting from the last `using` call to the furthest... 1930 for (k = refinements.length - 1; k >= 0; k--) { 1931 refinement = refinements[k]; 1932 if (typeof refinement.$$refine_modules === 'undefined') continue; 1933 1934 // A single module being given as an argument of the `using` call contains multiple 1935 // refinement modules 1936 refine_modules = refinement.$$refine_modules; 1937 1938 // Does this module refine a given call for a given ancestor module? 1939 if (typeof refine_modules[ancestor] === 'undefined') continue; 1940 refine_module = refine_modules[ancestor]; 1941 1942 // Does this module define a method we want to call? 1943 if (typeof refine_module.$$prototype[$jsid(method)] !== 'undefined') { 1944 body = refine_module.$$prototype[$jsid(method)]; 1945 return Opal.send2(recv, body, method, args, block, blockopts); 1946 } 1947 } 1948 } 1949 } 1950 1951 return Opal.send(recv, method, args, block, blockopts); 1952 }; 1953 1954 Opal.lambda = function(block, blockopts) { 1955 block.$$is_lambda = true; 1956 1957 apply_blockopts(block, blockopts); 1958 1959 return block; 1960 }; 1961 1962 // Used to define methods on an object. This is a helper method, used by the 1963 // compiled source to define methods on special case objects when the compiler 1964 // can not determine the destination object, or the object is a Module 1965 // instance. This can get called by `Module#define_method` as well. 1966 // 1967 // ## Modules 1968 // 1969 // Any method defined on a module will come through this runtime helper. 1970 // The method is added to the module body, and the owner of the method is 1971 // set to be the module itself. This is used later when choosing which 1972 // method should show on a class if more than 1 included modules define 1973 // the same method. Finally, if the module is in `module_function` mode, 1974 // then the method is also defined onto the module itself. 1975 // 1976 // ## Classes 1977 // 1978 // This helper will only be called for classes when a method is being 1979 // defined indirectly; either through `Module#define_method`, or by a 1980 // literal `def` method inside an `instance_eval` or `class_eval` body. In 1981 // either case, the method is simply added to the class' prototype. A special 1982 // exception exists for `BasicObject` and `Object`. These two classes are 1983 // special because they are used in toll-free bridged classes. In each of 1984 // these two cases, extra work is required to define the methods on toll-free 1985 // bridged class' prototypes as well. 1986 // 1987 // ## Objects 1988 // 1989 // If a simple ruby object is the object, then the method is simply just 1990 // defined on the object as a singleton method. This would be the case when 1991 // a method is defined inside an `instance_eval` block. 1992 // 1993 // @param obj [Object, Class] the actual obj to define method for 1994 // @param jsid [String] the JavaScript friendly method name (e.g. '$foo') 1995 // @param body [JS.Function] the literal JavaScript function used as method 1996 // @param blockopts [Object, Number] optional properties to set on the body 1997 // @return [null] 1998 // 1999 Opal.def = function(obj, jsid, body, blockopts) { 2000 apply_blockopts(body, blockopts); 2001 2002 // Special case for a method definition in the 2003 // top-level namespace 2004 if (obj === Opal.top) { 2005 return Opal.defn(Opal.Object, jsid, body); 2006 } 2007 // if instance_eval is invoked on a module/class, it sets inst_eval_mod 2008 else if (!obj.$$eval && obj.$$is_a_module) { 2009 return Opal.defn(obj, jsid, body); 2010 } 2011 else { 2012 return Opal.defs(obj, jsid, body); 2013 } 2014 }; 2015 2016 // Define method on a module or class (see Opal.def). 2017 Opal.defn = function(module, jsid, body) { 2018 $deny_frozen_access(module); 2019 2020 body.displayName = jsid; 2021 body.$$owner = module; 2022 2023 var name = jsid.substr(1); 2024 2025 var proto = module.$$prototype; 2026 if (proto.hasOwnProperty('$$dummy')) { 2027 proto = proto.$$define_methods_on; 2028 } 2029 $prop(proto, jsid, body); 2030 2031 if (module.$$is_module) { 2032 if (module.$$module_function) { 2033 Opal.defs(module, jsid, body); 2034 } 2035 2036 for (var i = 0, iclasses = module.$$iclasses, length = iclasses.length; i < length; i++) { 2037 var iclass = iclasses[i]; 2038 $prop(iclass, jsid, body); 2039 } 2040 } 2041 2042 var singleton_of = module.$$singleton_of; 2043 if (module.$method_added && !module.$method_added.$$stub && !singleton_of) { 2044 module.$method_added(name); 2045 } 2046 else if (singleton_of && singleton_of.$singleton_method_added && !singleton_of.$singleton_method_added.$$stub) { 2047 singleton_of.$singleton_method_added(name); 2048 } 2049 2050 return name; 2051 }; 2052 2053 // Define a singleton method on the given object (see Opal.def). 2054 Opal.defs = function(obj, jsid, body, blockopts) { 2055 apply_blockopts(body, blockopts); 2056 2057 if (obj.$$is_string || obj.$$is_number) { 2058 $raise(Opal.TypeError, "can't define singleton"); 2059 } 2060 return Opal.defn(Opal.get_singleton_class(obj), jsid, body); 2061 }; 2062 2063 // Called from #remove_method. 2064 Opal.rdef = function(obj, jsid) { 2065 if (!$has_own(obj.$$prototype, jsid)) { 2066 $raise(Opal.NameError, "method '" + jsid.substr(1) + "' not defined in " + obj.$name()); 2067 } 2068 2069 delete obj.$$prototype[jsid]; 2070 2071 if (obj.$$is_singleton) { 2072 if (obj.$$prototype.$singleton_method_removed && !obj.$$prototype.$singleton_method_removed.$$stub) { 2073 obj.$$prototype.$singleton_method_removed(jsid.substr(1)); 2074 } 2075 } 2076 else { 2077 if (obj.$method_removed && !obj.$method_removed.$$stub) { 2078 obj.$method_removed(jsid.substr(1)); 2079 } 2080 } 2081 }; 2082 2083 // Called from #undef_method. 2084 Opal.udef = function(obj, jsid) { 2085 if (!obj.$$prototype[jsid] || obj.$$prototype[jsid].$$stub) { 2086 $raise(Opal.NameError, "method '" + jsid.substr(1) + "' not defined in " + obj.$name()); 2087 } 2088 2089 Opal.add_stub_for(obj.$$prototype, jsid); 2090 2091 if (obj.$$is_singleton) { 2092 if (obj.$$prototype.$singleton_method_undefined && !obj.$$prototype.$singleton_method_undefined.$$stub) { 2093 obj.$$prototype.$singleton_method_undefined(jsid.substr(1)); 2094 } 2095 } 2096 else { 2097 if (obj.$method_undefined && !obj.$method_undefined.$$stub) { 2098 obj.$method_undefined(jsid.substr(1)); 2099 } 2100 } 2101 }; 2102 2103 function is_method_body(body) { 2104 return (typeof(body) === "function" && !body.$$stub); 2105 } 2106 2107 Opal.alias = function(obj, name, old) { 2108 var id = $jsid(name), 2109 old_id = $jsid(old), 2110 body, 2111 alias; 2112 2113 // Aliasing on main means aliasing on Object... 2114 if (typeof obj.$$prototype === 'undefined') { 2115 obj = Opal.Object; 2116 } 2117 2118 body = obj.$$prototype[old_id]; 2119 2120 // When running inside #instance_eval the alias refers to class methods. 2121 if (obj.$$eval) { 2122 return Opal.alias(Opal.get_singleton_class(obj), name, old); 2123 } 2124 2125 if (!is_method_body(body)) { 2126 var ancestor = obj.$$super; 2127 2128 while (typeof(body) !== "function" && ancestor) { 2129 body = ancestor[old_id]; 2130 ancestor = ancestor.$$super; 2131 } 2132 2133 if (!is_method_body(body) && obj.$$is_module) { 2134 // try to look into Object 2135 body = Opal.Object.$$prototype[old_id]; 2136 } 2137 2138 if (!is_method_body(body)) { 2139 $raise(Opal.NameError, "undefined method `" + old + "' for class `" + obj.$name() + "'"); 2140 } 2141 } 2142 2143 // If the body is itself an alias use the original body 2144 // to keep the max depth at 1. 2145 if (body.$$alias_of) body = body.$$alias_of; 2146 2147 // We need a wrapper because otherwise properties 2148 // would be overwritten on the original body. 2149 alias = function() { 2150 var block = alias.$$p, args, i, ii; 2151 2152 args = new Array(arguments.length); 2153 for(i = 0, ii = arguments.length; i < ii; i++) { 2154 args[i] = arguments[i]; 2155 } 2156 2157 alias.$$p = null; 2158 2159 return Opal.send(this, body, args, block); 2160 }; 2161 2162 // Assign the 'length' value with defineProperty because 2163 // in strict mode the property is not writable. 2164 // It doesn't work in older browsers (like Chrome 38), where 2165 // an exception is thrown breaking Opal altogether. 2166 try { 2167 Object.defineProperty(alias, 'length', { value: body.length }); 2168 } catch (e) {} 2169 2170 // Try to make the browser pick the right name 2171 alias.displayName = name; 2172 2173 alias.$$arity = body.$$arity == null ? body.length : body.$$arity; 2174 alias.$$parameters = body.$$parameters; 2175 alias.$$source_location = body.$$source_location; 2176 alias.$$alias_of = body; 2177 alias.$$alias_name = name; 2178 2179 Opal.defn(obj, id, alias); 2180 2181 return obj; 2182 }; 2183 2184 Opal.alias_gvar = function(new_name, old_name) { 2185 Object.defineProperty($gvars, new_name, { 2186 configurable: true, 2187 enumerable: true, 2188 get: function() { 2189 return $gvars[old_name]; 2190 }, 2191 set: function(new_value) { 2192 $gvars[old_name] = new_value; 2193 } 2194 }); 2195 return nil; 2196 }; 2197 2198 Opal.alias_native = function(obj, name, native_name) { 2199 var id = $jsid(name), 2200 body = obj.$$prototype[native_name]; 2201 2202 if (typeof(body) !== "function" || body.$$stub) { 2203 $raise(Opal.NameError, "undefined native method `" + native_name + "' for class `" + obj.$name() + "'"); 2204 } 2205 2206 Opal.defn(obj, id, body); 2207 2208 return obj; 2209 }; 2210 2211 2212 // Hashes 2213 // ------ 2214 2215 Opal.hash_init = function(hash) { 2216 hash.$$smap = Object.create(null); 2217 hash.$$map = Object.create(null); 2218 hash.$$keys = []; 2219 }; 2220 2221 Opal.hash_clone = function(from_hash, to_hash) { 2222 to_hash.$$none = from_hash.$$none; 2223 to_hash.$$proc = from_hash.$$proc; 2224 2225 for (var i = 0, keys = from_hash.$$keys, smap = from_hash.$$smap, len = keys.length, key, value; i < len; i++) { 2226 key = keys[i]; 2227 2228 if (key.$$is_string) { 2229 value = smap[key]; 2230 } else { 2231 value = key.value; 2232 key = key.key; 2233 } 2234 2235 Opal.hash_put(to_hash, key, value); 2236 } 2237 }; 2238 2239 Opal.hash_put = function(hash, key, value) { 2240 if (key.$$is_string) { 2241 if (!$has_own(hash.$$smap, key)) { 2242 hash.$$keys.push(key); 2243 } 2244 hash.$$smap[key] = value; 2245 return; 2246 } 2247 2248 var key_hash, bucket, last_bucket; 2249 key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash(); 2250 2251 if (!$has_own(hash.$$map, key_hash)) { 2252 bucket = {key: key, key_hash: key_hash, value: value}; 2253 hash.$$keys.push(bucket); 2254 hash.$$map[key_hash] = bucket; 2255 return; 2256 } 2257 2258 bucket = hash.$$map[key_hash]; 2259 2260 while (bucket) { 2261 if (key === bucket.key || key['$eql?'](bucket.key)) { 2262 last_bucket = undefined; 2263 bucket.value = value; 2264 break; 2265 } 2266 last_bucket = bucket; 2267 bucket = bucket.next; 2268 } 2269 2270 if (last_bucket) { 2271 bucket = {key: key, key_hash: key_hash, value: value}; 2272 hash.$$keys.push(bucket); 2273 last_bucket.next = bucket; 2274 } 2275 }; 2276 2277 Opal.hash_get = function(hash, key) { 2278 if (key.$$is_string) { 2279 if ($has_own(hash.$$smap, key)) { 2280 return hash.$$smap[key]; 2281 } 2282 return; 2283 } 2284 2285 var key_hash, bucket; 2286 key_hash = hash.$$by_identity ? Opal.id(key) : key.$hash(); 2287 2288 if ($has_own(hash.$$map, key_hash)) { 2289 bucket = hash.$$map[key_hash]; 2290 2291 while (bucket) { 2292 if (key === bucket.key || key['$eql?'](bucket.key)) { 2293 return bucket.value; 2294 } 2295 bucket = bucket.next; 2296 } 2297 } 2298 }; 2299 2300 Opal.hash_delete = function(hash, key) { 2301 var i, keys = hash.$$keys, length = keys.length, value, key_tmp; 2302 2303 if (key.$$is_string) { 2304 if (typeof key !== "string") key = key.valueOf(); 2305 2306 if (!$has_own(hash.$$smap, key)) { 2307 return; 2308 } 2309 2310 for (i = 0; i < length; i++) { 2311 key_tmp = keys[i]; 2312 2313 if (key_tmp.$$is_string && typeof key_tmp !== "string") { 2314 key_tmp = key_tmp.valueOf(); 2315 } 2316 2317 if (key_tmp === key) { 2318 keys.splice(i, 1); 2319 break; 2320 } 2321 } 2322 2323 value = hash.$$smap[key]; 2324 delete hash.$$smap[key]; 2325 return value; 2326 } 2327 2328 var key_hash = key.$hash(); 2329 2330 if (!$has_own(hash.$$map, key_hash)) { 2331 return; 2332 } 2333 2334 var bucket = hash.$$map[key_hash], last_bucket; 2335 2336 while (bucket) { 2337 if (key === bucket.key || key['$eql?'](bucket.key)) { 2338 value = bucket.value; 2339 2340 for (i = 0; i < length; i++) { 2341 if (keys[i] === bucket) { 2342 keys.splice(i, 1); 2343 break; 2344 } 2345 } 2346 2347 if (last_bucket && bucket.next) { 2348 last_bucket.next = bucket.next; 2349 } 2350 else if (last_bucket) { 2351 delete last_bucket.next; 2352 } 2353 else if (bucket.next) { 2354 hash.$$map[key_hash] = bucket.next; 2355 } 2356 else { 2357 delete hash.$$map[key_hash]; 2358 } 2359 2360 return value; 2361 } 2362 last_bucket = bucket; 2363 bucket = bucket.next; 2364 } 2365 }; 2366 2367 Opal.hash_rehash = function(hash) { 2368 for (var i = 0, length = hash.$$keys.length, key_hash, bucket, last_bucket; i < length; i++) { 2369 2370 if (hash.$$keys[i].$$is_string) { 2371 continue; 2372 } 2373 2374 key_hash = hash.$$keys[i].key.$hash(); 2375 2376 if (key_hash === hash.$$keys[i].key_hash) { 2377 continue; 2378 } 2379 2380 bucket = hash.$$map[hash.$$keys[i].key_hash]; 2381 last_bucket = undefined; 2382 2383 while (bucket) { 2384 if (bucket === hash.$$keys[i]) { 2385 if (last_bucket && bucket.next) { 2386 last_bucket.next = bucket.next; 2387 } 2388 else if (last_bucket) { 2389 delete last_bucket.next; 2390 } 2391 else if (bucket.next) { 2392 hash.$$map[hash.$$keys[i].key_hash] = bucket.next; 2393 } 2394 else { 2395 delete hash.$$map[hash.$$keys[i].key_hash]; 2396 } 2397 break; 2398 } 2399 last_bucket = bucket; 2400 bucket = bucket.next; 2401 } 2402 2403 hash.$$keys[i].key_hash = key_hash; 2404 2405 if (!$has_own(hash.$$map, key_hash)) { 2406 hash.$$map[key_hash] = hash.$$keys[i]; 2407 continue; 2408 } 2409 2410 bucket = hash.$$map[key_hash]; 2411 last_bucket = undefined; 2412 2413 while (bucket) { 2414 if (bucket === hash.$$keys[i]) { 2415 last_bucket = undefined; 2416 break; 2417 } 2418 last_bucket = bucket; 2419 bucket = bucket.next; 2420 } 2421 2422 if (last_bucket) { 2423 last_bucket.next = hash.$$keys[i]; 2424 } 2425 } 2426 }; 2427 2428 Opal.hash = function() { 2429 var arguments_length = arguments.length, args, hash, i, length, key, value; 2430 2431 if (arguments_length === 1 && arguments[0].$$is_hash) { 2432 return arguments[0]; 2433 } 2434 2435 hash = new Opal.Hash(); 2436 Opal.hash_init(hash); 2437 2438 if (arguments_length === 1) { 2439 args = arguments[0]; 2440 2441 if (arguments[0].$$is_array) { 2442 length = args.length; 2443 2444 for (i = 0; i < length; i++) { 2445 if (args[i].length !== 2) { 2446 $raise(Opal.ArgumentError, "value not of length 2: " + args[i].$inspect()); 2447 } 2448 2449 key = args[i][0]; 2450 value = args[i][1]; 2451 2452 Opal.hash_put(hash, key, value); 2453 } 2454 2455 return hash; 2456 } 2457 else { 2458 args = arguments[0]; 2459 for (key in args) { 2460 if ($has_own(args, key)) { 2461 value = args[key]; 2462 2463 Opal.hash_put(hash, key, value); 2464 } 2465 } 2466 2467 return hash; 2468 } 2469 } 2470 2471 if (arguments_length % 2 !== 0) { 2472 $raise(Opal.ArgumentError, "odd number of arguments for Hash"); 2473 } 2474 2475 for (i = 0; i < arguments_length; i += 2) { 2476 key = arguments[i]; 2477 value = arguments[i + 1]; 2478 2479 Opal.hash_put(hash, key, value); 2480 } 2481 2482 return hash; 2483 }; 2484 2485 // A faster Hash creator for hashes that just use symbols and 2486 // strings as keys. The map and keys array can be constructed at 2487 // compile time, so they are just added here by the constructor 2488 // function. 2489 // 2490 Opal.hash2 = function(keys, smap) { 2491 var hash = new Opal.Hash(); 2492 2493 hash.$$smap = smap; 2494 hash.$$map = Object.create(null); 2495 hash.$$keys = keys; 2496 2497 return hash; 2498 }; 2499 2500 // Create a new range instance with first and last values, and whether the 2501 // range excludes the last value. 2502 // 2503 Opal.range = function(first, last, exc) { 2504 var range = new Opal.Range(); 2505 range.begin = first; 2506 range.end = last; 2507 range.excl = exc; 2508 2509 return range; 2510 }; 2511 2512 var reserved_ivar_names = [ 2513 // properties 2514 "constructor", "displayName", "__count__", "__noSuchMethod__", 2515 "__parent__", "__proto__", 2516 // methods 2517 "hasOwnProperty", "valueOf" 2518 ]; 2519 2520 // Get the ivar name for a given name. 2521 // Mostly adds a trailing $ to reserved names. 2522 // 2523 Opal.ivar = function(name) { 2524 if (reserved_ivar_names.indexOf(name) !== -1) { 2525 name += "$"; 2526 } 2527 2528 return name; 2529 }; 2530 2531 // Support for #freeze 2532 // ------------------- 2533 2534 // helper that can be used from methods 2535 function $deny_frozen_access(obj) { 2536 if (obj.$$frozen) { 2537 $raise(Opal.FrozenError, "can't modify frozen " + (obj.$class()) + ": " + (obj), Opal.hash2(["receiver"], {"receiver": obj})); 2538 } 2539 } Opal.deny_frozen_access = $deny_frozen_access; 2540 2541 // common #freeze runtime support 2542 Opal.freeze = function(obj) { 2543 $prop(obj, "$$frozen", true); 2544 2545 // set $$id 2546 if (!obj.hasOwnProperty('$$id')) { $prop(obj, '$$id', $uid()); } 2547 2548 if (obj.hasOwnProperty('$$meta')) { 2549 // freeze $$meta if it has already been set 2550 obj.$$meta.$freeze(); 2551 } else { 2552 // ensure $$meta can be set lazily, $$meta is frozen when set in runtime.js 2553 $prop(obj, '$$meta', null); 2554 } 2555 2556 // $$comparable is used internally and set multiple times 2557 // defining it before sealing ensures it can be modified later on 2558 if (!obj.hasOwnProperty('$$comparable')) { $prop(obj, '$$comparable', null); } 2559 2560 // seal the Object 2561 Object.seal(obj); 2562 2563 return obj; 2564 }; 2565 2566 // freze props, make setters of instance variables throw FrozenError 2567 Opal.freeze_props = function(obj) { 2568 var prop, prop_type, desc; 2569 2570 for(prop in obj) { 2571 prop_type = typeof(prop); 2572 2573 // prop_type "object" here is a String(), skip $ props 2574 if ((prop_type === "string" || prop_type === "object") && prop[0] === '$') { 2575 continue; 2576 } 2577 2578 desc = Object.getOwnPropertyDescriptor(obj, prop); 2579 if (desc && desc.enumerable && desc.writable) { 2580 // create closure to retain current value as cv 2581 // for Opal 2.0 let for cv should do the trick, instead of a function 2582 (function() { 2583 // set v to undefined, as if the property is not set 2584 var cv = obj[prop]; 2585 Object.defineProperty(obj, prop, { 2586 get: function() { return cv; }, 2587 set: function(_val) { $deny_frozen_access(obj); }, 2588 enumerable: true 2589 }); 2590 })(); 2591 } 2592 } 2593 }; 2594 2595 // Regexps 2596 // ------- 2597 2598 // Escape Regexp special chars letting the resulting string be used to build 2599 // a new Regexp. 2600 // 2601 Opal.escape_regexp = function(str) { 2602 return str.replace(/([-[\]\/{}()*+?.^$\\| ])/g, '\\$1') 2603 .replace(/[\n]/g, '\\n') 2604 .replace(/[\r]/g, '\\r') 2605 .replace(/[\f]/g, '\\f') 2606 .replace(/[\t]/g, '\\t'); 2607 }; 2608 2609 // Create a global Regexp from a RegExp object and cache the result 2610 // on the object itself ($$g attribute). 2611 // 2612 Opal.global_regexp = function(pattern) { 2613 if (pattern.global) { 2614 return pattern; // RegExp already has the global flag 2615 } 2616 if (pattern.$$g == null) { 2617 pattern.$$g = new RegExp(pattern.source, (pattern.multiline ? 'gm' : 'g') + (pattern.ignoreCase ? 'i' : '')); 2618 } else { 2619 pattern.$$g.lastIndex = null; // reset lastIndex property 2620 } 2621 return pattern.$$g; 2622 }; 2623 2624 // Create a global multiline Regexp from a RegExp object and cache the result 2625 // on the object itself ($$gm or $$g attribute). 2626 // 2627 Opal.global_multiline_regexp = function(pattern) { 2628 var result, flags; 2629 2630 // RegExp already has the global and multiline flag 2631 if (pattern.global && pattern.multiline) return pattern; 2632 2633 flags = 'gm' + (pattern.ignoreCase ? 'i' : ''); 2634 if (pattern.multiline) { 2635 // we are using the $$g attribute because the Regexp is already multiline 2636 if (pattern.$$g == null) { 2637 pattern.$$g = new RegExp(pattern.source, flags); 2638 } 2639 result = pattern.$$g; 2640 } else { 2641 if (pattern.$$gm == null) { 2642 pattern.$$gm = new RegExp(pattern.source, flags); 2643 } 2644 result = pattern.$$gm; 2645 } 2646 result.lastIndex = null; // reset lastIndex property 2647 return result; 2648 }; 2649 2650 // Combine multiple regexp parts together 2651 Opal.regexp = function(parts, flags) { 2652 var part; 2653 var ignoreCase = typeof flags !== 'undefined' && flags && flags.indexOf('i') >= 0; 2654 2655 for (var i = 0, ii = parts.length; i < ii; i++) { 2656 part = parts[i]; 2657 if (part instanceof RegExp) { 2658 if (part.ignoreCase !== ignoreCase) 2659 Opal.Kernel.$warn( 2660 "ignore case doesn't match for " + part.source.$inspect(), 2661 Opal.hash({uplevel: 1}) 2662 ); 2663 2664 part = part.source; 2665 } 2666 if (part === '') part = '(?:' + part + ')'; 2667 parts[i] = part; 2668 } 2669 2670 if (flags) { 2671 return new RegExp(parts.join(''), flags); 2672 } else { 2673 return new RegExp(parts.join('')); 2674 } 2675 }; 2676 2677 // Require system 2678 // -------------- 2679 2680 Opal.modules = {}; 2681 Opal.loaded_features = ['corelib/runtime']; 2682 Opal.current_dir = '.'; 2683 Opal.require_table = {'corelib/runtime': true}; 2684 2685 Opal.normalize = function(path) { 2686 var parts, part, new_parts = [], SEPARATOR = '/'; 2687 2688 if (Opal.current_dir !== '.') { 2689 path = Opal.current_dir.replace(/\/*$/, '/') + path; 2690 } 2691 2692 path = path.replace(/^\.\//, ''); 2693 path = path.replace(/\.(rb|opal|js)$/, ''); 2694 parts = path.split(SEPARATOR); 2695 2696 for (var i = 0, ii = parts.length; i < ii; i++) { 2697 part = parts[i]; 2698 if (part === '') continue; 2699 (part === '..') ? new_parts.pop() : new_parts.push(part); 2700 } 2701 2702 return new_parts.join(SEPARATOR); 2703 }; 2704 2705 Opal.loaded = function(paths) { 2706 var i, l, path; 2707 2708 for (i = 0, l = paths.length; i < l; i++) { 2709 path = Opal.normalize(paths[i]); 2710 2711 if (Opal.require_table[path]) { 2712 continue; 2713 } 2714 2715 Opal.loaded_features.push(path); 2716 Opal.require_table[path] = true; 2717 } 2718 }; 2719 2720 Opal.load_normalized = function(path) { 2721 Opal.loaded([path]); 2722 2723 var module = Opal.modules[path]; 2724 2725 if (module) { 2726 var retval = module(Opal); 2727 if (typeof Promise !== 'undefined' && retval instanceof Promise) { 2728 // A special case of require having an async top: 2729 // We will need to await it. 2730 return retval.then($return_val(true)); 2731 } 2732 } 2733 else { 2734 var severity = Opal.config.missing_require_severity; 2735 var message = 'cannot load such file -- ' + path; 2736 2737 if (severity === "error") { 2738 $raise(Opal.LoadError, message); 2739 } 2740 else if (severity === "warning") { 2741 console.warn('WARNING: LoadError: ' + message); 2742 } 2743 } 2744 2745 return true; 2746 }; 2747 2748 Opal.load = function(path) { 2749 path = Opal.normalize(path); 2750 2751 return Opal.load_normalized(path); 2752 }; 2753 2754 Opal.require = function(path) { 2755 path = Opal.normalize(path); 2756 2757 if (Opal.require_table[path]) { 2758 return false; 2759 } 2760 2761 return Opal.load_normalized(path); 2762 }; 2763 2764 2765 // Strings 2766 // ------- 2767 2768 Opal.encodings = Object.create(null); 2769 2770 // Sets the encoding on a string, will treat string literals as frozen strings 2771 // raising a FrozenError. 2772 // 2773 // @param str [String] the string on which the encoding should be set 2774 // @param name [String] the canonical name of the encoding 2775 // @param type [String] possible values are either `"encoding"`, `"internal_encoding"`, or `undefined 2776 Opal.set_encoding = function(str, name, type) { 2777 if (typeof type === "undefined") type = "encoding"; 2778 if (typeof str === 'string' || str.$$frozen === true) 2779 $raise(Opal.FrozenError, "can't modify frozen String"); 2780 2781 var encoding = Opal.find_encoding(name); 2782 2783 if (encoding === str[type]) { return str; } 2784 2785 str[type] = encoding; 2786 2787 return str; 2788 }; 2789 2790 // Fetches the encoding for the given name or raises ArgumentError. 2791 Opal.find_encoding = function(name) { 2792 var register = Opal.encodings; 2793 var encoding = register[name] || register[name.toUpperCase()]; 2794 if (!encoding) $raise(Opal.ArgumentError, "unknown encoding name - " + name); 2795 return encoding; 2796 }; 2797 2798 // @returns a String object with the encoding set from a string literal 2799 Opal.enc = function(str, name) { 2800 var dup = new String(str); 2801 dup = Opal.set_encoding(dup, name); 2802 dup.internal_encoding = dup.encoding; 2803 return dup 2804 }; 2805 2806 // @returns a String object with the internal encoding set to Binary 2807 Opal.binary = function(str) { 2808 var dup = new String(str); 2809 return Opal.set_encoding(dup, "binary", "internal_encoding"); 2810 }; 2811 2812 Opal.last_promise = null; 2813 Opal.promise_unhandled_exception = false; 2814 2815 // Run a block of code, but if it returns a Promise, don't run the next 2816 // one, but queue it. 2817 Opal.queue = function(proc) { 2818 if (Opal.last_promise) { 2819 // The async path is taken only if anything before returned a 2820 // Promise(V2). 2821 Opal.last_promise = Opal.last_promise.then(function() { 2822 if (!Opal.promise_unhandled_exception) return proc(Opal); 2823 })['catch'](function(error) { 2824 if (Opal.respond_to(error, '$full_message')) { 2825 error = error.$full_message(); 2826 } 2827 console.error(error); 2828 // Abort further execution 2829 Opal.promise_unhandled_exception = true; 2830 Opal.exit(1); 2831 }); 2832 return Opal.last_promise; 2833 } 2834 else { 2835 var ret = proc(Opal); 2836 if (typeof Promise === 'function' && typeof ret === 'object' && ret instanceof Promise) { 2837 Opal.last_promise = ret; 2838 } 2839 return ret; 2840 } 2841 }; 2842 2843 // Operator helpers 2844 // ---------------- 2845 2846 function are_both_numbers(l,r) { return typeof(l) === 'number' && typeof(r) === 'number' } 2847 2848 Opal.rb_plus = function(l,r) { return are_both_numbers(l,r) ? l + r : l['$+'](r); }; 2849 Opal.rb_minus = function(l,r) { return are_both_numbers(l,r) ? l - r : l['$-'](r); }; 2850 Opal.rb_times = function(l,r) { return are_both_numbers(l,r) ? l * r : l['$*'](r); }; 2851 Opal.rb_divide = function(l,r) { return are_both_numbers(l,r) ? l / r : l['$/'](r); }; 2852 Opal.rb_lt = function(l,r) { return are_both_numbers(l,r) ? l < r : l['$<'](r); }; 2853 Opal.rb_gt = function(l,r) { return are_both_numbers(l,r) ? l > r : l['$>'](r); }; 2854 Opal.rb_le = function(l,r) { return are_both_numbers(l,r) ? l <= r : l['$<='](r); }; 2855 Opal.rb_ge = function(l,r) { return are_both_numbers(l,r) ? l >= r : l['$>='](r); }; 2856 2857 // Optimized helpers for calls like $truthy((a)['$==='](b)) -> $eqeqeq(a, b) 2858 function are_both_numbers_or_strings(lhs, rhs) { 2859 return (typeof lhs === 'number' && typeof rhs === 'number') || 2860 (typeof lhs === 'string' && typeof rhs === 'string'); 2861 } 2862 2863 function $eqeq(lhs, rhs) { 2864 return are_both_numbers_or_strings(lhs,rhs) ? lhs === rhs : $truthy((lhs)['$=='](rhs)); 2865 } Opal.eqeq = $eqeq; 2866 Opal.eqeqeq = function(lhs, rhs) { 2867 return are_both_numbers_or_strings(lhs,rhs) ? lhs === rhs : $truthy((lhs)['$==='](rhs)); 2868 }; 2869 Opal.neqeq = function(lhs, rhs) { 2870 return are_both_numbers_or_strings(lhs,rhs) ? lhs !== rhs : $truthy((lhs)['$!='](rhs)); 2871 }; 2872 Opal.not = function(arg) { 2873 if (undefined === arg || null === arg || false === arg || nil === arg) return true; 2874 if (true === arg || arg['$!'].$$pristine) return false; 2875 return $truthy(arg['$!']()); 2876 }; 2877 2878 // Shortcuts - optimized function generators for simple kinds of functions 2879 function $return_val(arg) { 2880 return function() { 2881 return arg; 2882 } 2883 } 2884 Opal.return_val = $return_val; 2885 2886 Opal.return_self = function() { 2887 return this; 2888 }; 2889 Opal.return_ivar = function(ivar) { 2890 return function() { 2891 if (this[ivar] == null) { return nil; } 2892 return this[ivar]; 2893 } 2894 }; 2895 Opal.assign_ivar = function(ivar) { 2896 return function(val) { 2897 $deny_frozen_access(this); 2898 return this[ivar] = val; 2899 } 2900 }; 2901 Opal.assign_ivar_val = function(ivar, static_val) { 2902 return function() { 2903 $deny_frozen_access(this); 2904 return this[ivar] = static_val; 2905 } 2906 }; 2907 2908 // Primitives for handling parameters 2909 Opal.ensure_kwargs = function(kwargs) { 2910 if (kwargs == null) { 2911 return Opal.hash2([], {}); 2912 } else if (kwargs.$$is_hash) { 2913 return kwargs; 2914 } else { 2915 $raise(Opal.ArgumentError, 'expected kwargs'); 2916 } 2917 }; 2918 2919 Opal.get_kwarg = function(kwargs, key) { 2920 if (!$has_own(kwargs.$$smap, key)) { 2921 $raise(Opal.ArgumentError, 'missing keyword: '+key); 2922 } 2923 return kwargs.$$smap[key]; 2924 }; 2925 2926 // Arrays of size > 32 elements that contain only strings, 2927 // symbols, integers and nils are compiled as a self-extracting 2928 // string. 2929 Opal.large_array_unpack = function(str) { 2930 var array = str.split(","), length = array.length, i; 2931 for (i = 0; i < length; i++) { 2932 switch(array[i][0]) { 2933 case undefined: 2934 array[i] = nil; 2935 break; 2936 case '-': 2937 case '0': 2938 case '1': 2939 case '2': 2940 case '3': 2941 case '4': 2942 case '5': 2943 case '6': 2944 case '7': 2945 case '8': 2946 case '9': 2947 array[i] = +array[i]; 2948 } 2949 } 2950 return array; 2951 }; 2952 2953 // Initialization 2954 // -------------- 2955 Opal.BasicObject = BasicObject = $allocate_class('BasicObject', null); 2956 Opal.Object = _Object = $allocate_class('Object', Opal.BasicObject); 2957 Opal.Module = Module = $allocate_class('Module', Opal.Object); 2958 Opal.Class = Class = $allocate_class('Class', Opal.Module); 2959 Opal.Opal = _Opal = $allocate_module('Opal'); 2960 Opal.Kernel = Kernel = $allocate_module('Kernel'); 2961 2962 $set_proto(Opal.BasicObject, Opal.Class.$$prototype); 2963 $set_proto(Opal.Object, Opal.Class.$$prototype); 2964 $set_proto(Opal.Module, Opal.Class.$$prototype); 2965 $set_proto(Opal.Class, Opal.Class.$$prototype); 2966 2967 // BasicObject can reach itself, avoid const_set to skip the $$base_module logic 2968 BasicObject.$$const.BasicObject = BasicObject; 2969 2970 // Assign basic constants 2971 $const_set(_Object, "BasicObject", BasicObject); 2972 $const_set(_Object, "Object", _Object); 2973 $const_set(_Object, "Module", Module); 2974 $const_set(_Object, "Class", Class); 2975 $const_set(_Object, "Opal", _Opal); 2976 $const_set(_Object, "Kernel", Kernel); 2977 2978 // Fix booted classes to have correct .class value 2979 BasicObject.$$class = Class; 2980 _Object.$$class = Class; 2981 Module.$$class = Class; 2982 Class.$$class = Class; 2983 _Opal.$$class = Module; 2984 Kernel.$$class = Module; 2985 2986 // Forward .toString() to #to_s 2987 $prop(_Object.$$prototype, 'toString', function() { 2988 var to_s = this.$to_s(); 2989 if (to_s.$$is_string && typeof(to_s) === 'object') { 2990 // a string created using new String('string') 2991 return to_s.valueOf(); 2992 } else { 2993 return to_s; 2994 } 2995 }); 2996 2997 // Make Kernel#require immediately available as it's needed to require all the 2998 // other corelib files. 2999 $prop(_Object.$$prototype, '$require', Opal.require); 3000 3001 // Instantiate the main object 3002 Opal.top = new _Object(); 3003 Opal.top.$to_s = Opal.top.$inspect = $return_val('main'); 3004 Opal.top.$define_method = top_define_method; 3005 3006 // Foward calls to define_method on the top object to Object 3007 function top_define_method() { 3008 var args = $slice(arguments); 3009 var block = top_define_method.$$p; 3010 top_define_method.$$p = null; 3011 return Opal.send(_Object, 'define_method', args, block) 3012 } 3013 // Nil 3014 Opal.NilClass = $allocate_class('NilClass', Opal.Object); 3015 $const_set(_Object, 'NilClass', Opal.NilClass); 3016 nil = Opal.nil = new Opal.NilClass(); 3017 nil.$$id = nil_id; 3018 nil.call = nil.apply = function() { $raise(Opal.LocalJumpError, 'no block given'); }; 3019 nil.$$frozen = true; 3020 nil.$$comparable = false; 3021 Object.seal(nil); 3022 3023 Opal.thrower = function(type) { 3024 var thrower = new Error('unexpected '+type); 3025 thrower.$thrower_type = type; 3026 thrower.$throw = function(value) { 3027 if (value == null) value = nil; 3028 thrower.$v = value; 3029 throw thrower; 3030 }; 3031 return thrower; 3032 }; 3033 3034 Opal.t_eval_return = Opal.thrower("return"); 3035 3036 TypeError.$$super = Error; 3037 3038 // If enable-file-source-embed compiler option is enabled, each module loaded will add its 3039 // sources to this object 3040 Opal.file_sources = {}; 3041}).call(undefined); 3042Opal.loaded(["corelib/runtime.js"]); 3043Opal.modules["corelib/helpers"] = function(Opal) {/* Generated by Opal 1.7.3 */ 3044 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.$$$; 3045 3046 Opal.add_stubs('===,raise,respond_to?,nil?,__send__,<=>,class,coerce_to!,new,to_s,__id__'); 3047 return (function($base) { 3048 var self = $module($base, 'Opal'); 3049 3050 3051 3052 $defs(self, '$bridge', function $$bridge(constructor, klass) { 3053 3054 return Opal.bridge(constructor, klass); 3055 }); 3056 $defs(self, '$coerce_to!', function $Opal_coerce_to$excl$1(object, type, method, $a) { 3057 var $post_args, args, coerced = nil; 3058 3059 3060 $post_args = $slice(arguments, 3); 3061 args = $post_args; 3062 coerced = $coerce_to(object, type, method, args); 3063 if (!$eqeqeq(type, coerced)) { 3064 $Kernel.$raise($type_error(object, type, method, coerced)); 3065 } return coerced; 3066 }, -4); 3067 $defs(self, '$coerce_to?', function $Opal_coerce_to$ques$2(object, type, method, $a) { 3068 var $post_args, args, coerced = nil; 3069 3070 3071 $post_args = $slice(arguments, 3); 3072 args = $post_args; 3073 if (!$truthy(object['$respond_to?'](method))) { 3074 return nil 3075 } coerced = $coerce_to(object, type, method, args); 3076 if ($truthy(coerced['$nil?']())) { 3077 return nil 3078 } if (!$eqeqeq(type, coerced)) { 3079 $Kernel.$raise($type_error(object, type, method, coerced)); 3080 } return coerced; 3081 }, -4); 3082 $defs(self, '$try_convert', function $$try_convert(object, type, method) { 3083 3084 3085 if ($eqeqeq(type, object)) { 3086 return object 3087 } if ($truthy(object['$respond_to?'](method))) { 3088 return object.$__send__(method) 3089 } else { 3090 return nil 3091 } }); 3092 $defs(self, '$compare', function $$compare(a, b) { 3093 var compare = nil; 3094 3095 3096 compare = a['$<=>'](b); 3097 if ($truthy(compare === nil)) { 3098 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (a.$class()) + " with " + (b.$class()) + " failed"); 3099 } return compare; 3100 }); 3101 $defs(self, '$destructure', function $$destructure(args) { 3102 3103 3104 if (args.length == 1) { 3105 return args[0]; 3106 } 3107 else if (args.$$is_array) { 3108 return args; 3109 } 3110 else { 3111 var args_ary = new Array(args.length); 3112 for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } 3113 3114 return args_ary; 3115 } 3116 3117 }); 3118 $defs(self, '$respond_to?', function $Opal_respond_to$ques$3(obj, method, include_all) { 3119 3120 3121 if (include_all == null) include_all = false; 3122 3123 if (obj == null || !obj.$$class) { 3124 return false; 3125 } 3126 return obj['$respond_to?'](method, include_all); 3127 }, -3); 3128 $defs(self, '$instance_variable_name!', function $Opal_instance_variable_name$excl$4(name) { 3129 3130 3131 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 3132 if (!$truthy(/^@[a-zA-Z_][a-zA-Z0-9_]*?$/.test(name))) { 3133 $Kernel.$raise($$$('NameError').$new("'" + (name) + "' is not allowed as an instance variable name", name)); 3134 } return name; 3135 }); 3136 $defs(self, '$class_variable_name!', function $Opal_class_variable_name$excl$5(name) { 3137 3138 3139 name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 3140 if ($truthy(name.length < 3 || name.slice(0,2) !== '@@')) { 3141 $Kernel.$raise($$$('NameError').$new("`" + (name) + "' is not allowed as a class variable name", name)); 3142 } return name; 3143 }); 3144 $defs(self, '$const_name?', function $Opal_const_name$ques$6(const_name) { 3145 3146 3147 if (typeof const_name !== 'string') { 3148 (const_name = $Opal['$coerce_to!'](const_name, $$$('String'), "to_str")); 3149 } 3150 3151 return const_name[0] === const_name[0].toUpperCase() 3152 3153 }); 3154 $defs(self, '$const_name!', function $Opal_const_name$excl$7(const_name) { 3155 var self = this; 3156 3157 3158 if ($truthy((($$$('::', 'String', 'skip_raise')) ? 'constant' : nil))) { 3159 const_name = $Opal['$coerce_to!'](const_name, $$$('String'), "to_str"); 3160 } 3161 if (!const_name || const_name[0] != const_name[0].toUpperCase()) { 3162 self.$raise($$$('NameError'), "wrong constant name " + (const_name)); 3163 } 3164 return const_name; 3165 }); 3166 $defs(self, '$pristine', function $$pristine(owner_class, $a) { 3167 var $post_args, method_names; 3168 3169 3170 $post_args = $slice(arguments, 1); 3171 method_names = $post_args; 3172 3173 var method_name, method; 3174 for (var i = method_names.length - 1; i >= 0; i--) { 3175 method_name = method_names[i]; 3176 method = owner_class.$$prototype[Opal.jsid(method_name)]; 3177 3178 if (method && !method.$$stub) { 3179 method.$$pristine = true; 3180 } 3181 } 3182 return nil; 3183 }, -2); 3184 var inspect_stack = []; 3185 return $defs(self, '$inspect', function $$inspect(value) { 3186 var e = nil; 3187 var pushed = false; 3188 3189 return (function() { try { 3190 try { 3191 3192 3193 if (value === null) { 3194 // JS null value 3195 return 'null'; 3196 } 3197 else if (value === undefined) { 3198 // JS undefined value 3199 return 'undefined'; 3200 } 3201 else if (typeof value.$$class === 'undefined') { 3202 // JS object / other value that is not bridged 3203 return Object.prototype.toString.apply(value); 3204 } 3205 else if (typeof value.$inspect !== 'function' || value.$inspect.$$stub) { 3206 // BasicObject and friends 3207 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3208 } 3209 else if (inspect_stack.indexOf(value.$__id__()) !== -1) { 3210 // inspect recursing inside inspect to find out about the 3211 // same object 3212 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3213 } 3214 else { 3215 // anything supporting Opal 3216 inspect_stack.push(value.$__id__()); 3217 pushed = true; 3218 return value.$inspect(); 3219 } 3220 ; 3221 return nil; 3222 } catch ($err) { 3223 if (Opal.rescue($err, [$$$('Exception')])) {(e = $err); 3224 try { 3225 return "#<" + (value.$$class) + ":0x" + (value.$__id__().$to_s(16)) + ">" 3226 } finally { Opal.pop_exception(); } 3227 } else { throw $err; } 3228 } 3229 } finally { 3230 if (pushed) inspect_stack.pop(); 3231 } })(); }, -1); 3232 })('::') 3233}; 3234 3235Opal.modules["corelib/module"] = function(Opal) {/* Generated by Opal 1.7.3 */ 3236 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.$$$; 3237 3238 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'); 3239 3240 (function($base, $super, $parent_nesting) { 3241 var self = $klass($base, $super, 'Module'); 3242 3243 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 3244 3245 3246 $defs(self, '$allocate', function $$allocate() { 3247 var self = this; 3248 3249 3250 var module = Opal.allocate_module(nil, function(){}); 3251 // Link the prototype of Module subclasses 3252 if (self !== Opal.Module) Object.setPrototypeOf(module, self.$$prototype); 3253 return module; 3254 3255 }); 3256 3257 $def(self, '$initialize', function $$initialize() { 3258 var block = $$initialize.$$p || nil, self = this; 3259 3260 $$initialize.$$p = null; 3261 if ((block !== nil)) { 3262 return $send(self, 'module_eval', [], block.$to_proc()) 3263 } else { 3264 return nil 3265 } }); 3266 3267 $def(self, '$===', function $Module_$eq_eq_eq$1(object) { 3268 var self = this; 3269 3270 3271 if ($truthy(object == null)) { 3272 return false 3273 } return Opal.is_a(object, self); }); 3274 3275 $def(self, '$<', function $Module_$lt$2(other) { 3276 var self = this; 3277 3278 3279 if (!$eqeqeq($Module, other)) { 3280 $Kernel.$raise($$$('TypeError'), "compared with non class/module"); 3281 } 3282 var working = self, 3283 ancestors, 3284 i, length; 3285 3286 if (working === other) { 3287 return false; 3288 } 3289 3290 for (i = 0, ancestors = Opal.ancestors(self), length = ancestors.length; i < length; i++) { 3291 if (ancestors[i] === other) { 3292 return true; 3293 } 3294 } 3295 3296 for (i = 0, ancestors = Opal.ancestors(other), length = ancestors.length; i < length; i++) { 3297 if (ancestors[i] === self) { 3298 return false; 3299 } 3300 } 3301 3302 return nil; 3303 }); 3304 3305 $def(self, '$<=', function $Module_$lt_eq$3(other) { 3306 var self = this, $ret_or_1 = nil; 3307 3308 if ($truthy(($ret_or_1 = self['$equal?'](other)))) { 3309 return $ret_or_1 3310 } else { 3311 return $rb_lt(self, other) 3312 } 3313 }); 3314 3315 $def(self, '$>', function $Module_$gt$4(other) { 3316 var self = this; 3317 3318 3319 if (!$eqeqeq($Module, other)) { 3320 $Kernel.$raise($$$('TypeError'), "compared with non class/module"); 3321 } return $rb_lt(other, self); 3322 }); 3323 3324 $def(self, '$>=', function $Module_$gt_eq$5(other) { 3325 var self = this, $ret_or_1 = nil; 3326 3327 if ($truthy(($ret_or_1 = self['$equal?'](other)))) { 3328 return $ret_or_1 3329 } else { 3330 return $rb_gt(self, other) 3331 } 3332 }); 3333 3334 $def(self, '$<=>', function $Module_$lt_eq_gt$6(other) { 3335 var self = this, lt = nil; 3336 3337 3338 3339 if (self === other) { 3340 return 0; 3341 } 3342 if (!$eqeqeq($Module, other)) { 3343 return nil 3344 } lt = $rb_lt(self, other); 3345 if ($truthy(lt['$nil?']())) { 3346 return nil 3347 } if ($truthy(lt)) { 3348 return -1 3349 } else { 3350 return 1 3351 } }); 3352 3353 $def(self, '$alias_method', function $$alias_method(newname, oldname) { 3354 var self = this; 3355 3356 3357 $deny_frozen_access(self); 3358 newname = $coerce_to(newname, $$$('String'), 'to_str'); 3359 oldname = $coerce_to(oldname, $$$('String'), 'to_str'); 3360 Opal.alias(self, newname, oldname); 3361 return self; 3362 }); 3363 3364 $def(self, '$alias_native', function $$alias_native(mid, jsid) { 3365 var self = this; 3366 3367 3368 if (jsid == null) jsid = mid; 3369 $deny_frozen_access(self); 3370 Opal.alias_native(self, mid, jsid); 3371 return self; 3372 }, -2); 3373 3374 $def(self, '$ancestors', function $$ancestors() { 3375 var self = this; 3376 3377 return Opal.ancestors(self); 3378 }); 3379 3380 $def(self, '$append_features', function $$append_features(includer) { 3381 var self = this; 3382 3383 3384 $deny_frozen_access(includer); 3385 Opal.append_features(self, includer); 3386 return self; 3387 }); 3388 3389 $def(self, '$attr_accessor', function $$attr_accessor($a) { 3390 var $post_args, names, self = this; 3391 3392 3393 $post_args = $slice(arguments); 3394 names = $post_args; 3395 $send(self, 'attr_reader', $to_a(names)); 3396 return $send(self, 'attr_writer', $to_a(names)); 3397 }, -1); 3398 3399 $def(self, '$attr', function $$attr($a) { 3400 var $post_args, args, self = this; 3401 3402 3403 $post_args = $slice(arguments); 3404 args = $post_args; 3405 3406 if (args.length == 2 && (args[1] === true || args[1] === false)) { 3407 self.$warn("optional boolean argument is obsoleted", $hash2(["uplevel"], {"uplevel": 1})); 3408 3409 args[1] ? self.$attr_accessor(args[0]) : self.$attr_reader(args[0]); 3410 return nil; 3411 } 3412 return $send(self, 'attr_reader', $to_a(args)); 3413 }, -1); 3414 3415 $def(self, '$attr_reader', function $$attr_reader($a) { 3416 var $post_args, names, self = this; 3417 3418 3419 $post_args = $slice(arguments); 3420 names = $post_args; 3421 3422 $deny_frozen_access(self); 3423 3424 var proto = self.$$prototype; 3425 3426 for (var i = names.length - 1; i >= 0; i--) { 3427 var name = names[i], 3428 id = $jsid(name), 3429 ivar = $ivar(name); 3430 3431 var body = $return_ivar(ivar); 3432 3433 // initialize the instance variable as nil 3434 Opal.prop(proto, ivar, nil); 3435 3436 body.$$parameters = []; 3437 body.$$arity = 0; 3438 3439 Opal.defn(self, id, body); 3440 } 3441 return nil; 3442 }, -1); 3443 3444 $def(self, '$attr_writer', function $$attr_writer($a) { 3445 var $post_args, names, self = this; 3446 3447 3448 $post_args = $slice(arguments); 3449 names = $post_args; 3450 3451 $deny_frozen_access(self); 3452 3453 var proto = self.$$prototype; 3454 3455 for (var i = names.length - 1; i >= 0; i--) { 3456 var name = names[i], 3457 id = $jsid(name + '='), 3458 ivar = $ivar(name); 3459 3460 var body = $assign_ivar(ivar); 3461 3462 body.$$parameters = [['req']]; 3463 body.$$arity = 1; 3464 3465 // initialize the instance variable as nil 3466 Opal.prop(proto, ivar, nil); 3467 3468 Opal.defn(self, id, body); 3469 } 3470 return nil; 3471 }, -1); 3472 3473 $def(self, '$autoload', function $$autoload(const$, path) { 3474 var self = this; 3475 3476 3477 $deny_frozen_access(self); 3478 3479 if (!$$('Opal')['$const_name?'](const$)) { 3480 $Kernel.$raise($$$('NameError'), "autoload must be constant name: " + (const$)); 3481 } 3482 3483 if (path == "") { 3484 $Kernel.$raise($$$('ArgumentError'), "empty file name"); 3485 } 3486 3487 if (!self.$$const.hasOwnProperty(const$)) { 3488 if (!self.$$autoload) { 3489 self.$$autoload = {}; 3490 } 3491 Opal.const_cache_version++; 3492 self.$$autoload[const$] = { path: path, loaded: false, required: false, success: false, exception: false }; 3493 3494 if (self.$const_added && !self.$const_added.$$pristine) { 3495 self.$const_added(const$); 3496 } 3497 } 3498 return nil; 3499 3500 }); 3501 3502 $def(self, '$autoload?', function $Module_autoload$ques$7(const$) { 3503 var self = this; 3504 3505 3506 if (self.$$autoload && self.$$autoload[const$] && !self.$$autoload[const$].required && !self.$$autoload[const$].success) { 3507 return self.$$autoload[const$].path; 3508 } 3509 3510 var ancestors = self.$ancestors(); 3511 3512 for (var i = 0, length = ancestors.length; i < length; i++) { 3513 if (ancestors[i].$$autoload && ancestors[i].$$autoload[const$] && !ancestors[i].$$autoload[const$].required && !ancestors[i].$$autoload[const$].success) { 3514 return ancestors[i].$$autoload[const$].path; 3515 } 3516 } 3517 return nil; 3518 3519 }); 3520 3521 $def(self, '$class_variables', function $$class_variables() { 3522 var self = this; 3523 3524 return Object.keys(Opal.class_variables(self)); 3525 }); 3526 3527 $def(self, '$class_variable_get', function $$class_variable_get(name) { 3528 var self = this; 3529 3530 3531 name = $Opal['$class_variable_name!'](name); 3532 return Opal.class_variable_get(self, name, false); }); 3533 3534 $def(self, '$class_variable_set', function $$class_variable_set(name, value) { 3535 var self = this; 3536 3537 3538 $deny_frozen_access(self); 3539 name = $Opal['$class_variable_name!'](name); 3540 return Opal.class_variable_set(self, name, value); }); 3541 3542 $def(self, '$class_variable_defined?', function $Module_class_variable_defined$ques$8(name) { 3543 var self = this; 3544 3545 3546 name = $Opal['$class_variable_name!'](name); 3547 return Opal.class_variables(self).hasOwnProperty(name); }); 3548 3549 $def(self, '$const_added', $return_val(nil)); 3550 $Opal.$pristine(self, "const_added"); 3551 3552 $def(self, '$remove_class_variable', function $$remove_class_variable(name) { 3553 var self = this; 3554 3555 3556 $deny_frozen_access(self); 3557 name = $Opal['$class_variable_name!'](name); 3558 3559 if (Opal.hasOwnProperty.call(self.$$cvars, name)) { 3560 var value = self.$$cvars[name]; 3561 delete self.$$cvars[name]; 3562 return value; 3563 } else { 3564 $Kernel.$raise($$$('NameError'), "cannot remove " + (name) + " for " + (self)); 3565 } 3566 }); 3567 3568 $def(self, '$constants', function $$constants(inherit) { 3569 var self = this; 3570 3571 3572 if (inherit == null) inherit = true; 3573 return Opal.constants(self, inherit); }, -1); 3574 $defs(self, '$constants', function $$constants(inherit) { 3575 var self = this; 3576 3577 if (inherit == null) { 3578 var nesting = (self.$$nesting || []).concat($Object), 3579 constant, constants = {}, 3580 i, ii; 3581 3582 for(i = 0, ii = nesting.length; i < ii; i++) { 3583 for (constant in nesting[i].$$const) { 3584 constants[constant] = true; 3585 } 3586 } 3587 return Object.keys(constants); 3588 } else { 3589 return Opal.constants(self, inherit) 3590 } 3591 }, -1); 3592 $defs(self, '$nesting', function $$nesting() { 3593 var self = this; 3594 3595 return self.$$nesting || []; 3596 }); 3597 3598 $def(self, '$const_defined?', function $Module_const_defined$ques$9(name, inherit) { 3599 var self = this; 3600 3601 3602 if (inherit == null) inherit = true; 3603 name = $$('Opal')['$const_name!'](name); 3604 if (!$truthy(name['$=~']($$$($Opal, 'CONST_NAME_REGEXP')))) { 3605 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)); 3606 } 3607 var module, modules = [self], i, ii; 3608 3609 // Add up ancestors if inherit is true 3610 if (inherit) { 3611 modules = modules.concat(Opal.ancestors(self)); 3612 3613 // Add Object's ancestors if it's a module – modules have no ancestors otherwise 3614 if (self.$$is_module) { 3615 modules = modules.concat([$Object]).concat(Opal.ancestors($Object)); 3616 } 3617 } 3618 3619 for (i = 0, ii = modules.length; i < ii; i++) { 3620 module = modules[i]; 3621 if (module.$$const[name] != null) { return true; } 3622 if ( 3623 module.$$autoload && 3624 module.$$autoload[name] && 3625 !module.$$autoload[name].required && 3626 !module.$$autoload[name].success 3627 ) { 3628 return true; 3629 } 3630 } 3631 3632 return false; 3633 }, -2); 3634 3635 $def(self, '$const_get', function $$const_get(name, inherit) { 3636 var self = this; 3637 3638 3639 if (inherit == null) inherit = true; 3640 name = $$('Opal')['$const_name!'](name); 3641 3642 if (name.indexOf('::') === 0 && name !== '::'){ 3643 name = name.slice(2); 3644 } 3645 if ($truthy(name.indexOf('::') != -1 && name != '::')) { 3646 return $send(name.$split("::"), 'inject', [self], function $$10(o, c){ 3647 3648 if (o == null) o = nil; 3649 if (c == null) c = nil; 3650 return o.$const_get(c);}) 3651 } if (!$truthy(name['$=~']($$$($Opal, 'CONST_NAME_REGEXP')))) { 3652 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)); 3653 } 3654 if (inherit) { 3655 return Opal.$$([self], name); 3656 } else { 3657 return Opal.const_get_local(self, name); 3658 } 3659 }, -2); 3660 3661 $def(self, '$const_missing', function $$const_missing(name) { 3662 var self = this, full_const_name = nil; 3663 3664 3665 full_const_name = ($eqeq(self, $Object) ? (name) : ("" + (self) + "::" + (name))); 3666 return $Kernel.$raise($$$('NameError').$new("uninitialized constant " + (full_const_name), name)); 3667 }); 3668 3669 $def(self, '$const_set', function $$const_set(name, value) { 3670 var self = this; 3671 3672 3673 $deny_frozen_access(self); 3674 name = $Opal['$const_name!'](name); 3675 if (($truthy(name['$!~']($$$($Opal, 'CONST_NAME_REGEXP'))) || ($truthy(name['$start_with?']("::"))))) { 3676 $Kernel.$raise($$$('NameError').$new("wrong constant name " + (name), name)); 3677 } $const_set(self, name, value); 3678 return value; 3679 }); 3680 3681 $def(self, '$public_constant', $return_val(nil)); 3682 3683 $def(self, '$define_method', function $$define_method(name, method) { 3684 var block = $$define_method.$$p || nil, self = this, $ret_or_1 = nil, $ret_or_2 = nil; 3685 3686 $$define_method.$$p = null; 3687 3688 $deny_frozen_access(self); 3689 3690 if (method === undefined && block === nil) 3691 $Kernel.$raise($$$('ArgumentError'), "tried to create a Proc object without a block") 3692 ; 3693 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; 3694 3695 3696 $post_args = $slice(arguments); 3697 args = $post_args; 3698 bound = method.$bind(self); 3699 return $send(bound, 'call', $to_a(args));}, {$$arity: -1, $$s: self})) : ($Kernel.$raise($$$('TypeError'), "wrong argument type " + (block.$class()) + " (expected Proc/Method)")))))); 3700 3701 if (typeof(Proxy) !== 'undefined') { 3702 3703 block.$$proxy_target = block; 3704 block = new Proxy(block, { 3705 apply: function(target, self, args) { 3706 var old_name = target.$$jsid; 3707 target.$$jsid = name; 3708 try { 3709 return target.apply(self, args); 3710 } catch(e) { 3711 if (e === target.$$brk || e === target.$$ret) return e.$v; 3712 throw e; 3713 } finally { 3714 target.$$jsid = old_name; 3715 } 3716 } 3717 }); 3718 } 3719 3720 block.$$jsid = name; 3721 block.$$s = null; 3722 block.$$def = block; 3723 block.$$define_meth = true; 3724 3725 return Opal.defn(self, $jsid(name), block); 3726 }, -2); 3727 3728 $def(self, '$freeze', function $$freeze() { 3729 var self = this; 3730 3731 3732 if ($truthy(self['$frozen?']())) { 3733 return self 3734 } 3735 if (!self.hasOwnProperty('$$base_module')) { $prop(self, '$$base_module', null); } 3736 3737 return $freeze(self); 3738 }); 3739 3740 $def(self, '$remove_method', function $$remove_method($a) { 3741 var $post_args, names, self = this; 3742 3743 3744 $post_args = $slice(arguments); 3745 names = $post_args; 3746 3747 for (var i = 0; i < names.length; i++) { 3748 var name = names[i]; 3749 if (!(typeof name === "string" || name.$$is_string)) { 3750 self.$raise($$$('TypeError'), "" + (self.$name()) + " is not a symbol nor a string"); 3751 } 3752 $deny_frozen_access(self); 3753 3754 Opal.rdef(self, "$" + name); 3755 } 3756 return self; 3757 }, -1); 3758 3759 $def(self, '$singleton_class?', function $Module_singleton_class$ques$12() { 3760 var self = this; 3761 3762 return !!self.$$is_singleton; 3763 }); 3764 3765 $def(self, '$include', function $$include($a) { 3766 var $post_args, mods, self = this; 3767 3768 3769 $post_args = $slice(arguments); 3770 mods = $post_args; 3771 3772 for (var i = mods.length - 1; i >= 0; i--) { 3773 var mod = mods[i]; 3774 3775 if (!mod.$$is_module) { 3776 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 3777 } 3778 3779 (mod).$append_features(self); 3780 (mod).$included(self); 3781 } 3782 return self; 3783 }, -1); 3784 3785 $def(self, '$included_modules', function $$included_modules() { 3786 var self = this; 3787 3788 return Opal.included_modules(self); 3789 }); 3790 3791 $def(self, '$include?', function $Module_include$ques$13(mod) { 3792 var self = this; 3793 3794 3795 if (!mod.$$is_module) { 3796 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 3797 } 3798 3799 var i, ii, mod2, ancestors = Opal.ancestors(self); 3800 3801 for (i = 0, ii = ancestors.length; i < ii; i++) { 3802 mod2 = ancestors[i]; 3803 if (mod2 === mod && mod2 !== self) { 3804 return true; 3805 } 3806 } 3807 3808 return false; 3809 3810 }); 3811 3812 $def(self, '$instance_method', function $$instance_method(name) { 3813 var self = this; 3814 3815 3816 var meth = self.$$prototype[$jsid(name)]; 3817 3818 if (!meth || meth.$$stub) { 3819 $Kernel.$raise($$$('NameError').$new("undefined method `" + (name) + "' for class `" + (self.$name()) + "'", name)); 3820 } 3821 3822 return $$$('UnboundMethod').$new(self, meth.$$owner || self, meth, name); 3823 3824 }); 3825 3826 $def(self, '$instance_methods', function $$instance_methods(include_super) { 3827 var self = this; 3828 3829 3830 if (include_super == null) include_super = true; 3831 3832 if ($truthy(include_super)) { 3833 return Opal.instance_methods(self); 3834 } else { 3835 return Opal.own_instance_methods(self); 3836 } 3837 }, -1); 3838 3839 $def(self, '$included', $return_val(nil)); 3840 3841 $def(self, '$extended', $return_val(nil)); 3842 3843 $def(self, '$extend_object', function $$extend_object(object) { 3844 3845 3846 $deny_frozen_access(object); 3847 return nil; 3848 }); 3849 3850 $def(self, '$method_added', function $$method_added($a) { 3851 3852 3853 $slice(arguments); 3854 return nil; 3855 }, -1); 3856 3857 $def(self, '$method_removed', function $$method_removed($a) { 3858 3859 3860 $slice(arguments); 3861 return nil; 3862 }, -1); 3863 3864 $def(self, '$method_undefined', function $$method_undefined($a) { 3865 3866 3867 $slice(arguments); 3868 return nil; 3869 }, -1); 3870 3871 $def(self, '$module_eval', function $$module_eval($a) { 3872 var block = $$module_eval.$$p || nil, $post_args, args, $b, self = this, string = nil, file = nil, default_eval_options = nil, $ret_or_1 = nil, compiling_options = nil, compiled = nil; 3873 3874 $$module_eval.$$p = null; 3875 $post_args = $slice(arguments); 3876 args = $post_args; 3877 if (($truthy(block['$nil?']()) && ($truthy(!!Opal.compile)))) { 3878 3879 if (!$truthy($range(1, 3, false)['$cover?'](args.$size()))) { 3880 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)"); 3881 } $b = [].concat($to_a(args)), (string = ($b[0] == null ? nil : $b[0])), (file = ($b[1] == null ? nil : $b[1])), (($b[2] == null ? nil : $b[2])); 3882 default_eval_options = $hash2(["file", "eval"], {"file": ($truthy(($ret_or_1 = file)) ? ($ret_or_1) : ("(eval)")), "eval": true}); 3883 compiling_options = Opal.hash({ arity_check: false }).$merge(default_eval_options); 3884 compiled = $Opal.$compile(string, compiling_options); 3885 block = $send($Kernel, 'proc', [], function $$14(){var self = $$14.$$s == null ? this : $$14.$$s; 3886 3887 return new Function("Opal,self", "return " + compiled)(Opal, self);}, {$$s: self}); 3888 } else if ($truthy(args['$any?']())) { 3889 $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"); 3890 } 3891 var old = block.$$s, 3892 result; 3893 3894 block.$$s = null; 3895 result = block.apply(self, [self]); 3896 block.$$s = old; 3897 3898 return result; 3899 }, -1); 3900 3901 $def(self, '$module_exec', function $$module_exec($a) { 3902 var block = $$module_exec.$$p || nil, $post_args, args, self = this; 3903 3904 $$module_exec.$$p = null; 3905 $post_args = $slice(arguments); 3906 args = $post_args; 3907 3908 if (block === nil) { 3909 $Kernel.$raise($$$('LocalJumpError'), "no block given"); 3910 } 3911 3912 var block_self = block.$$s, result; 3913 3914 block.$$s = null; 3915 result = block.apply(self, args); 3916 block.$$s = block_self; 3917 3918 return result; 3919 }, -1); 3920 3921 $def(self, '$method_defined?', function $Module_method_defined$ques$15(method) { 3922 var self = this; 3923 3924 3925 var body = self.$$prototype[$jsid(method)]; 3926 return (!!body) && !body.$$stub; 3927 3928 }); 3929 3930 $def(self, '$module_function', function $$module_function($a) { 3931 var $post_args, methods, self = this; 3932 3933 3934 $post_args = $slice(arguments); 3935 methods = $post_args; 3936 3937 $deny_frozen_access(self); 3938 3939 if (methods.length === 0) { 3940 self.$$module_function = true; 3941 return nil; 3942 } 3943 else { 3944 for (var i = 0, length = methods.length; i < length; i++) { 3945 var meth = methods[i], 3946 id = $jsid(meth), 3947 func = self.$$prototype[id]; 3948 3949 Opal.defs(self, id, func); 3950 } 3951 return methods.length === 1 ? methods[0] : methods; 3952 } 3953 }, -1); 3954 3955 $def(self, '$name', function $$name() { 3956 var self = this; 3957 3958 3959 if (self.$$full_name) { 3960 return self.$$full_name; 3961 } 3962 3963 var result = [], base = self; 3964 3965 while (base) { 3966 // Give up if any of the ancestors is unnamed 3967 if (base.$$name === nil || base.$$name == null) return nil; 3968 3969 result.unshift(base.$$name); 3970 3971 base = base.$$base_module; 3972 3973 if (base === $Object) { 3974 break; 3975 } 3976 } 3977 3978 if (result.length === 0) { 3979 return nil; 3980 } 3981 3982 return self.$$full_name = result.join('::'); 3983 3984 }); 3985 3986 $def(self, '$prepend', function $$prepend($a) { 3987 var $post_args, mods, self = this; 3988 3989 3990 $post_args = $slice(arguments); 3991 mods = $post_args; 3992 3993 if (mods.length === 0) { 3994 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (given 0, expected 1+)"); 3995 } 3996 3997 for (var i = mods.length - 1; i >= 0; i--) { 3998 var mod = mods[i]; 3999 4000 if (!mod.$$is_module) { 4001 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 4002 } 4003 4004 (mod).$prepend_features(self); 4005 (mod).$prepended(self); 4006 } 4007 return self; 4008 }, -1); 4009 4010 $def(self, '$prepend_features', function $$prepend_features(prepender) { 4011 var self = this; 4012 4013 4014 4015 $deny_frozen_access(prepender); 4016 4017 if (!self.$$is_module) { 4018 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (self.$class()) + " (expected Module)"); 4019 } 4020 4021 Opal.prepend_features(self, prepender) 4022 ; 4023 return self; 4024 }); 4025 4026 $def(self, '$prepended', $return_val(nil)); 4027 4028 $def(self, '$remove_const', function $$remove_const(name) { 4029 var self = this; 4030 4031 4032 $deny_frozen_access(self); 4033 return Opal.const_remove(self, name); }); 4034 4035 $def(self, '$to_s', function $$to_s() { 4036 var self = this, $ret_or_1 = nil; 4037 4038 if ($truthy(($ret_or_1 = Opal.Module.$name.call(self)))) { 4039 return $ret_or_1 4040 } else { 4041 return "#<" + (self.$$is_module ? 'Module' : 'Class') + ":0x" + (self.$__id__().$to_s(16)) + ">" 4042 } 4043 }); 4044 4045 $def(self, '$undef_method', function $$undef_method($a) { 4046 var $post_args, names, self = this; 4047 4048 4049 $post_args = $slice(arguments); 4050 names = $post_args; 4051 4052 for (var i = 0; i < names.length; i++) { 4053 var name = names[i]; 4054 if (!(typeof name === "string" || name.$$is_string)) { 4055 self.$raise($$$('TypeError'), "" + (self.$name()) + " is not a symbol nor a string"); 4056 } 4057 $deny_frozen_access(self); 4058 4059 Opal.udef(self, "$" + names[i]); 4060 } 4061 return self; 4062 }, -1); 4063 4064 $def(self, '$instance_variables', function $$instance_variables() { 4065 var self = this, consts = nil; 4066 4067 4068 consts = (Opal.Module.$$nesting = $nesting, self.$constants()); 4069 4070 var result = []; 4071 4072 for (var name in self) { 4073 if (self.hasOwnProperty(name) && name.charAt(0) !== '$' && name !== 'constructor' && !consts['$include?'](name)) { 4074 result.push('@' + name); 4075 } 4076 } 4077 4078 return result; 4079 }); 4080 4081 $def(self, '$dup', function $$dup() { 4082 var $yield = $$dup.$$p || nil, self = this, copy = nil; 4083 4084 $$dup.$$p = null; 4085 4086 copy = $send2(self, $find_super(self, 'dup', $$dup, false, true), 'dup', [], $yield); 4087 copy.$copy_class_variables(self); 4088 copy.$copy_constants(self); 4089 return copy; 4090 }); 4091 4092 $def(self, '$copy_class_variables', function $$copy_class_variables(other) { 4093 var self = this; 4094 4095 4096 for (var name in other.$$cvars) { 4097 self.$$cvars[name] = other.$$cvars[name]; 4098 } 4099 4100 }); 4101 4102 $def(self, '$copy_constants', function $$copy_constants(other) { 4103 var self = this; 4104 4105 4106 var name, other_constants = other.$$const; 4107 4108 for (name in other_constants) { 4109 $const_set(self, name, other_constants[name]); 4110 } 4111 4112 }); 4113 4114 $def(self, '$refine', function $$refine(klass) { 4115 var block = $$refine.$$p || nil, $a, self = this, refinement_module = nil, m = nil, klass_id = nil; 4116 4117 $$refine.$$p = null; 4118 $a = [self, nil, nil], (refinement_module = $a[0]), (m = $a[1]), (klass_id = $a[2]); 4119 4120 klass_id = Opal.id(klass); 4121 if (typeof self.$$refine_modules === "undefined") { 4122 self.$$refine_modules = Object.create(null); 4123 } 4124 if (typeof self.$$refine_modules[klass_id] === "undefined") { 4125 m = self.$$refine_modules[klass_id] = $$$('Refinement').$new(); 4126 } 4127 else { 4128 m = self.$$refine_modules[klass_id]; 4129 } 4130 m.refinement_module = refinement_module; 4131 m.refined_class = klass 4132 ; 4133 $send(m, 'class_exec', [], block.$to_proc()); 4134 return m; 4135 }); 4136 4137 $def(self, '$refinements', function $$refinements() { 4138 var self = this; 4139 4140 4141 var refine_modules = self.$$refine_modules, hash = $hash2([], {}); if (typeof refine_modules === "undefined") return hash; 4142 for (var id in refine_modules) { 4143 hash['$[]='](refine_modules[id].refined_class, refine_modules[id]); 4144 } 4145 return hash; 4146 4147 }); 4148 4149 $def(self, '$using', function $$using(mod) { 4150 4151 return $Kernel.$raise("Module#using is not permitted in methods") 4152 }); 4153 $alias(self, "class_eval", "module_eval"); 4154 $alias(self, "class_exec", "module_exec"); 4155 return $alias(self, "inspect", "to_s"); 4156 })('::', null, $nesting); 4157 return (function($base, $super) { 4158 var self = $klass($base, $super, 'Refinement'); 4159 4160 var $proto = self.$$prototype; 4161 4162 $proto.refinement_module = $proto.refined_class = nil; 4163 4164 self.$attr_reader("refined_class"); 4165 return $def(self, '$inspect', function $$inspect() { 4166 var $yield = $$inspect.$$p || nil, self = this; 4167 4168 $$inspect.$$p = null; 4169 if ($truthy(self.refinement_module)) { 4170 return "#<refinement:" + (self.refined_class.$inspect()) + "@" + (self.refinement_module.$inspect()) + ">" 4171 } else { 4172 return $send2(self, $find_super(self, 'inspect', $$inspect, false, true), 'inspect', [], $yield) 4173 } 4174 }); 4175 })('::', $Module); 4176}; 4177 4178Opal.modules["corelib/class"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4179 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.$$$; 4180 4181 Opal.add_stubs('require,class_eval,to_proc,+,subclasses,flatten,map,initialize_copy,allocate,name,to_s,raise'); 4182 4183 self.$require("corelib/module"); 4184 return (function($base, $super, $parent_nesting) { 4185 var self = $klass($base, $super, 'Class'); 4186 4187 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 4188 4189 4190 $defs(self, '$new', function $Class_new$1(superclass) { 4191 var block = $Class_new$1.$$p || nil; 4192 4193 $Class_new$1.$$p = null; 4194 if (superclass == null) superclass = $$('Object'); 4195 4196 if (!superclass.$$is_class) { 4197 throw Opal.TypeError.$new("superclass must be a Class"); 4198 } 4199 4200 var klass = Opal.allocate_class(nil, superclass); 4201 superclass.$inherited(klass); 4202 ((block !== nil) ? ($send((klass), 'class_eval', [], block.$to_proc())) : nil); 4203 return klass; 4204 }, -1); 4205 4206 $def(self, '$allocate', function $$allocate() { 4207 var self = this; 4208 4209 4210 var obj = new self.$$constructor(); 4211 obj.$$id = Opal.uid(); 4212 return obj; 4213 4214 }); 4215 4216 $def(self, '$descendants', function $$descendants() { 4217 var self = this; 4218 4219 return $rb_plus(self.$subclasses(), $send(self.$subclasses(), 'map', [], "descendants".$to_proc()).$flatten()) 4220 }); 4221 4222 $def(self, '$inherited', $return_val(nil)); 4223 4224 $def(self, '$initialize_dup', function $$initialize_dup(original) { 4225 var self = this; 4226 4227 4228 self.$initialize_copy(original); 4229 4230 self.$$name = null; 4231 self.$$full_name = null; 4232 }); 4233 4234 $def(self, '$new', function $Class_new$2($a) { 4235 var block = $Class_new$2.$$p || nil, $post_args, args, self = this; 4236 4237 $Class_new$2.$$p = null; 4238 $post_args = $slice(arguments); 4239 args = $post_args; 4240 4241 var object = self.$allocate(); 4242 Opal.send(object, object.$initialize, args, block); 4243 return object; 4244 }, -1); 4245 4246 $def(self, '$subclasses', function $$subclasses() { 4247 var self = this; 4248 4249 4250 if (typeof WeakRef !== 'undefined') { 4251 var i, subclass, out = []; 4252 for (i = 0; i < self.$$subclasses.length; i++) { 4253 subclass = self.$$subclasses[i].deref(); 4254 if (subclass !== undefined) { 4255 out.push(subclass); 4256 } 4257 } 4258 return out; 4259 } 4260 else { 4261 return self.$$subclasses; 4262 } 4263 4264 }); 4265 4266 $def(self, '$superclass', function $$superclass() { 4267 var self = this; 4268 4269 return self.$$super || nil; 4270 }); 4271 4272 $def(self, '$to_s', function $$to_s() { 4273 var self = this; 4274 4275 $$to_s.$$p = null; 4276 4277 var singleton_of = self.$$singleton_of; 4278 4279 if (singleton_of && singleton_of.$$is_a_module) { 4280 return "#<Class:" + ((singleton_of).$name()) + ">"; 4281 } 4282 else if (singleton_of) { 4283 // a singleton class created from an object 4284 return "#<Class:#<" + ((singleton_of.$$class).$name()) + ":0x" + ((Opal.id(singleton_of)).$to_s(16)) + ">>"; 4285 } 4286 4287 return $send2(self, $find_super(self, 'to_s', $$to_s, false, true), 'to_s', [], null); 4288 4289 }); 4290 4291 $def(self, '$attached_object', function $$attached_object() { 4292 var self = this; 4293 4294 4295 if (self.$$singleton_of != null) { 4296 return self.$$singleton_of; 4297 } 4298 else { 4299 $Kernel.$raise($$$('TypeError'), "`" + (self) + "' is not a singleton class"); 4300 } 4301 4302 }); 4303 return $alias(self, "inspect", "to_s"); 4304 })('::', null, $nesting); 4305}; 4306 4307Opal.modules["corelib/basic_object"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4308 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.$$$; 4309 4310 Opal.add_stubs('==,raise,inspect,pristine,!,nil?,cover?,size,merge,compile,proc,[],first,>=,length,instance_variable_get,any?,new,caller'); 4311 return (function($base, $super) { 4312 var self = $klass($base, $super, 'BasicObject'); 4313 4314 4315 4316 4317 $def(self, '$initialize', function $$initialize($a) { 4318 4319 4320 $slice(arguments); 4321 return nil; 4322 }, -1); 4323 4324 $def(self, '$==', function $BasicObject_$eq_eq$1(other) { 4325 var self = this; 4326 4327 return self === other; 4328 }); 4329 4330 $def(self, '$eql?', function $BasicObject_eql$ques$2(other) { 4331 var self = this; 4332 4333 return self['$=='](other) 4334 }); 4335 $alias(self, "equal?", "=="); 4336 4337 $def(self, '$__id__', function $$__id__() { 4338 var self = this; 4339 4340 4341 if (self.$$id != null) { 4342 return self.$$id; 4343 } 4344 Opal.prop(self, '$$id', Opal.uid()); 4345 return self.$$id; 4346 4347 }); 4348 4349 $def(self, '$__send__', function $$__send__(symbol, $a) { 4350 var block = $$__send__.$$p || nil, $post_args, args, self = this; 4351 4352 $$__send__.$$p = null; 4353 $post_args = $slice(arguments, 1); 4354 args = $post_args; 4355 4356 if (!symbol.$$is_string) { 4357 self.$raise($$$('TypeError'), "" + (self.$inspect()) + " is not a symbol nor a string"); 4358 } 4359 4360 var func = self[Opal.jsid(symbol)]; 4361 4362 if (func) { 4363 if (block !== nil) { 4364 func.$$p = block; 4365 } 4366 4367 return func.apply(self, args); 4368 } 4369 4370 if (block !== nil) { 4371 self.$method_missing.$$p = block; 4372 } 4373 4374 return self.$method_missing.apply(self, [symbol].concat(args)); 4375 }, -2); 4376 4377 $def(self, '$!', $return_val(false)); 4378 $Opal.$pristine("!"); 4379 4380 $def(self, '$!=', function $BasicObject_$not_eq$3(other) { 4381 var self = this; 4382 4383 return self['$=='](other)['$!']() 4384 }); 4385 4386 $def(self, '$instance_eval', function $$instance_eval($a) { 4387 var block = $$instance_eval.$$p || nil, $post_args, args, $b, self = this, string = nil, file = nil, default_eval_options = nil, $ret_or_1 = nil, compiling_options = nil, compiled = nil; 4388 4389 $$instance_eval.$$p = null; 4390 $post_args = $slice(arguments); 4391 args = $post_args; 4392 if (($truthy(block['$nil?']()) && ($truthy(!!Opal.compile)))) { 4393 4394 if (!$truthy($range(1, 3, false)['$cover?'](args.$size()))) { 4395 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)"); 4396 } $b = [].concat($to_a(args)), (string = ($b[0] == null ? nil : $b[0])), (file = ($b[1] == null ? nil : $b[1])), (($b[2] == null ? nil : $b[2])); 4397 default_eval_options = $hash2(["file", "eval"], {"file": ($truthy(($ret_or_1 = file)) ? ($ret_or_1) : ("(eval)")), "eval": true}); 4398 compiling_options = Opal.hash({ arity_check: false }).$merge(default_eval_options); 4399 compiled = $Opal.$compile(string, compiling_options); 4400 block = $send($Kernel, 'proc', [], function $$4(){var self = $$4.$$s == null ? this : $$4.$$s; 4401 4402 return new Function("Opal,self", "return " + compiled)(Opal, self);}, {$$s: self}); 4403 } else if ((($truthy(block['$nil?']()) && ($truthy($rb_ge(args.$length(), 1)))) && ($eqeq(args.$first()['$[]'](0), "@")))) { 4404 return self.$instance_variable_get(args.$first()) 4405 } else if ($truthy(args['$any?']())) { 4406 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$size()) + " for 0)"); 4407 } 4408 var old = block.$$s, 4409 result; 4410 4411 block.$$s = null; 4412 4413 // Need to pass $$eval so that method definitions know if this is 4414 // being done on a class/module. Cannot be compiler driven since 4415 // send(:instance_eval) needs to work. 4416 if (self.$$is_a_module) { 4417 self.$$eval = true; 4418 try { 4419 result = block.call(self, self); 4420 } 4421 finally { 4422 self.$$eval = false; 4423 } 4424 } 4425 else { 4426 result = block.call(self, self); 4427 } 4428 4429 block.$$s = old; 4430 4431 return result; 4432 }, -1); 4433 4434 $def(self, '$instance_exec', function $$instance_exec($a) { 4435 var block = $$instance_exec.$$p || nil, $post_args, args, self = this; 4436 4437 $$instance_exec.$$p = null; 4438 $post_args = $slice(arguments); 4439 args = $post_args; 4440 if (!$truthy(block)) { 4441 $Kernel.$raise($$$('ArgumentError'), "no block given"); 4442 } 4443 var block_self = block.$$s, 4444 result; 4445 4446 block.$$s = null; 4447 4448 if (self.$$is_a_module) { 4449 self.$$eval = true; 4450 try { 4451 result = block.apply(self, args); 4452 } 4453 finally { 4454 self.$$eval = false; 4455 } 4456 } 4457 else { 4458 result = block.apply(self, args); 4459 } 4460 4461 block.$$s = block_self; 4462 4463 return result; 4464 }, -1); 4465 4466 $def(self, '$singleton_method_added', function $$singleton_method_added($a) { 4467 4468 4469 $slice(arguments); 4470 return nil; 4471 }, -1); 4472 4473 $def(self, '$singleton_method_removed', function $$singleton_method_removed($a) { 4474 4475 4476 $slice(arguments); 4477 return nil; 4478 }, -1); 4479 4480 $def(self, '$singleton_method_undefined', function $$singleton_method_undefined($a) { 4481 4482 4483 $slice(arguments); 4484 return nil; 4485 }, -1); 4486 4487 $def(self, '$method_missing', function $$method_missing(symbol, $a) { 4488 var $post_args, args, self = this, inspect_result = nil; 4489 4490 $$method_missing.$$p = null; 4491 $post_args = $slice(arguments, 1); 4492 args = $post_args; 4493 inspect_result = $Opal.$inspect(self); 4494 return $Kernel.$raise($$$('NoMethodError').$new("undefined method `" + (symbol) + "' for " + (inspect_result), symbol, args), nil, $Kernel.$caller(1)); 4495 }, -2); 4496 $Opal.$pristine(self, "method_missing"); 4497 return $def(self, '$respond_to_missing?', function $BasicObject_respond_to_missing$ques$5(method_name, include_all) { 4498 return false; 4499 }, -2); 4500 })('::', null) 4501}; 4502 4503Opal.modules["corelib/kernel"] = function(Opal) {/* Generated by Opal 1.7.3 */ 4504 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.$$$; 4505 4506 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'); 4507 4508 (function($base, $parent_nesting) { 4509 var self = $module($base, 'Kernel'); 4510 4511 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 4512 4513 4514 4515 $def(self, '$=~', $return_val(false)); 4516 4517 $def(self, '$!~', function $Kernel_$excl_tilde$1(obj) { 4518 var self = this; 4519 4520 return self['$=~'](obj)['$!']() 4521 }); 4522 4523 $def(self, '$===', function $Kernel_$eq_eq_eq$2(other) { 4524 var self = this, $ret_or_1 = nil; 4525 4526 if ($truthy(($ret_or_1 = self.$object_id()['$=='](other.$object_id())))) { 4527 return $ret_or_1 4528 } else { 4529 return self['$=='](other) 4530 } 4531 }); 4532 4533 $def(self, '$<=>', function $Kernel_$lt_eq_gt$3(other) { 4534 var self = this; 4535 4536 4537 // set guard for infinite recursion 4538 self.$$comparable = true; 4539 4540 var x = self['$=='](other); 4541 4542 if (x && x !== nil) { 4543 return 0; 4544 } 4545 4546 return nil; 4547 4548 }); 4549 4550 $def(self, '$method', function $$method(name) { 4551 var self = this; 4552 4553 4554 var meth = self[$jsid(name)]; 4555 4556 if (!meth || meth.$$stub) { 4557 $Kernel.$raise($$$('NameError').$new("undefined method `" + (name) + "' for class `" + (self.$class()) + "'", name)); 4558 } 4559 4560 return $$$('Method').$new(self, meth.$$owner || self.$class(), meth, name); 4561 4562 }); 4563 4564 $def(self, '$methods', function $$methods(all) { 4565 var self = this; 4566 4567 4568 if (all == null) all = true; 4569 4570 if ($truthy(all)) { 4571 return Opal.methods(self); 4572 } else { 4573 return Opal.own_methods(self); 4574 } 4575 }, -1); 4576 4577 $def(self, '$public_methods', function $$public_methods(all) { 4578 var self = this; 4579 4580 4581 if (all == null) all = true; 4582 4583 if ($truthy(all)) { 4584 return Opal.methods(self); 4585 } else { 4586 return Opal.receiver_methods(self); 4587 } 4588 }, -1); 4589 4590 $def(self, '$Array', function $$Array(object) { 4591 4592 4593 var coerced; 4594 4595 if (object === nil) { 4596 return []; 4597 } 4598 4599 if (object.$$is_array) { 4600 return object; 4601 } 4602 4603 coerced = $Opal['$coerce_to?'](object, $$$('Array'), "to_ary"); 4604 if (coerced !== nil) { return coerced; } 4605 4606 coerced = $Opal['$coerce_to?'](object, $$$('Array'), "to_a"); 4607 if (coerced !== nil) { return coerced; } 4608 4609 return [object]; 4610 4611 }); 4612 4613 $def(self, '$at_exit', function $$at_exit() { 4614 var block = $$at_exit.$$p || nil, $ret_or_1 = nil; 4615 if ($gvars.__at_exit__ == null) $gvars.__at_exit__ = nil; 4616 4617 $$at_exit.$$p = null; 4618 $gvars.__at_exit__ = ($truthy(($ret_or_1 = $gvars.__at_exit__)) ? ($ret_or_1) : ([])); 4619 $gvars.__at_exit__['$<<'](block); 4620 return block; 4621 }); 4622 4623 $def(self, '$caller', function $$caller(start, length) { 4624 4625 4626 if (start == null) start = 1; 4627 if (length == null) length = nil; 4628 4629 var stack, result; 4630 4631 stack = new Error().$backtrace(); 4632 result = []; 4633 4634 for (var i = start + 1, ii = stack.length; i < ii; i++) { 4635 if (!stack[i].match(/runtime\.js/)) { 4636 result.push(stack[i]); 4637 } 4638 } 4639 if (length != nil) result = result.slice(0, length); 4640 return result; 4641 }, -1); 4642 4643 $def(self, '$caller_locations', function $$caller_locations($a) { 4644 var $post_args, args, self = this; 4645 4646 4647 $post_args = $slice(arguments); 4648 args = $post_args; 4649 return $send($send(self, 'caller', $to_a(args)), 'map', [], function $$4(loc){ 4650 4651 if (loc == null) loc = nil; 4652 return $$$($$$($$$('Thread'), 'Backtrace'), 'Location').$new(loc);}); 4653 }, -1); 4654 4655 $def(self, '$class', function $Kernel_class$5() { 4656 var self = this; 4657 4658 return self.$$class; 4659 }); 4660 4661 $def(self, '$copy_instance_variables', function $$copy_instance_variables(other) { 4662 var self = this; 4663 4664 4665 var keys = Object.keys(other), i, ii, name; 4666 for (i = 0, ii = keys.length; i < ii; i++) { 4667 name = keys[i]; 4668 if (name.charAt(0) !== '$' && other.hasOwnProperty(name)) { 4669 self[name] = other[name]; 4670 } 4671 } 4672 4673 }); 4674 4675 $def(self, '$copy_singleton_methods', function $$copy_singleton_methods(other) { 4676 var self = this; 4677 4678 4679 var i, name, names, length; 4680 4681 if (other.hasOwnProperty('$$meta') && other.$$meta !== null) { 4682 var other_singleton_class = Opal.get_singleton_class(other); 4683 var self_singleton_class = Opal.get_singleton_class(self); 4684 names = Object.getOwnPropertyNames(other_singleton_class.$$prototype); 4685 4686 for (i = 0, length = names.length; i < length; i++) { 4687 name = names[i]; 4688 if (Opal.is_method(name)) { 4689 self_singleton_class.$$prototype[name] = other_singleton_class.$$prototype[name]; 4690 } 4691 } 4692 4693 self_singleton_class.$$const = Object.assign({}, other_singleton_class.$$const); 4694 Object.setPrototypeOf( 4695 self_singleton_class.$$prototype, 4696 Object.getPrototypeOf(other_singleton_class.$$prototype) 4697 ); 4698 } 4699 4700 for (i = 0, names = Object.getOwnPropertyNames(other), length = names.length; i < length; i++) { 4701 name = names[i]; 4702 if (name.charAt(0) === '$' && name.charAt(1) !== '$' && other.hasOwnProperty(name)) { 4703 self[name] = other[name]; 4704 } 4705 } 4706 4707 }); 4708 4709 $def(self, '$clone', function $$clone($kwargs) { 4710 var freeze, self = this, copy = nil; 4711 4712 4713 $kwargs = $ensure_kwargs($kwargs); 4714 4715 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = nil; 4716 if (!(($truthy(freeze['$nil?']()) || ($eqeq(freeze, true))) || ($eqeq(freeze, false)))) { 4717 self.$raise($$('ArgumentError'), "unexpected value for freeze: " + (freeze.$class())); 4718 } copy = self.$class().$allocate(); 4719 copy.$copy_instance_variables(self); 4720 copy.$copy_singleton_methods(self); 4721 copy.$initialize_clone(self, $hash2(["freeze"], {"freeze": freeze})); 4722 if (($eqeq(freeze, true) || (($truthy(freeze['$nil?']()) && ($truthy(self['$frozen?']())))))) { 4723 copy.$freeze(); 4724 } return copy; 4725 }, -1); 4726 4727 $def(self, '$initialize_clone', function $$initialize_clone(other, $kwargs) { 4728 var self = this; 4729 4730 4731 $kwargs = $ensure_kwargs($kwargs); 4732 4733 $kwargs.$$smap["freeze"]; self.$initialize_copy(other); 4734 return self; 4735 }, -2); 4736 4737 $def(self, '$define_singleton_method', function $$define_singleton_method(name, method) { 4738 var block = $$define_singleton_method.$$p || nil, self = this; 4739 4740 $$define_singleton_method.$$p = null; 4741 return $send(self.$singleton_class(), 'define_method', [name, method], block.$to_proc()); 4742 }, -2); 4743 4744 $def(self, '$dup', function $$dup() { 4745 var self = this, copy = nil; 4746 4747 4748 copy = self.$class().$allocate(); 4749 copy.$copy_instance_variables(self); 4750 copy.$initialize_dup(self); 4751 return copy; 4752 }); 4753 4754 $def(self, '$initialize_dup', function $$initialize_dup(other) { 4755 var self = this; 4756 4757 return self.$initialize_copy(other) 4758 }); 4759 4760 $def(self, '$enum_for', function $$enum_for($a, $b) { 4761 var block = $$enum_for.$$p || nil, $post_args, method, args, self = this; 4762 4763 $$enum_for.$$p = null; 4764 $post_args = $slice(arguments); 4765 4766 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 4767 args = $post_args; 4768 return $send($$$('Enumerator'), 'for', [self, method].concat($to_a(args)), block.$to_proc()); 4769 }, -1); 4770 4771 $def(self, '$equal?', function $Kernel_equal$ques$6(other) { 4772 var self = this; 4773 4774 return self === other; 4775 }); 4776 4777 $def(self, '$exit', function $$exit(status) { 4778 var $ret_or_1 = nil, block = nil; 4779 if ($gvars.__at_exit__ == null) $gvars.__at_exit__ = nil; 4780 4781 4782 if (status == null) status = true; 4783 $gvars.__at_exit__ = ($truthy(($ret_or_1 = $gvars.__at_exit__)) ? ($ret_or_1) : ([])); 4784 while (!($truthy($gvars.__at_exit__['$empty?']()))) { 4785 4786 block = $gvars.__at_exit__.$pop(); 4787 block.$call(); 4788 } 4789 if (status.$$is_boolean) { 4790 status = status ? 0 : 1; 4791 } else { 4792 status = $coerce_to(status, $$$('Integer'), 'to_int'); 4793 } 4794 4795 Opal.exit(status); 4796 return nil; 4797 }, -1); 4798 4799 $def(self, '$extend', function $$extend($a) { 4800 var $post_args, mods, self = this; 4801 4802 4803 $post_args = $slice(arguments); 4804 mods = $post_args; 4805 4806 if (mods.length == 0) { 4807 self.$raise($$$('ArgumentError'), "wrong number of arguments (given 0, expected 1+)"); 4808 } 4809 4810 $deny_frozen_access(self); 4811 4812 var singleton = self.$singleton_class(); 4813 4814 for (var i = mods.length - 1; i >= 0; i--) { 4815 var mod = mods[i]; 4816 4817 if (!mod.$$is_module) { 4818 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((mod).$class()) + " (expected Module)"); 4819 } 4820 4821 (mod).$append_features(singleton); 4822 (mod).$extend_object(self); 4823 (mod).$extended(self); 4824 } 4825 return self; 4826 }, -1); 4827 4828 $def(self, '$freeze', function $$freeze() { 4829 var self = this; 4830 4831 4832 if ($truthy(self['$frozen?']())) { 4833 return self 4834 } 4835 if (typeof(self) === "object") { 4836 $freeze_props(self); 4837 return $freeze(self); 4838 } 4839 return self; 4840 }); 4841 4842 $def(self, '$frozen?', function $Kernel_frozen$ques$7() { 4843 var self = this; 4844 4845 4846 switch (typeof(self)) { 4847 case "string": 4848 case "symbol": 4849 case "number": 4850 case "boolean": 4851 return true; 4852 case "object": 4853 return (self.$$frozen || false); 4854 default: 4855 return false; 4856 } 4857 4858 }); 4859 4860 $def(self, '$gets', function $$gets($a) { 4861 var $post_args, args; 4862 if ($gvars.stdin == null) $gvars.stdin = nil; 4863 4864 4865 $post_args = $slice(arguments); 4866 args = $post_args; 4867 return $send($gvars.stdin, 'gets', $to_a(args)); 4868 }, -1); 4869 4870 $def(self, '$hash', function $$hash() { 4871 var self = this; 4872 4873 return self.$__id__() 4874 }); 4875 4876 $def(self, '$initialize_copy', $return_val(nil)); 4877 var inspect_stack = []; 4878 4879 $def(self, '$inspect', function $$inspect() { 4880 var self = this, ivs = nil, id = nil, pushed = nil, e = nil; 4881 4882 return (function() { try { 4883 try { 4884 4885 ivs = ""; 4886 id = self.$__id__(); 4887 if ($truthy((inspect_stack)['$include?'](id))) { 4888 ivs = " ..."; 4889 } else { 4890 4891 (inspect_stack)['$<<'](id); 4892 pushed = true; 4893 $send(self.$instance_variables(), 'each', [], function $$8(i){var self = $$8.$$s == null ? this : $$8.$$s, ivar = nil, inspect = nil; 4894 4895 4896 if (i == null) i = nil; 4897 ivar = self.$instance_variable_get(i); 4898 inspect = $$('Opal').$inspect(ivar); 4899 return (ivs = $rb_plus(ivs, " " + (i) + "=" + (inspect)));}, {$$s: self}); 4900 }; 4901 return "#<" + (self.$class()) + ":0x" + (id.$to_s(16)) + (ivs) + ">"; 4902 } catch ($err) { 4903 if (Opal.rescue($err, [$$('StandardError')])) {(e = $err); 4904 try { 4905 return "#<" + (self.$class()) + ":0x" + (id.$to_s(16)) + ">" 4906 } finally { Opal.pop_exception(); } 4907 } else { throw $err; } 4908 } 4909 } finally { 4910 ($truthy(pushed) ? ((inspect_stack).$pop()) : nil); 4911 } })() 4912 }); 4913 4914 $def(self, '$instance_of?', function $Kernel_instance_of$ques$9(klass) { 4915 var self = this; 4916 4917 4918 if (!klass.$$is_class && !klass.$$is_module) { 4919 $Kernel.$raise($$$('TypeError'), "class or module required"); 4920 } 4921 4922 return self.$$class === klass; 4923 4924 }); 4925 4926 $def(self, '$instance_variable_defined?', function $Kernel_instance_variable_defined$ques$10(name) { 4927 var self = this; 4928 4929 4930 name = $Opal['$instance_variable_name!'](name); 4931 return Opal.hasOwnProperty.call(self, name.substr(1)); }); 4932 4933 $def(self, '$instance_variable_get', function $$instance_variable_get(name) { 4934 var self = this; 4935 4936 4937 name = $Opal['$instance_variable_name!'](name); 4938 4939 var ivar = self[Opal.ivar(name.substr(1))]; 4940 4941 return ivar == null ? nil : ivar; 4942 }); 4943 4944 $def(self, '$instance_variable_set', function $$instance_variable_set(name, value) { 4945 var self = this; 4946 4947 4948 $deny_frozen_access(self); 4949 name = $Opal['$instance_variable_name!'](name); 4950 return self[Opal.ivar(name.substr(1))] = value; }); 4951 4952 $def(self, '$remove_instance_variable', function $$remove_instance_variable(name) { 4953 var self = this; 4954 4955 4956 name = $Opal['$instance_variable_name!'](name); 4957 4958 var key = Opal.ivar(name.substr(1)), 4959 val; 4960 if (self.hasOwnProperty(key)) { 4961 val = self[key]; 4962 delete self[key]; 4963 return val; 4964 } 4965 return $Kernel.$raise($$$('NameError'), "instance variable " + (name) + " not defined"); 4966 }); 4967 4968 $def(self, '$instance_variables', function $$instance_variables() { 4969 var self = this; 4970 4971 4972 var result = [], ivar; 4973 4974 for (var name in self) { 4975 if (self.hasOwnProperty(name) && name.charAt(0) !== '$') { 4976 if (name.substr(-1) === '$') { 4977 ivar = name.slice(0, name.length - 1); 4978 } else { 4979 ivar = name; 4980 } 4981 result.push('@' + ivar); 4982 } 4983 } 4984 4985 return result; 4986 4987 }); 4988 4989 $def(self, '$Integer', function $$Integer(value, base) { 4990 4991 var i, str, base_digits; 4992 4993 if (!value.$$is_string) { 4994 if (base !== undefined) { 4995 $Kernel.$raise($$$('ArgumentError'), "base specified for non string value"); 4996 } 4997 if (value === nil) { 4998 $Kernel.$raise($$$('TypeError'), "can't convert nil into Integer"); 4999 } 5000 if (value.$$is_number) { 5001 if (value === Infinity || value === -Infinity || isNaN(value)) { 5002 $Kernel.$raise($$$('FloatDomainError'), value); 5003 } 5004 return Math.floor(value); 5005 } 5006 if (value['$respond_to?']("to_int")) { 5007 i = value.$to_int(); 5008 if (i !== nil) { 5009 return i; 5010 } 5011 } 5012 return $Opal['$coerce_to!'](value, $$$('Integer'), "to_i"); 5013 } 5014 5015 if (value === "0") { 5016 return 0; 5017 } 5018 5019 if (base === undefined) { 5020 base = 0; 5021 } else { 5022 base = $coerce_to(base, $$$('Integer'), 'to_int'); 5023 if (base === 1 || base < 0 || base > 36) { 5024 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)); 5025 } 5026 } 5027 5028 str = value.toLowerCase(); 5029 5030 str = str.replace(/(\d)_(?=\d)/g, '$1'); 5031 5032 str = str.replace(/^(\s*[+-]?)(0[bodx]?)/, function (_, head, flag) { 5033 switch (flag) { 5034 case '0b': 5035 if (base === 0 || base === 2) { 5036 base = 2; 5037 return head; 5038 } 5039 // no-break 5040 case '0': 5041 case '0o': 5042 if (base === 0 || base === 8) { 5043 base = 8; 5044 return head; 5045 } 5046 // no-break 5047 case '0d': 5048 if (base === 0 || base === 10) { 5049 base = 10; 5050 return head; 5051 } 5052 // no-break 5053 case '0x': 5054 if (base === 0 || base === 16) { 5055 base = 16; 5056 return head; 5057 } 5058 // no-break 5059 } 5060 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\""); 5061 }); 5062 5063 base = (base === 0 ? 10 : base); 5064 5065 base_digits = '0-' + (base <= 10 ? base - 1 : '9a-' + String.fromCharCode(97 + (base - 11))); 5066 5067 if (!(new RegExp('^\\s*[+-]?[' + base_digits + ']+\\s*$')).test(str)) { 5068 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\""); 5069 } 5070 5071 i = parseInt(str, base); 5072 5073 if (isNaN(i)) { 5074 $Kernel.$raise($$$('ArgumentError'), "invalid value for Integer(): \"" + (value) + "\""); 5075 } 5076 5077 return i; 5078 }, -2); 5079 5080 $def(self, '$Float', function $$Float(value) { 5081 5082 5083 var str; 5084 5085 if (value === nil) { 5086 $Kernel.$raise($$$('TypeError'), "can't convert nil into Float"); 5087 } 5088 5089 if (value.$$is_string) { 5090 str = value.toString(); 5091 5092 str = str.replace(/(\d)_(?=\d)/g, '$1'); 5093 5094 //Special case for hex strings only: 5095 if (/^\s*[-+]?0[xX][0-9a-fA-F]+\s*$/.test(str)) { 5096 return $Kernel.$Integer(str); 5097 } 5098 5099 if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) { 5100 $Kernel.$raise($$$('ArgumentError'), "invalid value for Float(): \"" + (value) + "\""); 5101 } 5102 5103 return parseFloat(str); 5104 } 5105 5106 return $Opal['$coerce_to!'](value, $$$('Float'), "to_f"); 5107 5108 }); 5109 5110 $def(self, '$Hash', function $$Hash(arg) { 5111 5112 5113 if (($truthy(arg['$nil?']()) || ($eqeq(arg, [])))) { 5114 return $hash2([], {}) 5115 } if ($eqeqeq($$$('Hash'), arg)) { 5116 return arg 5117 } return $Opal['$coerce_to!'](arg, $$$('Hash'), "to_hash"); 5118 }); 5119 5120 $def(self, '$is_a?', function $Kernel_is_a$ques$11(klass) { 5121 var self = this; 5122 5123 5124 if (!klass.$$is_class && !klass.$$is_module) { 5125 $Kernel.$raise($$$('TypeError'), "class or module required"); 5126 } 5127 5128 return Opal.is_a(self, klass); 5129 5130 }); 5131 5132 $def(self, '$itself', $return_self); 5133 5134 $def(self, '$lambda', function $$lambda() { 5135 var block = $$lambda.$$p || nil; 5136 5137 $$lambda.$$p = null; 5138 return Opal.lambda(block); }); 5139 5140 $def(self, '$load', function $$load(file) { 5141 5142 5143 file = $Opal['$coerce_to!'](file, $$$('String'), "to_str"); 5144 return Opal.load(file); 5145 }); 5146 5147 $def(self, '$loop', function $$loop() { 5148 var $yield = $$loop.$$p || nil, self = this, e = nil; 5149 5150 $$loop.$$p = null; 5151 5152 if (!($yield !== nil)) { 5153 return $send(self, 'enum_for', ["loop"], function $$12(){ 5154 return $$$($$$('Float'), 'INFINITY')}) 5155 } while ($truthy(true)) { 5156 5157 try { 5158 Opal.yieldX($yield, []); 5159 } catch ($err) { 5160 if (Opal.rescue($err, [$$$('StopIteration')])) {(e = $err); 5161 try { 5162 return e.$result() 5163 } finally { Opal.pop_exception(); } 5164 } else { throw $err; } 5165 } } return self; 5166 }); 5167 5168 $def(self, '$nil?', $return_val(false)); 5169 5170 $def(self, '$printf', function $$printf($a) { 5171 var $post_args, args, self = this; 5172 5173 5174 $post_args = $slice(arguments); 5175 args = $post_args; 5176 if ($truthy(args['$any?']())) { 5177 self.$print($send(self, 'format', $to_a(args))); 5178 } return nil; 5179 }, -1); 5180 5181 $def(self, '$proc', function $$proc() { 5182 var block = $$proc.$$p || nil; 5183 5184 $$proc.$$p = null; 5185 if (!$truthy(block)) { 5186 $Kernel.$raise($$$('ArgumentError'), "tried to create Proc object without a block"); 5187 } block.$$is_lambda = false; 5188 return block; 5189 }); 5190 5191 $def(self, '$puts', function $$puts($a) { 5192 var $post_args, strs; 5193 if ($gvars.stdout == null) $gvars.stdout = nil; 5194 5195 5196 $post_args = $slice(arguments); 5197 strs = $post_args; 5198 return $send($gvars.stdout, 'puts', $to_a(strs)); 5199 }, -1); 5200 5201 $def(self, '$p', function $$p($a) { 5202 var $post_args, args; 5203 5204 5205 $post_args = $slice(arguments); 5206 args = $post_args; 5207 $send(args, 'each', [], function $$13(obj){ if ($gvars.stdout == null) $gvars.stdout = nil; 5208 5209 5210 if (obj == null) obj = nil; 5211 return $gvars.stdout.$puts(obj.$inspect());}); 5212 if ($truthy($rb_le(args.$length(), 1))) { 5213 return args['$[]'](0) 5214 } else { 5215 return args 5216 } }, -1); 5217 5218 $def(self, '$print', function $$print($a) { 5219 var $post_args, strs; 5220 if ($gvars.stdout == null) $gvars.stdout = nil; 5221 5222 5223 $post_args = $slice(arguments); 5224 strs = $post_args; 5225 return $send($gvars.stdout, 'print', $to_a(strs)); 5226 }, -1); 5227 5228 $def(self, '$readline', function $$readline($a) { 5229 var $post_args, args; 5230 if ($gvars.stdin == null) $gvars.stdin = nil; 5231 5232 5233 $post_args = $slice(arguments); 5234 args = $post_args; 5235 return $send($gvars.stdin, 'readline', $to_a(args)); 5236 }, -1); 5237 5238 $def(self, '$warn', function $$warn($a, $b) { 5239 var $post_args, $kwargs, strs, uplevel, $c, $d, self = this, location = nil; 5240 if ($gvars.VERBOSE == null) $gvars.VERBOSE = nil; 5241 if ($gvars.stderr == null) $gvars.stderr = nil; 5242 5243 5244 $post_args = $slice(arguments); 5245 $kwargs = $extract_kwargs($post_args); 5246 $kwargs = $ensure_kwargs($kwargs); 5247 strs = $post_args; 5248 5249 uplevel = $kwargs.$$smap["uplevel"];if (uplevel == null) uplevel = nil; 5250 if ($truthy(uplevel)) { 5251 5252 uplevel = $Opal['$coerce_to!'](uplevel, $$$('Integer'), "to_str"); 5253 if ($truthy($rb_lt(uplevel, 0))) { 5254 $Kernel.$raise($$$('ArgumentError'), "negative level (" + (uplevel) + ")"); 5255 } 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()); 5256 if ($truthy(location)) { 5257 location = "" + (location) + ": "; 5258 } strs = $send(strs, 'map', [], function $$14(s){ 5259 5260 if (s == null) s = nil; 5261 return "" + (location) + "warning: " + (s);}); 5262 } if (($truthy($gvars.VERBOSE['$nil?']()) || ($truthy(strs['$empty?']())))) { 5263 return nil 5264 } else { 5265 return $send($gvars.stderr, 'puts', $to_a(strs)) 5266 } }, -1); 5267 5268 $def(self, '$raise', function $$raise(exception, string, backtrace) { 5269 if ($gvars["!"] == null) $gvars["!"] = nil; 5270 if ($gvars["@"] == null) $gvars["@"] = nil; 5271 if (string == null) string = nil; 5272 if (backtrace == null) backtrace = nil; 5273 5274 if (exception == null && $gvars["!"] !== nil) { 5275 throw $gvars["!"]; 5276 } 5277 if (exception == null) { 5278 exception = $$$('RuntimeError').$new(""); 5279 } 5280 else if ($respond_to(exception, '$to_str')) { 5281 exception = $$$('RuntimeError').$new(exception.$to_str()); 5282 } 5283 // using respond_to? and not an undefined check to avoid method_missing matching as true 5284 else if (exception.$$is_class && $respond_to(exception, '$exception')) { 5285 exception = exception.$exception(string); 5286 } 5287 else if (exception.$$is_exception) ; 5288 else { 5289 exception = $$$('TypeError').$new("exception class/object expected"); 5290 } 5291 5292 if (backtrace !== nil) { 5293 exception.$set_backtrace(backtrace); 5294 } 5295 5296 if ($gvars["!"] !== nil) { 5297 Opal.exceptions.push($gvars["!"]); 5298 } 5299 5300 $gvars["!"] = exception; 5301 $gvars["@"] = (exception).$backtrace(); 5302 5303 throw exception; 5304 }, -1); 5305 5306 $def(self, '$rand', function $$rand(max) { 5307 5308 if (max === undefined) { 5309 return $$$($$$('Random'), 'DEFAULT').$rand(); 5310 } 5311 5312 if (max.$$is_number) { 5313 if (max < 0) { 5314 max = Math.abs(max); 5315 } 5316 5317 if (max % 1 !== 0) { 5318 max = max.$to_i(); 5319 } 5320 5321 if (max === 0) { 5322 max = undefined; 5323 } 5324 } 5325 return $$$($$$('Random'), 'DEFAULT').$rand(max); 5326 }, -1); 5327 5328 $def(self, '$respond_to?', function $Kernel_respond_to$ques$15(name, include_all) { 5329 var self = this; 5330 5331 5332 if (include_all == null) include_all = false; 5333 5334 var body = self[$jsid(name)]; 5335 5336 if (typeof(body) === "function" && !body.$$stub) { 5337 return true; 5338 } 5339 5340 if (self['$respond_to_missing?'].$$pristine === true) { 5341 return false; 5342 } else { 5343 return self['$respond_to_missing?'](name, include_all); 5344 } 5345 }, -2); 5346 5347 $def(self, '$respond_to_missing?', function $Kernel_respond_to_missing$ques$16(method_name, include_all) { 5348 return false; 5349 }, -2); 5350 $Opal.$pristine(self, "respond_to?", "respond_to_missing?"); 5351 5352 $def(self, '$require', function $$require(file) { 5353 5354 5355 // As Object.require refers to Kernel.require once Kernel has been loaded the String 5356 // class may not be available yet, the coercion requires both String and Array to be loaded. 5357 if (typeof file !== 'string' && Opal.String && Opal.Array) { 5358 (file = $Opal['$coerce_to!'](file, $$$('String'), "to_str")); 5359 } 5360 return Opal.require(file) 5361 5362 }); 5363 5364 $def(self, '$require_relative', function $$require_relative(file) { 5365 5366 5367 $Opal['$try_convert!'](file, $$$('String'), "to_str"); 5368 file = $$$('File').$expand_path($$$('File').$join(Opal.current_file, "..", file)); 5369 return Opal.require(file); 5370 }); 5371 5372 $def(self, '$require_tree', function $$require_tree(path, $kwargs) { 5373 var autoload; 5374 5375 5376 $kwargs = $ensure_kwargs($kwargs); 5377 5378 autoload = $kwargs.$$smap["autoload"];if (autoload == null) autoload = false; 5379 5380 var result = []; 5381 5382 path = $$$('File').$expand_path(path); 5383 path = Opal.normalize(path); 5384 if (path === '.') path = ''; 5385 for (var name in Opal.modules) { 5386 if ((name)['$start_with?'](path)) { 5387 if(!autoload) { 5388 result.push([name, Opal.require(name)]); 5389 } else { 5390 result.push([name, true]); // do nothing, delegated to a autoloading 5391 } 5392 } 5393 } 5394 5395 return result; 5396 }, -2); 5397 5398 $def(self, '$singleton_class', function $$singleton_class() { 5399 var self = this; 5400 5401 return Opal.get_singleton_class(self); 5402 }); 5403 5404 $def(self, '$sleep', function $$sleep(seconds) { 5405 5406 5407 if (seconds == null) seconds = nil; 5408 5409 if (seconds === nil) { 5410 $Kernel.$raise($$$('TypeError'), "can't convert NilClass into time interval"); 5411 } 5412 if (!seconds.$$is_number) { 5413 $Kernel.$raise($$$('TypeError'), "can't convert " + (seconds.$class()) + " into time interval"); 5414 } 5415 if (seconds < 0) { 5416 $Kernel.$raise($$$('ArgumentError'), "time interval must be positive"); 5417 } 5418 var get_time = Opal.global.performance ? 5419 function() {return performance.now()} : 5420 function() {return new Date()}; 5421 5422 var t = get_time(); 5423 while (get_time() - t <= seconds * 1000); 5424 return Math.round(seconds); 5425 }, -1); 5426 5427 $def(self, '$srand', function $$srand(seed) { 5428 5429 5430 if (seed == null) seed = $$('Random').$new_seed(); 5431 return $$$('Random').$srand(seed); 5432 }, -1); 5433 5434 $def(self, '$String', function $$String(str) { 5435 var $ret_or_1 = nil; 5436 5437 if ($truthy(($ret_or_1 = $Opal['$coerce_to?'](str, $$$('String'), "to_str")))) { 5438 return $ret_or_1 5439 } else { 5440 return $Opal['$coerce_to!'](str, $$$('String'), "to_s") 5441 } 5442 }); 5443 5444 $def(self, '$tap', function $$tap() { 5445 var block = $$tap.$$p || nil, self = this; 5446 5447 $$tap.$$p = null; 5448 Opal.yield1(block, self); 5449 return self; 5450 }); 5451 5452 $def(self, '$to_proc', $return_self); 5453 5454 $def(self, '$to_s', function $$to_s() { 5455 var self = this; 5456 5457 return "#<" + (self.$class()) + ":0x" + (self.$__id__().$to_s(16)) + ">" 5458 }); 5459 5460 $def(self, '$catch', function $Kernel_catch$17(tag) { 5461 var $yield = $Kernel_catch$17.$$p || nil, $ret_or_1 = nil, e = nil; 5462 5463 $Kernel_catch$17.$$p = null; 5464 5465 if (tag == null) tag = nil; 5466 try { 5467 5468 tag = ($truthy(($ret_or_1 = tag)) ? ($ret_or_1) : ($Object.$new())); 5469 return Opal.yield1($yield, tag);; 5470 } catch ($err) { 5471 if (Opal.rescue($err, [$$$('UncaughtThrowError')])) {(e = $err); 5472 try { 5473 5474 if ($eqeq(e.$tag(), tag)) { 5475 return e.$value() 5476 }; 5477 return $Kernel.$raise(); 5478 } finally { Opal.pop_exception(); } 5479 } else { throw $err; } 5480 } }, -1); 5481 5482 $def(self, '$throw', function $Kernel_throw$18(tag, obj) { 5483 5484 5485 if (obj == null) obj = nil; 5486 return $Kernel.$raise($$$('UncaughtThrowError').$new(tag, obj)); 5487 }, -2); 5488 5489 $def(self, '$open', function $$open($a) { 5490 var block = $$open.$$p || nil, $post_args, args; 5491 5492 $$open.$$p = null; 5493 $post_args = $slice(arguments); 5494 args = $post_args; 5495 return $send($$$('File'), 'open', $to_a(args), block.$to_proc()); 5496 }, -1); 5497 5498 $def(self, '$yield_self', function $$yield_self() { 5499 var $yield = $$yield_self.$$p || nil, self = this; 5500 5501 $$yield_self.$$p = null; 5502 5503 if (!($yield !== nil)) { 5504 return $send(self, 'enum_for', ["yield_self"], $return_val(1)) 5505 } return Opal.yield1($yield, self); }); 5506 $alias(self, "fail", "raise"); 5507 $alias(self, "kind_of?", "is_a?"); 5508 $alias(self, "object_id", "__id__"); 5509 $alias(self, "public_send", "__send__"); 5510 $alias(self, "send", "__send__"); 5511 $alias(self, "then", "yield_self"); 5512 return $alias(self, "to_enum", "enum_for"); 5513 })('::', $nesting); 5514 return (function($base, $super) { 5515 var self = $klass($base, $super, 'Object'); 5516 5517 5518 5519 delete $Object.$$prototype.$require; 5520 return self.$include($Kernel); 5521 })('::', null); 5522}; 5523 5524Opal.modules["corelib/main"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5525 var $return_val = Opal.return_val, $def = Opal.def, $Object = Opal.Object, $slice = Opal.slice, $Kernel = Opal.Kernel, self = Opal.top; Opal.nil; 5526 5527 Opal.add_stubs('include,raise'); 5528 return (function(self, $parent_nesting) { 5529 5530 5531 5532 $def(self, '$to_s', $return_val("main")); 5533 5534 $def(self, '$include', function $$include(mod) { 5535 5536 return $Object.$include(mod) 5537 }); 5538 5539 $def(self, '$autoload', function $$autoload($a) { 5540 var $post_args, args; 5541 5542 5543 $post_args = $slice(arguments); 5544 args = $post_args; 5545 return Opal.Object.$autoload.apply(Opal.Object, args); }, -1); 5546 return $def(self, '$using', function $$using(mod) { 5547 5548 return $Kernel.$raise("main.using is permitted only at toplevel") 5549 }); 5550 })(Opal.get_singleton_class(self)) 5551}; 5552 5553Opal.modules["corelib/error/errno"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5554 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, nil = Opal.nil, $$$ = Opal.$$$; 5555 5556 Opal.add_stubs('+,errno,class,attr_reader'); 5557 5558 (function($base, $parent_nesting) { 5559 var self = $module($base, 'Errno'); 5560 5561 var errors = nil, klass = nil; 5562 5563 5564 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]]; 5565 klass = nil; 5566 5567 var i; 5568 for (i = 0; i < errors.length; i++) { 5569 (function() { // Create a closure 5570 var class_name = errors[i][0]; 5571 var default_message = errors[i][1]; 5572 var errno = errors[i][2]; 5573 5574 klass = Opal.klass(self, Opal.SystemCallError, class_name); 5575 klass.errno = errno; 5576 5577 (function(self, $parent_nesting) { 5578 5579 return $def(self, '$new', function $new$1(name) { 5580 var self = this, message = nil; 5581 5582 $new$1.$$p = null; 5583 5584 if (name == null) name = nil; 5585 message = default_message; 5586 if ($truthy(name)) { 5587 message = $rb_plus(message, " - " + (name)); 5588 } return $send2(self, $find_super(self, 'new', $new$1, false, true), 'new', [message], null); 5589 }, -1) 5590 })(Opal.get_singleton_class(klass)); 5591 })(); 5592 } 5593 })('::'); 5594 return (function($base, $super, $parent_nesting) { 5595 var self = $klass($base, $super, 'SystemCallError'); 5596 5597 5598 5599 $def(self, '$errno', function $$errno() { 5600 var self = this; 5601 5602 return self.$class().$errno() 5603 }); 5604 return (function(self, $parent_nesting) { 5605 5606 return self.$attr_reader("errno") 5607 })(Opal.get_singleton_class(self)); 5608 })('::', $$$('StandardError')); 5609}; 5610 5611Opal.modules["corelib/error"] = function(Opal) {/* Generated by Opal 1.7.3 */ 5612 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.$$$; 5613 5614 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'); 5615 5616 (function($base, $super, $parent_nesting) { 5617 var self = $klass($base, $super, 'Exception'); 5618 5619 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 5620 5621 $proto.message = nil; 5622 5623 Opal.prop(self.$$prototype, '$$is_exception', true); 5624 var stack_trace_limit; 5625 Error.stackTraceLimit = 100; 5626 $defs(self, '$new', function $Exception_new$1($a) { 5627 var $post_args, args, self = this; 5628 if ($gvars["!"] == null) $gvars["!"] = nil; 5629 5630 5631 $post_args = $slice(arguments); 5632 args = $post_args; 5633 5634 var message = (args.length > 0) ? args[0] : nil; 5635 var error = new self.$$constructor(message); 5636 error.name = self.$$name; 5637 error.message = message; 5638 error.cause = $gvars["!"]; 5639 Opal.send(error, error.$initialize, args); 5640 5641 // Error.captureStackTrace() will use .name and .toString to build the 5642 // first line of the stack trace so it must be called after the error 5643 // has been initialized. 5644 // https://nodejs.org/dist/latest-v6.x/docs/api/errors.html 5645 if (Opal.config.enable_stack_trace && Error.captureStackTrace) { 5646 // Passing Kernel.raise will cut the stack trace from that point above 5647 Error.captureStackTrace(error, stack_trace_limit); 5648 } 5649 5650 return error; 5651 }, -1); 5652 stack_trace_limit = self.$new; 5653 $defs(self, '$exception', function $$exception($a) { 5654 var $post_args, args, self = this; 5655 5656 5657 $post_args = $slice(arguments); 5658 args = $post_args; 5659 return $send(self, 'new', $to_a(args)); 5660 }, -1); 5661 5662 $def(self, '$initialize', function $$initialize($a) { 5663 var $post_args, args, self = this; 5664 5665 5666 $post_args = $slice(arguments); 5667 args = $post_args; 5668 return self.message = (args.length > 0) ? args[0] : nil; }, -1); 5669 5670 // Convert backtrace from any format to Ruby format 5671 function correct_backtrace(backtrace) { 5672 var new_bt = [], m; 5673 5674 for (var i = 0; i < backtrace.length; i++) { 5675 var loc = backtrace[i]; 5676 if (!loc || !loc.$$is_string) ; 5677 /* Chromium format */ 5678 else if ((m = loc.match(/^ at (.*?) \((.*?)\)$/))) { 5679 new_bt.push(m[2] + ":in `" + m[1] + "'"); 5680 } 5681 else if ((m = loc.match(/^ at (.*?)$/))) { 5682 new_bt.push(m[1] + ":in `undefined'"); 5683 } 5684 /* Node format */ 5685 else if ((m = loc.match(/^ from (.*?)$/))) { 5686 new_bt.push(m[1]); 5687 } 5688 /* Mozilla/Apple format */ 5689 else if ((m = loc.match(/^(.*?)@(.*?)$/))) { 5690 new_bt.push(m[2] + ':in `' + m[1] + "'"); 5691 } 5692 } 5693 5694 return new_bt; 5695 } 5696 5697 $def(self, '$backtrace', function $$backtrace() { 5698 var self = this; 5699 5700 5701 if (self.backtrace) { 5702 // nil is a valid backtrace 5703 return self.backtrace; 5704 } 5705 5706 var backtrace = self.stack; 5707 5708 if (typeof(backtrace) !== 'undefined' && backtrace.$$is_string) { 5709 return self.backtrace = correct_backtrace(backtrace.split("\n")); 5710 } 5711 else if (backtrace) { 5712 return self.backtrace = correct_backtrace(backtrace); 5713 } 5714 5715 return []; 5716 5717 }); 5718 5719 $def(self, '$backtrace_locations', function $$backtrace_locations() { 5720 var $a, self = this; 5721 5722 5723 if (self.backtrace_locations) return self.backtrace_locations; 5724 self.backtrace_locations = ($a = self.$backtrace(), ($a === nil || $a == null) ? nil : $send($a, 'map', [], function $$2(loc){ 5725 5726 if (loc == null) loc = nil; 5727 return $$$($$$($$$('Thread'), 'Backtrace'), 'Location').$new(loc);})); 5728 return self.backtrace_locations; 5729 5730 }); 5731 5732 $def(self, '$cause', function $$cause() { 5733 var self = this; 5734 5735 return self.cause || nil; 5736 }); 5737 5738 $def(self, '$exception', function $$exception(str) { 5739 var self = this; 5740 5741 5742 if (str == null) str = nil; 5743 5744 if (str === nil || self === str) { 5745 return self; 5746 } 5747 5748 var cloned = self.$clone(); 5749 cloned.message = str; 5750 if (self.backtrace) cloned.backtrace = self.backtrace.$dup(); 5751 cloned.stack = self.stack; 5752 cloned.cause = self.cause; 5753 return cloned; 5754 }, -1); 5755 5756 $def(self, '$message', function $$message() { 5757 var self = this; 5758 5759 return self.$to_s() 5760 }); 5761 5762 $def(self, '$full_message', function $$full_message(kwargs) { 5763 var $b, self = this, $ret_or_1 = nil, highlight = nil, order = nil, bold_underline = nil, bold = nil, reset = nil, bt = nil, first = nil, msg = nil; 5764 if ($gvars.stderr == null) $gvars.stderr = nil; 5765 5766 5767 if (kwargs == null) kwargs = nil; 5768 if (!$truthy((($$('Hash', 'skip_raise')) ? 'constant' : nil))) { 5769 return "" + (self.message) + "\n" + (self.stack) 5770 } kwargs = $hash2(["highlight", "order"], {"highlight": $gvars.stderr['$tty?'](), "order": "top"}).$merge(($truthy(($ret_or_1 = kwargs)) ? ($ret_or_1) : ($hash2([], {})))); 5771 $b = [kwargs['$[]']("highlight"), kwargs['$[]']("order")], (highlight = $b[0]), (order = $b[1]); 5772 if (!$truthy([true, false]['$include?'](highlight))) { 5773 $Kernel.$raise($$$('ArgumentError'), "expected true or false as highlight: " + (highlight)); 5774 } if (!$truthy(["top", "bottom"]['$include?'](order))) { 5775 $Kernel.$raise($$$('ArgumentError'), "expected :top or :bottom as order: " + (order)); 5776 } if ($truthy(highlight)) { 5777 5778 bold_underline = "\u001b[1;4m"; 5779 bold = "\u001b[1m"; 5780 reset = "\u001b[m"; 5781 } else { 5782 bold_underline = (bold = (reset = "")); 5783 } bt = self.$backtrace().$dup(); 5784 if (($not(bt) || ($truthy(bt['$empty?']())))) { 5785 bt = self.$caller(); 5786 } first = bt.$shift(); 5787 msg = "" + (first) + ": "; 5788 msg = $rb_plus(msg, "" + (bold) + (self.$to_s()) + " (" + (bold_underline) + (self.$class()) + (reset) + (bold) + ")" + (reset) + "\n"); 5789 msg = $rb_plus(msg, $send(bt, 'map', [], function $$3(loc){ 5790 5791 if (loc == null) loc = nil; 5792 return "\tfrom " + (loc) + "\n";}).$join()); 5793 if ($truthy(self.$cause())) { 5794 msg = $rb_plus(msg, self.$cause().$full_message($hash2(["highlight"], {"highlight": highlight}))); 5795 } if ($eqeq(order, "bottom")) { 5796 5797 msg = msg.$split("\n").$reverse().$join("\n"); 5798 msg = $rb_plus("" + (bold) + "Traceback" + (reset) + " (most recent call last):\n", msg); 5799 } return msg; 5800 }, -1); 5801 5802 $def(self, '$inspect', function $$inspect() { 5803 var self = this, as_str = nil; 5804 5805 5806 as_str = self.$to_s(); 5807 if ($truthy(as_str['$empty?']())) { 5808 return self.$class().$to_s() 5809 } else { 5810 return "#<" + (self.$class().$to_s()) + ": " + (self.$to_s()) + ">" 5811 } }); 5812 5813 $def(self, '$set_backtrace', function $$set_backtrace(backtrace) { 5814 var self = this; 5815 5816 5817 var valid = true, i, ii; 5818 5819 if (backtrace === nil) { 5820 self.backtrace = nil; 5821 self.stack = ''; 5822 } else if (backtrace.$$is_string) { 5823 self.backtrace = [backtrace]; 5824 self.stack = ' from ' + backtrace; 5825 } else { 5826 if (backtrace.$$is_array) { 5827 for (i = 0, ii = backtrace.length; i < ii; i++) { 5828 if (!backtrace[i].$$is_string) { 5829 valid = false; 5830 break; 5831 } 5832 } 5833 } else { 5834 valid = false; 5835 } 5836 5837 if (valid === false) { 5838 $Kernel.$raise($$$('TypeError'), "backtrace must be Array of String"); 5839 } 5840 5841 self.backtrace = backtrace; 5842 self.stack = $send((backtrace), 'map', [], function $$4(i){ 5843 5844 if (i == null) i = nil; 5845 return $rb_plus(" from ", i);}).join("\n"); 5846 } 5847 5848 return backtrace; 5849 5850 }); 5851 return $def(self, '$to_s', function $$to_s() { 5852 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 5853 5854 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = self.message)) ? (self.message.$to_s()) : ($ret_or_2))))) { 5855 return $ret_or_1 5856 } else { 5857 return self.$class().$to_s() 5858 } 5859 }); 5860 })('::', Error, $nesting); 5861 $klass('::', $$$('Exception'), 'ScriptError'); 5862 $klass('::', $$$('ScriptError'), 'SyntaxError'); 5863 $klass('::', $$$('ScriptError'), 'LoadError'); 5864 $klass('::', $$$('ScriptError'), 'NotImplementedError'); 5865 $klass('::', $$$('Exception'), 'SystemExit'); 5866 $klass('::', $$$('Exception'), 'NoMemoryError'); 5867 $klass('::', $$$('Exception'), 'SignalException'); 5868 $klass('::', $$$('SignalException'), 'Interrupt'); 5869 $klass('::', $$$('Exception'), 'SecurityError'); 5870 $klass('::', $$$('Exception'), 'SystemStackError'); 5871 $klass('::', $$$('Exception'), 'StandardError'); 5872 $klass('::', $$$('StandardError'), 'EncodingError'); 5873 $klass('::', $$$('StandardError'), 'ZeroDivisionError'); 5874 $klass('::', $$$('StandardError'), 'NameError'); 5875 $klass('::', $$$('NameError'), 'NoMethodError'); 5876 $klass('::', $$$('StandardError'), 'RuntimeError'); 5877 $klass('::', $$$('RuntimeError'), 'FrozenError'); 5878 $klass('::', $$$('StandardError'), 'LocalJumpError'); 5879 $klass('::', $$$('StandardError'), 'TypeError'); 5880 $klass('::', $$$('StandardError'), 'ArgumentError'); 5881 $klass('::', $$$('ArgumentError'), 'UncaughtThrowError'); 5882 $klass('::', $$$('StandardError'), 'IndexError'); 5883 $klass('::', $$$('IndexError'), 'StopIteration'); 5884 $klass('::', $$$('StopIteration'), 'ClosedQueueError'); 5885 $klass('::', $$$('IndexError'), 'KeyError'); 5886 $klass('::', $$$('StandardError'), 'RangeError'); 5887 $klass('::', $$$('RangeError'), 'FloatDomainError'); 5888 $klass('::', $$$('StandardError'), 'IOError'); 5889 $klass('::', $$$('IOError'), 'EOFError'); 5890 $klass('::', $$$('StandardError'), 'SystemCallError'); 5891 $klass('::', $$$('StandardError'), 'RegexpError'); 5892 $klass('::', $$$('StandardError'), 'ThreadError'); 5893 $klass('::', $$$('StandardError'), 'FiberError'); 5894 $Object.$autoload("Errno", "corelib/error/errno"); 5895 (function($base, $super) { 5896 var self = $klass($base, $super, 'FrozenError'); 5897 5898 5899 5900 self.$attr_reader("receiver"); 5901 return $def(self, '$initialize', function $$initialize(message, $kwargs) { 5902 var receiver, self = this; 5903 5904 $$initialize.$$p = null; 5905 5906 $kwargs = $ensure_kwargs($kwargs); 5907 5908 receiver = $kwargs.$$smap["receiver"];if (receiver == null) receiver = nil; 5909 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 5910 return (self.receiver = receiver); 5911 }, -2); 5912 })('::', $$$('RuntimeError')); 5913 (function($base, $super) { 5914 var self = $klass($base, $super, 'UncaughtThrowError'); 5915 5916 var $proto = self.$$prototype; 5917 5918 $proto.tag = nil; 5919 5920 self.$attr_reader("tag", "value"); 5921 return $def(self, '$initialize', function $$initialize(tag, value) { 5922 var self = this; 5923 5924 $$initialize.$$p = null; 5925 5926 if (value == null) value = nil; 5927 self.tag = tag; 5928 self.value = value; 5929 return $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', ["uncaught throw " + (self.tag.$inspect())], null); 5930 }, -2); 5931 })('::', $$$('ArgumentError')); 5932 (function($base, $super) { 5933 var self = $klass($base, $super, 'NameError'); 5934 5935 5936 5937 self.$attr_reader("name"); 5938 return $def(self, '$initialize', function $$initialize(message, name) { 5939 var self = this; 5940 5941 $$initialize.$$p = null; 5942 5943 if (name == null) name = nil; 5944 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 5945 return (self.name = name); 5946 }, -2); 5947 })('::', null); 5948 (function($base, $super) { 5949 var self = $klass($base, $super, 'NoMethodError'); 5950 5951 5952 5953 self.$attr_reader("args"); 5954 return $def(self, '$initialize', function $$initialize(message, name, args) { 5955 var self = this; 5956 5957 $$initialize.$$p = null; 5958 5959 if (name == null) name = nil; 5960 if (args == null) args = []; 5961 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message, name], null); 5962 return (self.args = args); 5963 }, -2); 5964 })('::', null); 5965 (function($base, $super) { 5966 var self = $klass($base, $super, 'StopIteration'); 5967 5968 5969 return self.$attr_reader("result") 5970 })('::', null); 5971 (function($base, $super) { 5972 var self = $klass($base, $super, 'KeyError'); 5973 5974 var $proto = self.$$prototype; 5975 5976 $proto.receiver = $proto.key = nil; 5977 5978 5979 $def(self, '$initialize', function $$initialize(message, $kwargs) { 5980 var receiver, key, self = this; 5981 5982 $$initialize.$$p = null; 5983 5984 $kwargs = $ensure_kwargs($kwargs); 5985 5986 receiver = $kwargs.$$smap["receiver"];if (receiver == null) receiver = nil; 5987 5988 key = $kwargs.$$smap["key"];if (key == null) key = nil; 5989 $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [message], null); 5990 self.receiver = receiver; 5991 return (self.key = key); 5992 }, -2); 5993 5994 $def(self, '$receiver', function $$receiver() { 5995 var self = this, $ret_or_1 = nil; 5996 5997 if ($truthy(($ret_or_1 = self.receiver))) { 5998 return $ret_or_1 5999 } else { 6000 return $Kernel.$raise($$$('ArgumentError'), "no receiver is available") 6001 } 6002 }); 6003 return $def(self, '$key', function $$key() { 6004 var self = this, $ret_or_1 = nil; 6005 6006 if ($truthy(($ret_or_1 = self.key))) { 6007 return $ret_or_1 6008 } else { 6009 return $Kernel.$raise($$$('ArgumentError'), "no key is available") 6010 } 6011 }); 6012 })('::', null); 6013 return (function($base, $parent_nesting) { 6014 var self = $module($base, 'JS'); 6015 6016 var $nesting = [self].concat($parent_nesting); 6017 6018 return ($klass($nesting[0], null, 'Error'), nil) 6019 })('::', $nesting); 6020}; 6021 6022Opal.modules["corelib/constants"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6023 var $const_set = Opal.const_set; Opal.nil; var $$$ = Opal.$$$; 6024 6025 6026 $const_set('::', 'RUBY_PLATFORM', "opal"); 6027 $const_set('::', 'RUBY_ENGINE', "opal"); 6028 $const_set('::', 'RUBY_VERSION', "3.2.0"); 6029 $const_set('::', 'RUBY_ENGINE_VERSION', "1.7.3"); 6030 $const_set('::', 'RUBY_RELEASE_DATE', "2023-03-23"); 6031 $const_set('::', 'RUBY_PATCHLEVEL', 0); 6032 $const_set('::', 'RUBY_REVISION', "0"); 6033 $const_set('::', 'RUBY_COPYRIGHT', "opal - Copyright (C) 2011-2023 Adam Beynon and the Opal contributors"); 6034 return $const_set('::', 'RUBY_DESCRIPTION', "opal " + ($$$('RUBY_ENGINE_VERSION')) + " (" + ($$$('RUBY_RELEASE_DATE')) + " revision " + ($$$('RUBY_REVISION')) + ")"); 6035}; 6036 6037Opal.modules["opal/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6038 var $Object = Opal.Object; Opal.nil; 6039 6040 Opal.add_stubs('require'); 6041 6042 $Object.$require("corelib/runtime"); 6043 $Object.$require("corelib/helpers"); 6044 $Object.$require("corelib/module"); 6045 $Object.$require("corelib/class"); 6046 $Object.$require("corelib/basic_object"); 6047 $Object.$require("corelib/kernel"); 6048 $Object.$require("corelib/main"); 6049 $Object.$require("corelib/error"); 6050 return $Object.$require("corelib/constants"); 6051}; 6052 6053Opal.modules["corelib/nil"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6054 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, nil = Opal.nil, $$$ = Opal.$$$; 6055 6056 Opal.add_stubs('raise,name,new,>,length,Rational,to_i'); 6057 return (function($base, $super, $parent_nesting) { 6058 var self = $klass($base, $super, 'NilClass'); 6059 6060 6061 self.$$prototype.$$meta = self; 6062 (function(self, $parent_nesting) { 6063 6064 6065 6066 $def(self, '$allocate', function $$allocate() { 6067 var self = this; 6068 6069 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 6070 }); 6071 6072 6073 Opal.udef(self, '$' + "new"); return nil; })(Opal.get_singleton_class(self)); 6074 6075 $def(self, '$!', $return_val(true)); 6076 6077 $def(self, '$&', $return_val(false)); 6078 6079 $def(self, '$|', function $NilClass_$$1(other) { 6080 6081 return other !== false && other !== nil; 6082 }); 6083 6084 $def(self, '$^', function $NilClass_$$2(other) { 6085 6086 return other !== false && other !== nil; 6087 }); 6088 6089 $def(self, '$==', function $NilClass_$eq_eq$3(other) { 6090 6091 return other === nil; 6092 }); 6093 6094 $def(self, '$dup', $return_val(nil)); 6095 6096 $def(self, '$clone', function $$clone($kwargs) { 6097 6098 6099 $kwargs = $ensure_kwargs($kwargs); 6100 6101 $kwargs.$$smap["freeze"]; return nil; 6102 }, -1); 6103 6104 $def(self, '$inspect', $return_val("nil")); 6105 6106 $def(self, '$nil?', $return_val(true)); 6107 6108 $def(self, '$singleton_class', function $$singleton_class() { 6109 6110 return $NilClass 6111 }); 6112 6113 $def(self, '$to_a', function $$to_a() { 6114 6115 return [] 6116 }); 6117 6118 $def(self, '$to_h', function $$to_h() { 6119 6120 return Opal.hash(); 6121 }); 6122 6123 $def(self, '$to_i', $return_val(0)); 6124 6125 $def(self, '$to_s', $return_val("")); 6126 6127 $def(self, '$to_c', function $$to_c() { 6128 6129 return $$$('Complex').$new(0, 0) 6130 }); 6131 6132 $def(self, '$rationalize', function $$rationalize($a) { 6133 var $post_args, args; 6134 6135 6136 $post_args = $slice(arguments); 6137 args = $post_args; 6138 if ($truthy($rb_gt(args.$length(), 1))) { 6139 $Kernel.$raise($$$('ArgumentError')); 6140 } return $Kernel.$Rational(0, 1); 6141 }, -1); 6142 6143 $def(self, '$to_r', function $$to_r() { 6144 6145 return $Kernel.$Rational(0, 1) 6146 }); 6147 6148 $def(self, '$instance_variables', function $$instance_variables() { 6149 6150 return [] 6151 }); 6152 return $alias(self, "to_f", "to_i"); 6153 })('::', null) 6154}; 6155 6156Opal.modules["corelib/boolean"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6157 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, nil = Opal.nil, $$$ = Opal.$$$; 6158 6159 Opal.add_stubs('raise,name,==,to_s,__id__'); 6160 6161 (function($base, $super, $parent_nesting) { 6162 var self = $klass($base, $super, 'Boolean'); 6163 6164 6165 Opal.prop(self.$$prototype, '$$is_boolean', true); 6166 6167 var properties = ['$$class', '$$meta']; 6168 6169 for (var i = 0; i < properties.length; i++) { 6170 Object.defineProperty(self.$$prototype, properties[i], { 6171 configurable: true, 6172 enumerable: false, 6173 get: function() { 6174 return this == true ? Opal.TrueClass : 6175 this == false ? Opal.FalseClass : 6176 Opal.Boolean; 6177 } 6178 }); 6179 } 6180 6181 Object.defineProperty(self.$$prototype, "$$id", { 6182 configurable: true, 6183 enumerable: false, 6184 get: function() { 6185 return this == true ? 2 : 6186 this == false ? 0 : 6187 nil; 6188 } 6189 }); 6190 (function(self, $parent_nesting) { 6191 6192 6193 6194 $def(self, '$allocate', function $$allocate() { 6195 var self = this; 6196 6197 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 6198 }); 6199 6200 6201 Opal.udef(self, '$' + "new"); return nil; })(Opal.get_singleton_class(self)); 6202 6203 $def(self, '$__id__', function $$__id__() { 6204 var self = this; 6205 6206 return self.valueOf() ? 2 : 0; 6207 }); 6208 6209 $def(self, '$!', function $Boolean_$excl$1() { 6210 var self = this; 6211 6212 return self != true; 6213 }); 6214 6215 $def(self, '$&', function $Boolean_$$2(other) { 6216 var self = this; 6217 6218 return (self == true) ? (other !== false && other !== nil) : false; 6219 }); 6220 6221 $def(self, '$|', function $Boolean_$$3(other) { 6222 var self = this; 6223 6224 return (self == true) ? true : (other !== false && other !== nil); 6225 }); 6226 6227 $def(self, '$^', function $Boolean_$$4(other) { 6228 var self = this; 6229 6230 return (self == true) ? (other === false || other === nil) : (other !== false && other !== nil); 6231 }); 6232 6233 $def(self, '$==', function $Boolean_$eq_eq$5(other) { 6234 var self = this; 6235 6236 return (self == true) === other.valueOf(); 6237 }); 6238 6239 $def(self, '$singleton_class', function $$singleton_class() { 6240 var self = this; 6241 6242 return self.$$meta; 6243 }); 6244 6245 $def(self, '$to_s', function $$to_s() { 6246 var self = this; 6247 6248 return (self == true) ? 'true' : 'false'; 6249 }); 6250 6251 $def(self, '$dup', $return_self); 6252 6253 $def(self, '$clone', function $$clone($kwargs) { 6254 var self = this; 6255 6256 6257 $kwargs = $ensure_kwargs($kwargs); 6258 6259 $kwargs.$$smap["freeze"]; return self; 6260 }, -1); 6261 6262 $def(self, '$method_missing', function $$method_missing(method, $a) { 6263 var block = $$method_missing.$$p || nil, $post_args, args, self = this; 6264 6265 $$method_missing.$$p = null; 6266 $post_args = $slice(arguments, 1); 6267 args = $post_args; 6268 var body = self.$$class.$$prototype[Opal.jsid(method)]; 6269 if (!$truthy(typeof body !== 'undefined' && !body.$$stub)) { 6270 $send2(self, $find_super(self, 'method_missing', $$method_missing, false, true), 'method_missing', [method].concat($to_a(args)), block); 6271 } return Opal.send(self, body, args, block); 6272 }, -2); 6273 6274 $def(self, '$respond_to_missing?', function $Boolean_respond_to_missing$ques$6(method, _include_all) { 6275 var self = this; 6276 var body = self.$$class.$$prototype[Opal.jsid(method)]; 6277 return typeof body !== 'undefined' && !body.$$stub; }, -2); 6278 $alias(self, "eql?", "=="); 6279 $alias(self, "equal?", "=="); 6280 $alias(self, "inspect", "to_s"); 6281 return $alias(self, "object_id", "__id__"); 6282 })('::', Boolean); 6283 $klass('::', $$$('Boolean'), 'TrueClass'); 6284 return ($klass('::', $$$('Boolean'), 'FalseClass'), nil); 6285}; 6286 6287Opal.modules["corelib/comparable"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6288 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.$$$; 6289 6290 Opal.add_stubs('>,<,===,raise,class,<=>,equal?'); 6291 return (function($base) { 6292 var self = $module($base, 'Comparable'); 6293 6294 var $ret_or_1 = nil; 6295 6296 6297 6298 function normalize(what) { 6299 if (Opal.is_a(what, Opal.Integer)) { return what; } 6300 6301 if ($rb_gt(what, 0)) { return 1; } 6302 if ($rb_lt(what, 0)) { return -1; } 6303 return 0; 6304 } 6305 6306 function fail_comparison(lhs, rhs) { 6307 var class_name; 6308 (($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)); 6309 $Kernel.$raise($$$('ArgumentError'), "comparison of " + ((lhs).$class()) + " with " + (class_name) + " failed"); 6310 } 6311 6312 function cmp_or_fail(lhs, rhs) { 6313 var cmp = (lhs)['$<=>'](rhs); 6314 if (!$truthy(cmp)) fail_comparison(lhs, rhs); 6315 return normalize(cmp); 6316 } 6317 6318 $def(self, '$==', function $Comparable_$eq_eq$1(other) { 6319 var self = this, cmp = nil; 6320 6321 6322 if ($truthy(self['$equal?'](other))) { 6323 return true 6324 } 6325 if (self["$<=>"] == Opal.Kernel["$<=>"]) { 6326 return false; 6327 } 6328 6329 // check for infinite recursion 6330 if (self.$$comparable) { 6331 self.$$comparable = false; 6332 return false; 6333 } 6334 if (!$truthy((cmp = self['$<=>'](other)))) { 6335 return false 6336 } return normalize(cmp) == 0; }); 6337 6338 $def(self, '$>', function $Comparable_$gt$2(other) { 6339 var self = this; 6340 6341 return cmp_or_fail(self, other) > 0; 6342 }); 6343 6344 $def(self, '$>=', function $Comparable_$gt_eq$3(other) { 6345 var self = this; 6346 6347 return cmp_or_fail(self, other) >= 0; 6348 }); 6349 6350 $def(self, '$<', function $Comparable_$lt$4(other) { 6351 var self = this; 6352 6353 return cmp_or_fail(self, other) < 0; 6354 }); 6355 6356 $def(self, '$<=', function $Comparable_$lt_eq$5(other) { 6357 var self = this; 6358 6359 return cmp_or_fail(self, other) <= 0; 6360 }); 6361 6362 $def(self, '$between?', function $Comparable_between$ques$6(min, max) { 6363 var self = this; 6364 6365 6366 if ($rb_lt(self, min)) { 6367 return false 6368 } if ($rb_gt(self, max)) { 6369 return false 6370 } return true; 6371 }); 6372 return $def(self, '$clamp', function $$clamp(min, max) { 6373 var self = this; 6374 6375 6376 if (max == null) max = nil; 6377 6378 var c, excl; 6379 6380 if (max === nil) { 6381 // We are dealing with a new Ruby 2.7 behaviour that we are able to 6382 // provide a single Range argument instead of 2 Comparables. 6383 6384 if (!Opal.is_a(min, Opal.Range)) { 6385 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (min.$class()) + " (expected Range)"); 6386 } 6387 6388 excl = min.excl; 6389 max = min.end; 6390 min = min.begin; 6391 6392 if (max !== nil && excl) { 6393 $Kernel.$raise($$$('ArgumentError'), "cannot clamp with an exclusive range"); 6394 } 6395 } 6396 6397 if (min !== nil && max !== nil && cmp_or_fail(min, max) > 0) { 6398 $Kernel.$raise($$$('ArgumentError'), "min argument must be smaller than max argument"); 6399 } 6400 6401 if (min !== nil) { 6402 c = cmp_or_fail(self, min); 6403 6404 if (c == 0) return self; 6405 if (c < 0) return min; 6406 } 6407 6408 if (max !== nil) { 6409 c = cmp_or_fail(self, max); 6410 6411 if (c > 0) return max; 6412 } 6413 6414 return self; 6415 }, -2); 6416 })('::') 6417}; 6418 6419Opal.modules["corelib/regexp"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6420 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.$$$; 6421 6422 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'); 6423 6424 $klass('::', $$$('StandardError'), 'RegexpError'); 6425 (function($base, $super, $parent_nesting) { 6426 var self = $klass($base, $super, 'Regexp'); 6427 6428 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 6429 6430 6431 $const_set(self, 'IGNORECASE', 1); 6432 $const_set(self, 'EXTENDED', 2); 6433 $const_set(self, 'MULTILINE', 4); 6434 Opal.prop(self.$$prototype, '$$is_regexp', true); 6435 (function(self, $parent_nesting) { 6436 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 6437 6438 6439 6440 $def(self, '$allocate', function $$allocate() { 6441 var $yield = $$allocate.$$p || nil, self = this, allocated = nil; 6442 6443 $$allocate.$$p = null; 6444 6445 allocated = $send2(self, $find_super(self, 'allocate', $$allocate, false, true), 'allocate', [], $yield); 6446 allocated.uninitialized = true; 6447 return allocated; 6448 }); 6449 6450 $def(self, '$escape', function $$escape(string) { 6451 6452 return Opal.escape_regexp(string); 6453 }); 6454 6455 $def(self, '$last_match', function $$last_match(n) { 6456 if ($gvars["~"] == null) $gvars["~"] = nil; 6457 6458 6459 if (n == null) n = nil; 6460 if ($truthy(n['$nil?']())) { 6461 return $gvars["~"] 6462 } else if ($truthy($gvars["~"])) { 6463 return $gvars["~"]['$[]'](n) 6464 } else { 6465 return nil 6466 } }, -1); 6467 6468 $def(self, '$union', function $$union($a) { 6469 var $post_args, parts, self = this; 6470 6471 6472 $post_args = $slice(arguments); 6473 parts = $post_args; 6474 6475 var is_first_part_array, quoted_validated, part, options, each_part_options; 6476 if (parts.length == 0) { 6477 return /(?!)/; 6478 } 6479 // return fast if there's only one element 6480 if (parts.length == 1 && parts[0].$$is_regexp) { 6481 return parts[0]; 6482 } 6483 // cover the 2 arrays passed as arguments case 6484 is_first_part_array = parts[0].$$is_array; 6485 if (parts.length > 1 && is_first_part_array) { 6486 $Kernel.$raise($$$('TypeError'), "no implicit conversion of Array into String"); 6487 } 6488 // deal with splat issues (related to https://github.com/opal/opal/issues/858) 6489 if (is_first_part_array) { 6490 parts = parts[0]; 6491 } 6492 options = undefined; 6493 quoted_validated = []; 6494 for (var i=0; i < parts.length; i++) { 6495 part = parts[i]; 6496 if (part.$$is_string) { 6497 quoted_validated.push(self.$escape(part)); 6498 } 6499 else if (part.$$is_regexp) { 6500 each_part_options = (part).$options(); 6501 if (options != undefined && options != each_part_options) { 6502 $Kernel.$raise($$$('TypeError'), "All expressions must use the same options"); 6503 } 6504 options = each_part_options; 6505 quoted_validated.push('('+part.source+')'); 6506 } 6507 else { 6508 quoted_validated.push(self.$escape((part).$to_str())); 6509 } 6510 } 6511 return self.$new((quoted_validated).$join("|"), options); 6512 }, -1); 6513 6514 $def(self, '$new', function $new$1(regexp, options) { 6515 6516 if (regexp.$$is_regexp) { 6517 return new RegExp(regexp); 6518 } 6519 6520 regexp = $Opal['$coerce_to!'](regexp, $$$('String'), "to_str"); 6521 6522 if (regexp.charAt(regexp.length - 1) === '\\' && regexp.charAt(regexp.length - 2) !== '\\') { 6523 $Kernel.$raise($$$('RegexpError'), "too short escape sequence: /" + (regexp) + "/"); 6524 } 6525 6526 regexp = regexp.replace('\\A', '^').replace('\\z', '$'); 6527 6528 if (options === undefined || options['$!']()) { 6529 return new RegExp(regexp); 6530 } 6531 6532 if (options.$$is_number) { 6533 var temp = ''; 6534 if ($$('IGNORECASE') & options) { temp += 'i'; } 6535 if ($$('MULTILINE') & options) { temp += 'm'; } 6536 options = temp; 6537 } 6538 else { 6539 options = 'i'; 6540 } 6541 6542 return new RegExp(regexp, options); 6543 }, -2); 6544 $alias(self, "compile", "new"); 6545 return $alias(self, "quote", "escape"); 6546 })(Opal.get_singleton_class(self), $nesting); 6547 6548 $def(self, '$==', function $Regexp_$eq_eq$2(other) { 6549 var self = this; 6550 6551 return other instanceof RegExp && self.toString() === other.toString(); 6552 }); 6553 6554 $def(self, '$===', function $Regexp_$eq_eq_eq$3(string) { 6555 var self = this; 6556 6557 return self.$match($Opal['$coerce_to?'](string, $$$('String'), "to_str")) !== nil 6558 }); 6559 6560 $def(self, '$=~', function $Regexp_$eq_tilde$4(string) { 6561 var self = this, $ret_or_1 = nil; 6562 if ($gvars["~"] == null) $gvars["~"] = nil; 6563 6564 if ($truthy(($ret_or_1 = self.$match(string)))) { 6565 return $gvars["~"].$begin(0) 6566 } else { 6567 return $ret_or_1 6568 } 6569 }); 6570 6571 $def(self, '$freeze', function $$freeze() { 6572 var self = this; 6573 6574 6575 if ($truthy(self['$frozen?']())) { 6576 return self 6577 } 6578 if (!self.hasOwnProperty('$$g')) { $prop(self, '$$g', null); } 6579 if (!self.hasOwnProperty('$$gm')) { $prop(self, '$$gm', null); } 6580 6581 return $freeze(self); 6582 }); 6583 6584 $def(self, '$inspect', function $$inspect() { 6585 var self = this; 6586 6587 6588 var regexp_format = /^\/(.*)\/([^\/]*)$/; 6589 var value = self.toString(); 6590 var matches = regexp_format.exec(value); 6591 if (matches) { 6592 var regexp_pattern = matches[1]; 6593 var regexp_flags = matches[2]; 6594 var chars = regexp_pattern.split(''); 6595 var chars_length = chars.length; 6596 var char_escaped = false; 6597 var regexp_pattern_escaped = ''; 6598 for (var i = 0; i < chars_length; i++) { 6599 var current_char = chars[i]; 6600 if (!char_escaped && current_char == '/') { 6601 regexp_pattern_escaped = regexp_pattern_escaped.concat('\\'); 6602 } 6603 regexp_pattern_escaped = regexp_pattern_escaped.concat(current_char); 6604 if (current_char == '\\') { 6605 if (char_escaped) { 6606 // does not over escape 6607 char_escaped = false; 6608 } else { 6609 char_escaped = true; 6610 } 6611 } else { 6612 char_escaped = false; 6613 } 6614 } 6615 return '/' + regexp_pattern_escaped + '/' + regexp_flags; 6616 } else { 6617 return value; 6618 } 6619 6620 }); 6621 6622 $def(self, '$match', function $$match(string, pos) { 6623 var block = $$match.$$p || nil, self = this; 6624 if ($gvars["~"] == null) $gvars["~"] = nil; 6625 6626 $$match.$$p = null; 6627 6628 if (self.uninitialized) { 6629 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp"); 6630 } 6631 6632 if (pos === undefined) { 6633 if (string === nil) return ($gvars["~"] = nil); 6634 var m = self.exec($coerce_to(string, $$$('String'), 'to_str')); 6635 if (m) { 6636 ($gvars["~"] = $$$('MatchData').$new(self, m)); 6637 return block === nil ? $gvars["~"] : Opal.yield1(block, $gvars["~"]); 6638 } else { 6639 return ($gvars["~"] = nil); 6640 } 6641 } 6642 6643 pos = $coerce_to(pos, $$$('Integer'), 'to_int'); 6644 6645 if (string === nil) { 6646 return ($gvars["~"] = nil); 6647 } 6648 6649 string = $coerce_to(string, $$$('String'), 'to_str'); 6650 6651 if (pos < 0) { 6652 pos += string.length; 6653 if (pos < 0) { 6654 return ($gvars["~"] = nil); 6655 } 6656 } 6657 6658 // global RegExp maintains state, so not using self/this 6659 var md, re = Opal.global_regexp(self); 6660 6661 while (true) { 6662 md = re.exec(string); 6663 if (md === null) { 6664 return ($gvars["~"] = nil); 6665 } 6666 if (md.index >= pos) { 6667 ($gvars["~"] = $$$('MatchData').$new(re, md)); 6668 return block === nil ? $gvars["~"] : Opal.yield1(block, $gvars["~"]); 6669 } 6670 re.lastIndex = md.index + 1; 6671 } 6672 }, -2); 6673 6674 $def(self, '$match?', function $Regexp_match$ques$5(string, pos) { 6675 var self = this; 6676 6677 if (self.uninitialized) { 6678 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp"); 6679 } 6680 6681 if (pos === undefined) { 6682 return string === nil ? false : self.test($coerce_to(string, $$$('String'), 'to_str')); 6683 } 6684 6685 pos = $coerce_to(pos, $$$('Integer'), 'to_int'); 6686 6687 if (string === nil) { 6688 return false; 6689 } 6690 6691 string = $coerce_to(string, $$$('String'), 'to_str'); 6692 6693 if (pos < 0) { 6694 pos += string.length; 6695 if (pos < 0) { 6696 return false; 6697 } 6698 } 6699 6700 // global RegExp maintains state, so not using self/this 6701 var md, re = Opal.global_regexp(self); 6702 6703 md = re.exec(string); 6704 if (md === null || md.index < pos) { 6705 return false; 6706 } else { 6707 return true; 6708 } 6709 }, -2); 6710 6711 $def(self, '$names', function $$names() { 6712 var self = this; 6713 6714 return $send(self.$source().$scan(/\(?<(\w+)>/, $hash2(["no_matchdata"], {"no_matchdata": true})), 'map', [], "first".$to_proc()).$uniq() 6715 }); 6716 6717 $def(self, '$named_captures', function $$named_captures() { 6718 var self = this; 6719 6720 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){ 6721 6722 if (i == null) i = nil; 6723 return $send(i, 'map', [], function $$7(j){ 6724 6725 if (j == null) j = nil; 6726 return $rb_plus(j.$last(), 1);});}) 6727 }); 6728 6729 $def(self, '$~', function $Regexp_$$8() { 6730 var self = this; 6731 if ($gvars._ == null) $gvars._ = nil; 6732 6733 return self['$=~']($gvars._) 6734 }); 6735 6736 $def(self, '$source', function $$source() { 6737 var self = this; 6738 6739 return self.source; 6740 }); 6741 6742 $def(self, '$options', function $$options() { 6743 var self = this; 6744 6745 6746 if (self.uninitialized) { 6747 $Kernel.$raise($$$('TypeError'), "uninitialized Regexp"); 6748 } 6749 var result = 0; 6750 // should be supported in IE6 according to https://msdn.microsoft.com/en-us/library/7f5z26w4(v=vs.94).aspx 6751 if (self.multiline) { 6752 result |= $$('MULTILINE'); 6753 } 6754 if (self.ignoreCase) { 6755 result |= $$('IGNORECASE'); 6756 } 6757 return result; 6758 6759 }); 6760 6761 $def(self, '$casefold?', function $Regexp_casefold$ques$9() { 6762 var self = this; 6763 6764 return self.ignoreCase; 6765 }); 6766 $alias(self, "eql?", "=="); 6767 return $alias(self, "to_s", "source"); 6768 })('::', RegExp, $nesting); 6769 return (function($base, $super, $parent_nesting) { 6770 var self = $klass($base, $super, 'MatchData'); 6771 6772 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 6773 6774 $proto.matches = nil; 6775 6776 self.$attr_reader("post_match", "pre_match", "regexp", "string"); 6777 6778 $def(self, '$initialize', function $$initialize(regexp, match_groups, $kwargs) { 6779 var no_matchdata, self = this; 6780 6781 6782 $kwargs = $ensure_kwargs($kwargs); 6783 6784 no_matchdata = $kwargs.$$smap["no_matchdata"];if (no_matchdata == null) no_matchdata = false; 6785 if (!$truthy(no_matchdata)) { 6786 $gvars["~"] = self; 6787 } self.regexp = regexp; 6788 self.begin = match_groups.index; 6789 self.string = match_groups.input; 6790 self.pre_match = match_groups.input.slice(0, match_groups.index); 6791 self.post_match = match_groups.input.slice(match_groups.index + match_groups[0].length); 6792 self.matches = []; 6793 6794 for (var i = 0, length = match_groups.length; i < length; i++) { 6795 var group = match_groups[i]; 6796 6797 if (group == null) { 6798 self.matches.push(nil); 6799 } 6800 else { 6801 self.matches.push(group); 6802 } 6803 } 6804 }, -3); 6805 6806 $def(self, '$match', function $$match(idx) { 6807 var self = this, match = nil; 6808 6809 if ($truthy((match = self['$[]'](idx)))) { 6810 return match 6811 } else if (($truthy(idx['$is_a?']($$('Integer'))) && ($truthy($rb_ge(idx, self.$length()))))) { 6812 return $Kernel.$raise($$$('IndexError'), "index " + (idx) + " out of matches") 6813 } else { 6814 return nil 6815 } 6816 }); 6817 6818 $def(self, '$match_length', function $$match_length(idx) { 6819 var $a, self = this; 6820 6821 return ($a = self.$match(idx), ($a === nil || $a == null) ? nil : $a.$length()) 6822 }); 6823 6824 $def(self, '$[]', function $MatchData_$$$10($a) { 6825 var $post_args, args, self = this; 6826 6827 6828 $post_args = $slice(arguments); 6829 args = $post_args; 6830 6831 if (args[0].$$is_string) { 6832 if (self.$regexp().$names()['$include?'](args['$[]'](0))['$!']()) { 6833 $Kernel.$raise($$$('IndexError'), "undefined group name reference: " + (args['$[]'](0))); 6834 } 6835 return self.$named_captures()['$[]'](args['$[]'](0)) 6836 } 6837 else { 6838 return $send(self.matches, '[]', $to_a(args)) 6839 } 6840 }, -1); 6841 6842 $def(self, '$offset', function $$offset(n) { 6843 var self = this; 6844 6845 6846 if (n !== 0) { 6847 $Kernel.$raise($$$('ArgumentError'), "MatchData#offset only supports 0th element"); 6848 } 6849 return [self.begin, self.begin + self.matches[n].length]; 6850 6851 }); 6852 6853 $def(self, '$==', function $MatchData_$eq_eq$11(other) { 6854 var self = this, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, $ret_or_4 = nil; 6855 6856 6857 if (!$eqeqeq($$$('MatchData'), other)) { 6858 return false 6859 } 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))))) { 6860 return self.begin == other.begin; 6861 } else { 6862 return $ret_or_1 6863 } }); 6864 6865 $def(self, '$begin', function $$begin(n) { 6866 var self = this; 6867 6868 6869 if (n !== 0) { 6870 $Kernel.$raise($$$('ArgumentError'), "MatchData#begin only supports 0th element"); 6871 } 6872 return self.begin; 6873 6874 }); 6875 6876 $def(self, '$end', function $$end(n) { 6877 var self = this; 6878 6879 6880 if (n !== 0) { 6881 $Kernel.$raise($$$('ArgumentError'), "MatchData#end only supports 0th element"); 6882 } 6883 return self.begin + self.matches[n].length; 6884 6885 }); 6886 6887 $def(self, '$captures', function $$captures() { 6888 var self = this; 6889 6890 return self.matches.slice(1) 6891 }); 6892 6893 $def(self, '$named_captures', function $$named_captures() { 6894 var self = this, matches = nil; 6895 6896 6897 matches = self.$captures(); 6898 return $send(self.$regexp().$named_captures(), 'transform_values', [], function $$12(i){ 6899 6900 if (i == null) i = nil; 6901 return matches['$[]']($rb_minus(i.$last(), 1));}); 6902 }); 6903 6904 $def(self, '$names', function $$names() { 6905 var self = this; 6906 6907 return self.$regexp().$names() 6908 }); 6909 6910 $def(self, '$inspect', function $$inspect() { 6911 var self = this; 6912 6913 6914 var str = "#<MatchData " + (self.matches[0]).$inspect(); 6915 6916 if (self.$regexp().$names()['$empty?']()) { 6917 for (var i = 1, length = self.matches.length; i < length; i++) { 6918 str += " " + i + ":" + (self.matches[i]).$inspect(); 6919 } 6920 } 6921 else { 6922 $send(self.$named_captures(), 'each', [], function $$13(k, v){ 6923 6924 if (k == null) k = nil; 6925 if (v == null) v = nil; 6926 return str += " " + k + ":" + v.$inspect();}); 6927 } 6928 6929 return str + ">"; 6930 6931 }); 6932 6933 $def(self, '$length', function $$length() { 6934 var self = this; 6935 6936 return self.matches.length 6937 }); 6938 6939 $def(self, '$to_a', $return_ivar("matches")); 6940 6941 $def(self, '$to_s', function $$to_s() { 6942 var self = this; 6943 6944 return self.matches[0] 6945 }); 6946 6947 $def(self, '$values_at', function $$values_at($a) { 6948 var $post_args, args, self = this; 6949 6950 6951 $post_args = $slice(arguments); 6952 args = $post_args; 6953 6954 var i, a, index, values = []; 6955 6956 for (i = 0; i < args.length; i++) { 6957 6958 if (args[i].$$is_range) { 6959 a = (args[i]).$to_a(); 6960 a.unshift(i, 1); 6961 Array.prototype.splice.apply(args, a); 6962 } 6963 6964 index = $Opal['$coerce_to!'](args[i], $$$('Integer'), "to_int"); 6965 6966 if (index < 0) { 6967 index += self.matches.length; 6968 if (index < 0) { 6969 values.push(nil); 6970 continue; 6971 } 6972 } 6973 6974 values.push(self.matches[index]); 6975 } 6976 6977 return values; 6978 }, -1); 6979 $alias(self, "eql?", "=="); 6980 return $alias(self, "size", "length"); 6981 })($nesting[0], null, $nesting); 6982}; 6983 6984Opal.modules["corelib/string"] = function(Opal) {/* Generated by Opal 1.7.3 */ 6985 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.$$$; 6986 6987 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'); 6988 6989 self.$require("corelib/comparable"); 6990 self.$require("corelib/regexp"); 6991 (function($base, $super, $parent_nesting) { 6992 var self = $klass($base, $super, 'String'); 6993 6994 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 6995 6996 6997 self.$include($$$('Comparable')); 6998 6999 Opal.prop(self.$$prototype, '$$is_string', true); 7000 7001 $def(self, '$__id__', function $$__id__() { 7002 var self = this; 7003 7004 return self.toString(); 7005 }); 7006 $defs(self, '$try_convert', function $$try_convert(what) { 7007 7008 return $Opal['$coerce_to?'](what, $$$('String'), "to_str") 7009 }); 7010 $defs(self, '$new', function $String_new$1($a) { 7011 var $post_args, args, self = this; 7012 7013 7014 $post_args = $slice(arguments); 7015 args = $post_args; 7016 7017 var str = args[0] || ""; 7018 var opts = args[args.length-1]; 7019 str = $coerce_to(str, $$$('String'), 'to_str'); 7020 if (opts && opts.$$is_hash) { 7021 if (opts.$$smap.encoding) str = str.$force_encoding(opts.$$smap.encoding); 7022 } 7023 str = new self.$$constructor(str); 7024 if (!str.$initialize.$$pristine) $send((str), 'initialize', $to_a(args)); 7025 return str; 7026 }, -1); 7027 7028 $def(self, '$initialize', function $$initialize($a, $b) { 7029 var $post_args, $kwargs; 7030 7031 7032 $post_args = $slice(arguments); 7033 $kwargs = $extract_kwargs($post_args); 7034 $kwargs = $ensure_kwargs($kwargs); 7035 7036 if ($post_args.length > 0) $post_args.shift(); 7037 $kwargs.$$smap["encoding"]; 7038 $kwargs.$$smap["capacity"]; return nil; 7039 }, -1); 7040 7041 $def(self, '$%', function $String_$percent$2(data) { 7042 var self = this; 7043 7044 if ($eqeqeq($$$('Array'), data)) { 7045 return $send(self, 'format', [self].concat($to_a(data))) 7046 } else { 7047 return self.$format(self, data) 7048 } 7049 }); 7050 7051 $def(self, '$*', function $String_$$3(count) { 7052 var self = this; 7053 7054 7055 count = $coerce_to(count, $$$('Integer'), 'to_int'); 7056 7057 if (count < 0) { 7058 $Kernel.$raise($$$('ArgumentError'), "negative argument"); 7059 } 7060 7061 if (count === 0) { 7062 return ''; 7063 } 7064 7065 var result = '', 7066 string = self.toString(); 7067 7068 // All credit for the bit-twiddling magic code below goes to Mozilla 7069 // polyfill implementation of String.prototype.repeat() posted here: 7070 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat 7071 7072 if (string.length * count >= 1 << 28) { 7073 $Kernel.$raise($$$('RangeError'), "multiply count must not overflow maximum string size"); 7074 } 7075 7076 for (;;) { 7077 if ((count & 1) === 1) { 7078 result += string; 7079 } 7080 count >>>= 1; 7081 if (count === 0) { 7082 break; 7083 } 7084 string += string; 7085 } 7086 7087 return result; 7088 7089 }); 7090 7091 $def(self, '$+', function $String_$plus$4(other) { 7092 var self = this; 7093 7094 7095 other = $coerce_to(other, $$$('String'), 'to_str'); 7096 7097 if (other == "" && self.$$class === Opal.String) return self; 7098 if (self == "" && other.$$class === Opal.String) return other; 7099 var out = self + other; 7100 if (self.encoding === out.encoding && other.encoding === out.encoding) return out; 7101 if (self.encoding.name === "UTF-8" || other.encoding.name === "UTF-8") return out; 7102 return Opal.enc(out, self.encoding); 7103 }); 7104 7105 $def(self, '$<=>', function $String_$lt_eq_gt$5(other) { 7106 var self = this; 7107 7108 if ($truthy(other['$respond_to?']("to_str"))) { 7109 7110 other = other.$to_str().$to_s(); 7111 return self > other ? 1 : (self < other ? -1 : 0); } else { 7112 7113 var cmp = other['$<=>'](self); 7114 7115 if (cmp === nil) { 7116 return nil; 7117 } 7118 else { 7119 return cmp > 0 ? -1 : (cmp < 0 ? 1 : 0); 7120 } 7121 7122 } 7123 }); 7124 7125 $def(self, '$==', function $String_$eq_eq$6(other) { 7126 var self = this; 7127 7128 7129 if (other.$$is_string) { 7130 return self.toString() === other.toString(); 7131 } 7132 if ($respond_to(other, '$to_str')) { 7133 return other['$=='](self); 7134 } 7135 return false; 7136 7137 }); 7138 7139 $def(self, '$=~', function $String_$eq_tilde$7(other) { 7140 var self = this; 7141 7142 7143 if (other.$$is_string) { 7144 $Kernel.$raise($$$('TypeError'), "type mismatch: String given"); 7145 } 7146 7147 return other['$=~'](self); 7148 7149 }); 7150 7151 $def(self, '$[]', function $String_$$$8(index, length) { 7152 var self = this; 7153 7154 var size = self.length, exclude, range; 7155 7156 if (index.$$is_range) { 7157 exclude = index.excl; 7158 range = index; 7159 length = index.end === nil ? -1 : $coerce_to(index.end, $$$('Integer'), 'to_int'); 7160 index = index.begin === nil ? 0 : $coerce_to(index.begin, $$$('Integer'), 'to_int'); 7161 7162 if (Math.abs(index) > size) { 7163 return nil; 7164 } 7165 7166 if (index < 0) { 7167 index += size; 7168 } 7169 7170 if (length < 0) { 7171 length += size; 7172 } 7173 7174 if (!exclude || range.end === nil) { 7175 length += 1; 7176 } 7177 7178 length = length - index; 7179 7180 if (length < 0) { 7181 length = 0; 7182 } 7183 7184 return self.substr(index, length); 7185 } 7186 7187 7188 if (index.$$is_string) { 7189 if (length != null) { 7190 $Kernel.$raise($$$('TypeError')); 7191 } 7192 return self.indexOf(index) !== -1 ? index : nil; 7193 } 7194 7195 7196 if (index.$$is_regexp) { 7197 var match = self.match(index); 7198 7199 if (match === null) { 7200 ($gvars["~"] = nil); 7201 return nil; 7202 } 7203 7204 ($gvars["~"] = $$$('MatchData').$new(index, match)); 7205 7206 if (length == null) { 7207 return match[0]; 7208 } 7209 7210 length = $coerce_to(length, $$$('Integer'), 'to_int'); 7211 7212 if (length < 0 && -length < match.length) { 7213 return match[length += match.length]; 7214 } 7215 7216 if (length >= 0 && length < match.length) { 7217 return match[length]; 7218 } 7219 7220 return nil; 7221 } 7222 7223 7224 index = $coerce_to(index, $$$('Integer'), 'to_int'); 7225 7226 if (index < 0) { 7227 index += size; 7228 } 7229 7230 if (length == null) { 7231 if (index >= size || index < 0) { 7232 return nil; 7233 } 7234 return self.substr(index, 1); 7235 } 7236 7237 length = $coerce_to(length, $$$('Integer'), 'to_int'); 7238 7239 if (length < 0) { 7240 return nil; 7241 } 7242 7243 if (index > size || index < 0) { 7244 return nil; 7245 } 7246 7247 return self.substr(index, length); 7248 }, -2); 7249 7250 $def(self, '$b', function $$b() { 7251 var self = this; 7252 7253 return (new String(self)).$force_encoding("binary") 7254 }); 7255 7256 $def(self, '$capitalize', function $$capitalize() { 7257 var self = this; 7258 7259 return self.charAt(0).toUpperCase() + self.substr(1).toLowerCase(); 7260 }); 7261 7262 $def(self, '$casecmp', function $$casecmp(other) { 7263 var self = this; 7264 7265 7266 if (!$truthy(other['$respond_to?']("to_str"))) { 7267 return nil 7268 } other = ($coerce_to(other, $$$('String'), 'to_str')).$to_s(); 7269 7270 var ascii_only = /^[\x00-\x7F]*$/; 7271 if (ascii_only.test(self) && ascii_only.test(other)) { 7272 self = self.toLowerCase(); 7273 other = other.toLowerCase(); 7274 } 7275 return self['$<=>'](other); 7276 }); 7277 7278 $def(self, '$casecmp?', function $String_casecmp$ques$9(other) { 7279 var self = this; 7280 7281 7282 var cmp = self.$casecmp(other); 7283 if (cmp === nil) { 7284 return nil; 7285 } else { 7286 return cmp === 0; 7287 } 7288 7289 }); 7290 7291 $def(self, '$center', function $$center(width, padstr) { 7292 var self = this; 7293 7294 7295 if (padstr == null) padstr = " "; 7296 width = $coerce_to(width, $$$('Integer'), 'to_int'); 7297 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 7298 if ($truthy(padstr['$empty?']())) { 7299 $Kernel.$raise($$$('ArgumentError'), "zero width padding"); 7300 } if ($truthy(width <= self.length)) { 7301 return self 7302 } 7303 var ljustified = self.$ljust($rb_divide($rb_plus(width, self.length), 2).$ceil(), padstr), 7304 rjustified = self.$rjust($rb_divide($rb_plus(width, self.length), 2).$floor(), padstr); 7305 7306 return rjustified + ljustified.slice(self.length); 7307 }, -2); 7308 7309 $def(self, '$chomp', function $$chomp(separator) { 7310 var self = this; 7311 if ($gvars["/"] == null) $gvars["/"] = nil; 7312 7313 7314 if (separator == null) separator = $gvars["/"]; 7315 if ($truthy(separator === nil || self.length === 0)) { 7316 return self 7317 } separator = $Opal['$coerce_to!'](separator, $$$('String'), "to_str").$to_s(); 7318 7319 var result; 7320 7321 if (separator === "\n") { 7322 result = self.replace(/\r?\n?$/, ''); 7323 } 7324 else if (separator === "") { 7325 result = self.replace(/(\r?\n)+$/, ''); 7326 } 7327 else if (self.length >= separator.length) { 7328 var tail = self.substr(self.length - separator.length, separator.length); 7329 7330 if (tail === separator) { 7331 result = self.substr(0, self.length - separator.length); 7332 } 7333 } 7334 7335 if (result != null) { 7336 return result; 7337 } 7338 return self; 7339 }, -1); 7340 7341 $def(self, '$chop', function $$chop() { 7342 var self = this; 7343 7344 7345 var length = self.length, result; 7346 7347 if (length <= 1) { 7348 result = ""; 7349 } else if (self.charAt(length - 1) === "\n" && self.charAt(length - 2) === "\r") { 7350 result = self.substr(0, length - 2); 7351 } else { 7352 result = self.substr(0, length - 1); 7353 } 7354 7355 return result; 7356 7357 }); 7358 7359 $def(self, '$chr', function $$chr() { 7360 var self = this; 7361 7362 return self.charAt(0); 7363 }); 7364 7365 $def(self, '$clone', function $$clone($kwargs) { 7366 var freeze, self = this, copy = nil; 7367 7368 7369 $kwargs = $ensure_kwargs($kwargs); 7370 7371 freeze = $kwargs.$$smap["freeze"];if (freeze == null) freeze = nil; 7372 if (!(($truthy(freeze['$nil?']()) || ($eqeq(freeze, true))) || ($eqeq(freeze, false)))) { 7373 self.$raise($$('ArgumentError'), "unexpected value for freeze: " + (freeze.$class())); 7374 } copy = new String(self); 7375 copy.$copy_singleton_methods(self); 7376 copy.$initialize_clone(self, $hash2(["freeze"], {"freeze": freeze})); 7377 if ($eqeq(freeze, true)) { 7378 if (!copy.$$frozen) { copy.$$frozen = true; } 7379 } else if ($truthy(freeze['$nil?']())) { 7380 if (self.$$frozen) { copy.$$frozen = true; } 7381 } return copy; 7382 }, -1); 7383 7384 $def(self, '$dup', function $$dup() { 7385 var self = this, copy = nil; 7386 7387 7388 copy = new String(self); 7389 copy.$initialize_dup(self); 7390 return copy; 7391 }); 7392 7393 $def(self, '$count', function $$count($a) { 7394 var $post_args, sets, self = this; 7395 7396 7397 $post_args = $slice(arguments); 7398 sets = $post_args; 7399 7400 if (sets.length === 0) { 7401 $Kernel.$raise($$$('ArgumentError'), "ArgumentError: wrong number of arguments (0 for 1+)"); 7402 } 7403 var char_class = char_class_from_char_sets(sets); 7404 if (char_class === null) { 7405 return 0; 7406 } 7407 return self.length - self.replace(new RegExp(char_class, 'g'), '').length; 7408 }, -1); 7409 7410 $def(self, '$delete', function $String_delete$10($a) { 7411 var $post_args, sets, self = this; 7412 7413 7414 $post_args = $slice(arguments); 7415 sets = $post_args; 7416 7417 if (sets.length === 0) { 7418 $Kernel.$raise($$$('ArgumentError'), "ArgumentError: wrong number of arguments (0 for 1+)"); 7419 } 7420 var char_class = char_class_from_char_sets(sets); 7421 if (char_class === null) { 7422 return self; 7423 } 7424 return self.replace(new RegExp(char_class, 'g'), ''); 7425 }, -1); 7426 7427 $def(self, '$delete_prefix', function $$delete_prefix(prefix) { 7428 var self = this; 7429 7430 7431 if (!prefix.$$is_string) { 7432 prefix = $coerce_to(prefix, $$$('String'), 'to_str'); 7433 } 7434 7435 if (self.slice(0, prefix.length) === prefix) { 7436 return self.slice(prefix.length); 7437 } else { 7438 return self; 7439 } 7440 7441 }); 7442 7443 $def(self, '$delete_suffix', function $$delete_suffix(suffix) { 7444 var self = this; 7445 7446 7447 if (!suffix.$$is_string) { 7448 suffix = $coerce_to(suffix, $$$('String'), 'to_str'); 7449 } 7450 7451 if (self.slice(self.length - suffix.length) === suffix) { 7452 return self.slice(0, self.length - suffix.length); 7453 } else { 7454 return self; 7455 } 7456 7457 }); 7458 7459 $def(self, '$downcase', function $$downcase() { 7460 var self = this; 7461 7462 return self.toLowerCase(); 7463 }); 7464 7465 $def(self, '$each_line', function $$each_line($a, $b) { 7466 var block = $$each_line.$$p || nil, $post_args, $kwargs, separator, chomp, self = this; 7467 if ($gvars["/"] == null) $gvars["/"] = nil; 7468 7469 $$each_line.$$p = null; 7470 $post_args = $slice(arguments); 7471 $kwargs = $extract_kwargs($post_args); 7472 $kwargs = $ensure_kwargs($kwargs); 7473 7474 if ($post_args.length > 0) separator = $post_args.shift();if (separator == null) separator = $gvars["/"]; 7475 7476 chomp = $kwargs.$$smap["chomp"];if (chomp == null) chomp = false; 7477 if (!(block !== nil)) { 7478 return self.$enum_for("each_line", separator, $hash2(["chomp"], {"chomp": chomp})) 7479 } 7480 if (separator === nil) { 7481 Opal.yield1(block, self); 7482 7483 return self; 7484 } 7485 7486 separator = $coerce_to(separator, $$$('String'), 'to_str'); 7487 7488 var a, i, n, length, chomped, trailing, splitted, value; 7489 7490 if (separator.length === 0) { 7491 for (a = self.split(/((?:\r?\n){2})(?:(?:\r?\n)*)/), i = 0, n = a.length; i < n; i += 2) { 7492 if (a[i] || a[i + 1]) { 7493 value = (a[i] || "") + (a[i + 1] || ""); 7494 if (chomp) { 7495 value = (value).$chomp("\n"); 7496 } 7497 Opal.yield1(block, value); 7498 } 7499 } 7500 7501 return self; 7502 } 7503 7504 chomped = self.$chomp(separator); 7505 trailing = self.length != chomped.length; 7506 splitted = chomped.split(separator); 7507 7508 for (i = 0, length = splitted.length; i < length; i++) { 7509 value = splitted[i]; 7510 if (i < length - 1 || trailing) { 7511 value += separator; 7512 } 7513 if (chomp) { 7514 value = (value).$chomp(separator); 7515 } 7516 Opal.yield1(block, value); 7517 } 7518 return self; 7519 }, -1); 7520 7521 $def(self, '$empty?', function $String_empty$ques$11() { 7522 var self = this; 7523 7524 return self.length === 0; 7525 }); 7526 7527 $def(self, '$end_with?', function $String_end_with$ques$12($a) { 7528 var $post_args, suffixes, self = this; 7529 7530 7531 $post_args = $slice(arguments); 7532 suffixes = $post_args; 7533 7534 for (var i = 0, length = suffixes.length; i < length; i++) { 7535 var suffix = $coerce_to(suffixes[i], $$$('String'), 'to_str').$to_s(); 7536 7537 if (self.length >= suffix.length && 7538 self.substr(self.length - suffix.length, suffix.length) == suffix) { 7539 return true; 7540 } 7541 } 7542 return false; 7543 }, -1); 7544 7545 $def(self, '$gsub', function $$gsub(pattern, replacement) { 7546 var block = $$gsub.$$p || nil, self = this; 7547 7548 $$gsub.$$p = null; 7549 7550 if (replacement === undefined && block === nil) { 7551 return self.$enum_for("gsub", pattern); 7552 } 7553 7554 var result = '', match_data = nil, index = 0, match, _replacement; 7555 7556 if (pattern.$$is_regexp) { 7557 pattern = $global_multiline_regexp(pattern); 7558 } else { 7559 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 7560 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 7561 } 7562 7563 var lastIndex; 7564 while (true) { 7565 match = pattern.exec(self); 7566 7567 if (match === null) { 7568 ($gvars["~"] = nil); 7569 result += self.slice(index); 7570 break; 7571 } 7572 7573 match_data = $$$('MatchData').$new(pattern, match); 7574 7575 if (replacement === undefined) { 7576 lastIndex = pattern.lastIndex; 7577 _replacement = block(match[0]); 7578 pattern.lastIndex = lastIndex; // save and restore lastIndex 7579 } 7580 else if (replacement.$$is_hash) { 7581 _replacement = (replacement)['$[]'](match[0]).$to_s(); 7582 } 7583 else { 7584 if (!replacement.$$is_string) { 7585 replacement = $coerce_to(replacement, $$$('String'), 'to_str'); 7586 } 7587 _replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) { 7588 if (slashes.length % 2 === 0) { 7589 return original; 7590 } 7591 switch (command) { 7592 case "+": 7593 for (var i = match.length - 1; i > 0; i--) { 7594 if (match[i] !== undefined) { 7595 return slashes.slice(1) + match[i]; 7596 } 7597 } 7598 return ''; 7599 case "&": return slashes.slice(1) + match[0]; 7600 case "`": return slashes.slice(1) + self.slice(0, match.index); 7601 case "'": return slashes.slice(1) + self.slice(match.index + match[0].length); 7602 default: return slashes.slice(1) + (match[command] || ''); 7603 } 7604 }).replace(/\\\\/g, '\\'); 7605 } 7606 7607 if (pattern.lastIndex === match.index) { 7608 result += (self.slice(index, match.index) + _replacement + (self[match.index] || "")); 7609 pattern.lastIndex += 1; 7610 } 7611 else { 7612 result += (self.slice(index, match.index) + _replacement); 7613 } 7614 index = pattern.lastIndex; 7615 } 7616 7617 ($gvars["~"] = match_data); 7618 return result; 7619 }, -2); 7620 7621 $def(self, '$hash', function $$hash() { 7622 var self = this; 7623 7624 return self.toString(); 7625 }); 7626 7627 $def(self, '$hex', function $$hex() { 7628 var self = this; 7629 7630 return self.$to_i(16) 7631 }); 7632 7633 $def(self, '$include?', function $String_include$ques$13(other) { 7634 var self = this; 7635 7636 7637 if (!other.$$is_string) { 7638 other = $coerce_to(other, $$$('String'), 'to_str'); 7639 } 7640 return self.indexOf(other) !== -1; 7641 7642 }); 7643 7644 $def(self, '$index', function $$index(search, offset) { 7645 var self = this; 7646 7647 var index, 7648 match, 7649 regex; 7650 7651 if (offset === undefined) { 7652 offset = 0; 7653 } else { 7654 offset = $coerce_to(offset, $$$('Integer'), 'to_int'); 7655 if (offset < 0) { 7656 offset += self.length; 7657 if (offset < 0) { 7658 return nil; 7659 } 7660 } 7661 } 7662 7663 if (search.$$is_regexp) { 7664 regex = $global_multiline_regexp(search); 7665 while (true) { 7666 match = regex.exec(self); 7667 if (match === null) { 7668 ($gvars["~"] = nil); 7669 index = -1; 7670 break; 7671 } 7672 if (match.index >= offset) { 7673 ($gvars["~"] = $$$('MatchData').$new(regex, match)); 7674 index = match.index; 7675 break; 7676 } 7677 regex.lastIndex = match.index + 1; 7678 } 7679 } else { 7680 search = $coerce_to(search, $$$('String'), 'to_str'); 7681 if (search.length === 0 && offset > self.length) { 7682 index = -1; 7683 } else { 7684 index = self.indexOf(search, offset); 7685 } 7686 } 7687 7688 return index === -1 ? nil : index; 7689 }, -2); 7690 7691 $def(self, '$inspect', function $$inspect() { 7692 var self = this; 7693 7694 7695 /* eslint-disable no-misleading-character-class */ 7696 var escapable = /[\\\"\x00-\x1f\u007F-\u009F\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, 7697 meta = { 7698 '\u0007': '\\a', 7699 '\u001b': '\\e', 7700 '\b': '\\b', 7701 '\t': '\\t', 7702 '\n': '\\n', 7703 '\f': '\\f', 7704 '\r': '\\r', 7705 '\v': '\\v', 7706 '"' : '\\"', 7707 '\\': '\\\\' 7708 }, 7709 escaped = self.replace(escapable, function (chr) { 7710 if (meta[chr]) return meta[chr]; 7711 chr = chr.charCodeAt(0); 7712 if (chr <= 0xff && (self.encoding["$binary?"]() || self.internal_encoding["$binary?"]())) { 7713 return '\\x' + ('00' + chr.toString(16).toUpperCase()).slice(-2); 7714 } else { 7715 return '\\u' + ('0000' + chr.toString(16).toUpperCase()).slice(-4); 7716 } 7717 }); 7718 return '"' + escaped.replace(/\#[\$\@\{]/g, '\\$&') + '"'; 7719 /* eslint-enable no-misleading-character-class */ 7720 7721 }); 7722 7723 $def(self, '$intern', function $$intern() { 7724 var self = this; 7725 7726 return self.toString(); 7727 }); 7728 7729 $def(self, '$length', function $$length() { 7730 var self = this; 7731 7732 return self.length; 7733 }); 7734 $alias(self, "size", "length"); 7735 7736 $def(self, '$lines', function $$lines($a, $b) { 7737 var block = $$lines.$$p || nil, $post_args, $kwargs, separator, chomp, self = this, e = nil; 7738 if ($gvars["/"] == null) $gvars["/"] = nil; 7739 7740 $$lines.$$p = null; 7741 $post_args = $slice(arguments); 7742 $kwargs = $extract_kwargs($post_args); 7743 $kwargs = $ensure_kwargs($kwargs); 7744 7745 if ($post_args.length > 0) separator = $post_args.shift();if (separator == null) separator = $gvars["/"]; 7746 7747 chomp = $kwargs.$$smap["chomp"];if (chomp == null) chomp = false; 7748 e = $send(self, 'each_line', [separator, $hash2(["chomp"], {"chomp": chomp})], block.$to_proc()); 7749 if ($truthy(block)) { 7750 return self 7751 } else { 7752 return e.$to_a() 7753 } }, -1); 7754 7755 $def(self, '$ljust', function $$ljust(width, padstr) { 7756 var self = this; 7757 7758 7759 if (padstr == null) padstr = " "; 7760 width = $coerce_to(width, $$$('Integer'), 'to_int'); 7761 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 7762 if ($truthy(padstr['$empty?']())) { 7763 $Kernel.$raise($$$('ArgumentError'), "zero width padding"); 7764 } if ($truthy(width <= self.length)) { 7765 return self 7766 } 7767 var index = -1, 7768 result = ""; 7769 7770 width -= self.length; 7771 7772 while (++index < width) { 7773 result += padstr; 7774 } 7775 7776 return self + result.slice(0, width); 7777 }, -2); 7778 7779 $def(self, '$lstrip', function $$lstrip() { 7780 var self = this; 7781 7782 return self.replace(/^[\u0000\s]*/, ''); 7783 }); 7784 7785 $def(self, '$ascii_only?', function $String_ascii_only$ques$14() { 7786 var self = this; 7787 7788 7789 if (!self.encoding.ascii) return false; 7790 return /^[\x00-\x7F]*$/.test(self); 7791 7792 }); 7793 7794 $def(self, '$match', function $$match(pattern, pos) { 7795 var block = $$match.$$p || nil, self = this; 7796 7797 $$match.$$p = null; 7798 if (($eqeqeq($$('String'), pattern) || ($truthy(pattern['$respond_to?']("to_str"))))) { 7799 pattern = $$$('Regexp').$new(pattern.$to_str()); 7800 } if (!$eqeqeq($$$('Regexp'), pattern)) { 7801 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (pattern.$class()) + " (expected Regexp)"); 7802 } return $send(pattern, 'match', [self, pos], block.$to_proc()); 7803 }, -2); 7804 7805 $def(self, '$match?', function $String_match$ques$15(pattern, pos) { 7806 var self = this; 7807 if (($eqeqeq($$('String'), pattern) || ($truthy(pattern['$respond_to?']("to_str"))))) { 7808 pattern = $$$('Regexp').$new(pattern.$to_str()); 7809 } if (!$eqeqeq($$$('Regexp'), pattern)) { 7810 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (pattern.$class()) + " (expected Regexp)"); 7811 } return pattern['$match?'](self, pos); 7812 }, -2); 7813 7814 $def(self, '$next', function $$next() { 7815 var self = this; 7816 7817 7818 var i = self.length; 7819 if (i === 0) { 7820 return ''; 7821 } 7822 var result = self; 7823 var first_alphanum_char_index = self.search(/[a-zA-Z0-9]/); 7824 var carry = false; 7825 var code; 7826 while (i--) { 7827 code = self.charCodeAt(i); 7828 if ((code >= 48 && code <= 57) || 7829 (code >= 65 && code <= 90) || 7830 (code >= 97 && code <= 122)) { 7831 switch (code) { 7832 case 57: 7833 carry = true; 7834 code = 48; 7835 break; 7836 case 90: 7837 carry = true; 7838 code = 65; 7839 break; 7840 case 122: 7841 carry = true; 7842 code = 97; 7843 break; 7844 default: 7845 carry = false; 7846 code += 1; 7847 } 7848 } else { 7849 if (first_alphanum_char_index === -1) { 7850 if (code === 255) { 7851 carry = true; 7852 code = 0; 7853 } else { 7854 carry = false; 7855 code += 1; 7856 } 7857 } else { 7858 carry = true; 7859 } 7860 } 7861 result = result.slice(0, i) + String.fromCharCode(code) + result.slice(i + 1); 7862 if (carry && (i === 0 || i === first_alphanum_char_index)) { 7863 switch (code) { 7864 case 65: 7865 break; 7866 case 97: 7867 break; 7868 default: 7869 code += 1; 7870 } 7871 if (i === 0) { 7872 result = String.fromCharCode(code) + result; 7873 } else { 7874 result = result.slice(0, i) + String.fromCharCode(code) + result.slice(i); 7875 } 7876 carry = false; 7877 } 7878 if (!carry) { 7879 break; 7880 } 7881 } 7882 return result; 7883 7884 }); 7885 7886 $def(self, '$oct', function $$oct() { 7887 var self = this; 7888 7889 7890 var result, 7891 string = self, 7892 radix = 8; 7893 7894 if (/^\s*_/.test(string)) { 7895 return 0; 7896 } 7897 7898 string = string.replace(/^(\s*[+-]?)(0[bodx]?)(.+)$/i, function (original, head, flag, tail) { 7899 switch (tail.charAt(0)) { 7900 case '+': 7901 case '-': 7902 return original; 7903 case '0': 7904 if (tail.charAt(1) === 'x' && flag === '0x') { 7905 return original; 7906 } 7907 } 7908 switch (flag) { 7909 case '0b': 7910 radix = 2; 7911 break; 7912 case '0': 7913 case '0o': 7914 radix = 8; 7915 break; 7916 case '0d': 7917 radix = 10; 7918 break; 7919 case '0x': 7920 radix = 16; 7921 break; 7922 } 7923 return head + tail; 7924 }); 7925 7926 result = parseInt(string.replace(/_(?!_)/g, ''), radix); 7927 return isNaN(result) ? 0 : result; 7928 7929 }); 7930 7931 $def(self, '$ord', function $$ord() { 7932 var self = this; 7933 7934 7935 if (typeof self.codePointAt === "function") { 7936 return self.codePointAt(0); 7937 } 7938 else { 7939 return self.charCodeAt(0); 7940 } 7941 7942 }); 7943 7944 $def(self, '$partition', function $$partition(sep) { 7945 var self = this; 7946 7947 7948 var i, m; 7949 7950 if (sep.$$is_regexp) { 7951 m = sep.exec(self); 7952 if (m === null) { 7953 i = -1; 7954 } else { 7955 $$$('MatchData').$new(sep, m); 7956 sep = m[0]; 7957 i = m.index; 7958 } 7959 } else { 7960 sep = $coerce_to(sep, $$$('String'), 'to_str'); 7961 i = self.indexOf(sep); 7962 } 7963 7964 if (i === -1) { 7965 return [self, '', '']; 7966 } 7967 7968 return [ 7969 self.slice(0, i), 7970 self.slice(i, i + sep.length), 7971 self.slice(i + sep.length) 7972 ]; 7973 7974 }); 7975 7976 $def(self, '$reverse', function $$reverse() { 7977 var self = this; 7978 7979 return self.split('').reverse().join(''); 7980 }); 7981 7982 $def(self, '$rindex', function $$rindex(search, offset) { 7983 var self = this; 7984 7985 var i, m, r, _m; 7986 7987 if (offset === undefined) { 7988 offset = self.length; 7989 } else { 7990 offset = $coerce_to(offset, $$$('Integer'), 'to_int'); 7991 if (offset < 0) { 7992 offset += self.length; 7993 if (offset < 0) { 7994 return nil; 7995 } 7996 } 7997 } 7998 7999 if (search.$$is_regexp) { 8000 m = null; 8001 r = $global_multiline_regexp(search); 8002 while (true) { 8003 _m = r.exec(self); 8004 if (_m === null || _m.index > offset) { 8005 break; 8006 } 8007 m = _m; 8008 r.lastIndex = m.index + 1; 8009 } 8010 if (m === null) { 8011 ($gvars["~"] = nil); 8012 i = -1; 8013 } else { 8014 $$$('MatchData').$new(r, m); 8015 i = m.index; 8016 } 8017 } else { 8018 search = $coerce_to(search, $$$('String'), 'to_str'); 8019 i = self.lastIndexOf(search, offset); 8020 } 8021 8022 return i === -1 ? nil : i; 8023 }, -2); 8024 8025 $def(self, '$rjust', function $$rjust(width, padstr) { 8026 var self = this; 8027 8028 8029 if (padstr == null) padstr = " "; 8030 width = $coerce_to(width, $$$('Integer'), 'to_int'); 8031 padstr = ($coerce_to(padstr, $$$('String'), 'to_str')).$to_s(); 8032 if ($truthy(padstr['$empty?']())) { 8033 $Kernel.$raise($$$('ArgumentError'), "zero width padding"); 8034 } if ($truthy(width <= self.length)) { 8035 return self 8036 } 8037 var chars = Math.floor(width - self.length), 8038 patterns = Math.floor(chars / padstr.length), 8039 result = Array(patterns + 1).join(padstr), 8040 remaining = chars - result.length; 8041 8042 return result + padstr.slice(0, remaining) + self; 8043 }, -2); 8044 8045 $def(self, '$rpartition', function $$rpartition(sep) { 8046 var self = this; 8047 8048 8049 var i, m, r, _m; 8050 8051 if (sep.$$is_regexp) { 8052 m = null; 8053 r = $global_multiline_regexp(sep); 8054 8055 while (true) { 8056 _m = r.exec(self); 8057 if (_m === null) { 8058 break; 8059 } 8060 m = _m; 8061 r.lastIndex = m.index + 1; 8062 } 8063 8064 if (m === null) { 8065 i = -1; 8066 } else { 8067 $$$('MatchData').$new(r, m); 8068 sep = m[0]; 8069 i = m.index; 8070 } 8071 8072 } else { 8073 sep = $coerce_to(sep, $$$('String'), 'to_str'); 8074 i = self.lastIndexOf(sep); 8075 } 8076 8077 if (i === -1) { 8078 return ['', '', self]; 8079 } 8080 8081 return [ 8082 self.slice(0, i), 8083 self.slice(i, i + sep.length), 8084 self.slice(i + sep.length) 8085 ]; 8086 8087 }); 8088 8089 $def(self, '$rstrip', function $$rstrip() { 8090 var self = this; 8091 8092 return self.replace(/[\s\u0000]*$/, ''); 8093 }); 8094 8095 $def(self, '$scan', function $$scan(pattern, $kwargs) { 8096 var block = $$scan.$$p || nil, no_matchdata, self = this; 8097 8098 $$scan.$$p = null; 8099 $kwargs = $ensure_kwargs($kwargs); 8100 8101 no_matchdata = $kwargs.$$smap["no_matchdata"];if (no_matchdata == null) no_matchdata = false; 8102 8103 var result = [], 8104 match_data = nil, 8105 match; 8106 8107 if (pattern.$$is_regexp) { 8108 pattern = $global_multiline_regexp(pattern); 8109 } else { 8110 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 8111 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 8112 } 8113 8114 while ((match = pattern.exec(self)) != null) { 8115 match_data = $$$('MatchData').$new(pattern, match, $hash2(["no_matchdata"], {"no_matchdata": no_matchdata})); 8116 if (block === nil) { 8117 match.length == 1 ? result.push(match[0]) : result.push((match_data).$captures()); 8118 } else { 8119 match.length == 1 ? Opal.yield1(block, match[0]) : Opal.yield1(block, (match_data).$captures()); 8120 } 8121 if (pattern.lastIndex === match.index) { 8122 pattern.lastIndex += 1; 8123 } 8124 } 8125 8126 if (!no_matchdata) ($gvars["~"] = match_data); 8127 8128 return (block !== nil ? self : result); 8129 }, -2); 8130 8131 $def(self, '$singleton_class', function $$singleton_class() { 8132 var self = this; 8133 8134 return Opal.get_singleton_class(self); 8135 }); 8136 8137 $def(self, '$split', function $$split(pattern, limit) { 8138 var self = this, $ret_or_1 = nil; 8139 if ($gvars[";"] == null) $gvars[";"] = nil; 8140 8141 if (self.length === 0) { 8142 return []; 8143 } 8144 8145 if (limit === undefined) { 8146 limit = 0; 8147 } else { 8148 limit = $Opal['$coerce_to!'](limit, $$$('Integer'), "to_int"); 8149 if (limit === 1) { 8150 return [self]; 8151 } 8152 } 8153 8154 if (pattern === undefined || pattern === nil) { 8155 pattern = ($truthy(($ret_or_1 = $gvars[";"])) ? ($ret_or_1) : (" ")); 8156 } 8157 8158 var result = [], 8159 string = self.toString(), 8160 index = 0, 8161 match, 8162 i, ii; 8163 8164 if (pattern.$$is_regexp) { 8165 pattern = $global_multiline_regexp(pattern); 8166 } else { 8167 pattern = $coerce_to(pattern, $$$('String'), 'to_str').$to_s(); 8168 if (pattern === ' ') { 8169 pattern = /\s+/gm; 8170 string = string.replace(/^\s+/, ''); 8171 } else { 8172 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gm'); 8173 } 8174 } 8175 8176 result = string.split(pattern); 8177 8178 if (result.length === 1 && result[0] === string) { 8179 return [result[0]]; 8180 } 8181 8182 while ((i = result.indexOf(undefined)) !== -1) { 8183 result.splice(i, 1); 8184 } 8185 8186 if (limit === 0) { 8187 while (result[result.length - 1] === '') { 8188 result.length -= 1; 8189 } 8190 return result; 8191 } 8192 8193 match = pattern.exec(string); 8194 8195 if (limit < 0) { 8196 if (match !== null && match[0] === '' && pattern.source.indexOf('(?=') === -1) { 8197 for (i = 0, ii = match.length; i < ii; i++) { 8198 result.push(''); 8199 } 8200 } 8201 return result; 8202 } 8203 8204 if (match !== null && match[0] === '') { 8205 result.splice(limit - 1, result.length - 1, result.slice(limit - 1).join('')); 8206 return result; 8207 } 8208 8209 if (limit >= result.length) { 8210 return result; 8211 } 8212 8213 i = 0; 8214 while (match !== null) { 8215 i++; 8216 index = pattern.lastIndex; 8217 if (i + 1 === limit) { 8218 break; 8219 } 8220 match = pattern.exec(string); 8221 } 8222 result.splice(limit - 1, result.length - 1, string.slice(index)); 8223 return result; 8224 }, -1); 8225 8226 $def(self, '$squeeze', function $$squeeze($a) { 8227 var $post_args, sets, self = this; 8228 8229 8230 $post_args = $slice(arguments); 8231 sets = $post_args; 8232 8233 if (sets.length === 0) { 8234 return self.replace(/(.)\1+/g, '$1'); 8235 } 8236 var char_class = char_class_from_char_sets(sets); 8237 if (char_class === null) { 8238 return self; 8239 } 8240 return self.replace(new RegExp('(' + char_class + ')\\1+', 'g'), '$1'); 8241 }, -1); 8242 8243 $def(self, '$start_with?', function $String_start_with$ques$16($a) { 8244 var $post_args, prefixes, self = this; 8245 8246 8247 $post_args = $slice(arguments); 8248 prefixes = $post_args; 8249 8250 for (var i = 0, length = prefixes.length; i < length; i++) { 8251 if (prefixes[i].$$is_regexp) { 8252 var regexp = prefixes[i]; 8253 var match = regexp.exec(self); 8254 8255 if (match != null && match.index === 0) { 8256 ($gvars["~"] = $$$('MatchData').$new(regexp, match)); 8257 return true; 8258 } else { 8259 ($gvars["~"] = nil); 8260 } 8261 } else { 8262 var prefix = $coerce_to(prefixes[i], $$$('String'), 'to_str').$to_s(); 8263 8264 if (self.indexOf(prefix) === 0) { 8265 return true; 8266 } 8267 } 8268 } 8269 8270 return false; 8271 }, -1); 8272 8273 $def(self, '$strip', function $$strip() { 8274 var self = this; 8275 8276 return self.replace(/^[\s\u0000]*|[\s\u0000]*$/g, ''); 8277 }); 8278 8279 $def(self, '$sub', function $$sub(pattern, replacement) { 8280 var block = $$sub.$$p || nil, self = this; 8281 8282 $$sub.$$p = null; 8283 8284 if (!pattern.$$is_regexp) { 8285 pattern = $coerce_to(pattern, $$$('String'), 'to_str'); 8286 pattern = new RegExp(pattern.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')); 8287 } 8288 8289 var result, match = pattern.exec(self); 8290 8291 if (match === null) { 8292 ($gvars["~"] = nil); 8293 result = self.toString(); 8294 } else { 8295 $$$('MatchData').$new(pattern, match); 8296 8297 if (replacement === undefined) { 8298 8299 if (block === nil) { 8300 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (1 for 2)"); 8301 } 8302 result = self.slice(0, match.index) + block(match[0]) + self.slice(match.index + match[0].length); 8303 8304 } else if (replacement.$$is_hash) { 8305 8306 result = self.slice(0, match.index) + (replacement)['$[]'](match[0]).$to_s() + self.slice(match.index + match[0].length); 8307 8308 } else { 8309 8310 replacement = $coerce_to(replacement, $$$('String'), 'to_str'); 8311 8312 replacement = replacement.replace(/([\\]+)([0-9+&`'])/g, function (original, slashes, command) { 8313 if (slashes.length % 2 === 0) { 8314 return original; 8315 } 8316 switch (command) { 8317 case "+": 8318 for (var i = match.length - 1; i > 0; i--) { 8319 if (match[i] !== undefined) { 8320 return slashes.slice(1) + match[i]; 8321 } 8322 } 8323 return ''; 8324 case "&": return slashes.slice(1) + match[0]; 8325 case "`": return slashes.slice(1) + self.slice(0, match.index); 8326 case "'": return slashes.slice(1) + self.slice(match.index + match[0].length); 8327 default: return slashes.slice(1) + (match[command] || ''); 8328 } 8329 }).replace(/\\\\/g, '\\'); 8330 8331 result = self.slice(0, match.index) + replacement + self.slice(match.index + match[0].length); 8332 } 8333 } 8334 8335 return result; 8336 }, -2); 8337 8338 $def(self, '$sum', function $$sum(n) { 8339 var self = this; 8340 8341 8342 if (n == null) n = 16; 8343 8344 n = $coerce_to(n, $$$('Integer'), 'to_int'); 8345 8346 var result = 0, 8347 length = self.length, 8348 i = 0; 8349 8350 for (; i < length; i++) { 8351 result += self.charCodeAt(i); 8352 } 8353 8354 if (n <= 0) { 8355 return result; 8356 } 8357 8358 return result & (Math.pow(2, n) - 1); 8359 }, -1); 8360 8361 $def(self, '$swapcase', function $$swapcase() { 8362 var self = this; 8363 8364 8365 var str = self.replace(/([a-z]+)|([A-Z]+)/g, function($0,$1,$2) { 8366 return $1 ? $0.toUpperCase() : $0.toLowerCase(); 8367 }); 8368 8369 return str; 8370 8371 }); 8372 8373 $def(self, '$to_f', function $$to_f() { 8374 var self = this; 8375 8376 8377 if (self.charAt(0) === '_') { 8378 return 0; 8379 } 8380 8381 var result = parseFloat(self.replace(/_/g, '')); 8382 8383 if (isNaN(result) || result == Infinity || result == -Infinity) { 8384 return 0; 8385 } 8386 else { 8387 return result; 8388 } 8389 8390 }); 8391 8392 $def(self, '$to_i', function $$to_i(base) { 8393 var self = this; 8394 8395 8396 if (base == null) base = 10; 8397 8398 var result, 8399 string = self.toLowerCase(), 8400 radix = $coerce_to(base, $$$('Integer'), 'to_int'); 8401 8402 if (radix === 1 || radix < 0 || radix > 36) { 8403 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (radix)); 8404 } 8405 8406 if (/^\s*_/.test(string)) { 8407 return 0; 8408 } 8409 8410 string = string.replace(/^(\s*[+-]?)(0[bodx]?)(.+)$/, function (original, head, flag, tail) { 8411 switch (tail.charAt(0)) { 8412 case '+': 8413 case '-': 8414 return original; 8415 case '0': 8416 if (tail.charAt(1) === 'x' && flag === '0x' && (radix === 0 || radix === 16)) { 8417 return original; 8418 } 8419 } 8420 switch (flag) { 8421 case '0b': 8422 if (radix === 0 || radix === 2) { 8423 radix = 2; 8424 return head + tail; 8425 } 8426 break; 8427 case '0': 8428 case '0o': 8429 if (radix === 0 || radix === 8) { 8430 radix = 8; 8431 return head + tail; 8432 } 8433 break; 8434 case '0d': 8435 if (radix === 0 || radix === 10) { 8436 radix = 10; 8437 return head + tail; 8438 } 8439 break; 8440 case '0x': 8441 if (radix === 0 || radix === 16) { 8442 radix = 16; 8443 return head + tail; 8444 } 8445 break; 8446 } 8447 return original 8448 }); 8449 8450 result = parseInt(string.replace(/_(?!_)/g, ''), radix); 8451 return isNaN(result) ? 0 : result; 8452 }, -1); 8453 8454 $def(self, '$to_proc', function $$to_proc() { 8455 var self = this, method_name = nil, jsid = nil, proc = nil; 8456 8457 $$to_proc.$$p = null; 8458 8459 method_name = self.valueOf(); 8460 jsid = Opal.jsid(method_name); 8461 proc = $send($Kernel, 'proc', [], function $$17($a){var block = $$17.$$p || nil, $post_args, args; 8462 8463 $$17.$$p = null; 8464 $post_args = $slice(arguments); 8465 args = $post_args; 8466 8467 if (args.length === 0) { 8468 $Kernel.$raise($$$('ArgumentError'), "no receiver given"); 8469 } 8470 8471 var recv = args[0]; 8472 8473 if (recv == null) recv = nil; 8474 8475 var body = recv[jsid]; 8476 8477 if (!body) { 8478 body = recv.$method_missing; 8479 args[0] = method_name; 8480 } else { 8481 args = args.slice(1); 8482 } 8483 8484 if (typeof block === 'function') { 8485 body.$$p = block; 8486 } 8487 8488 if (args.length === 0) { 8489 return body.call(recv); 8490 } else { 8491 return body.apply(recv, args); 8492 } 8493}, -1); 8494 proc.$$source_location = nil; 8495 return proc; 8496 }); 8497 8498 $def(self, '$to_s', function $$to_s() { 8499 var self = this; 8500 8501 return self.toString(); 8502 }); 8503 8504 $def(self, '$tr', function $$tr(from, to) { 8505 var self = this; 8506 8507 8508 from = $coerce_to(from, $$$('String'), 'to_str').$to_s(); 8509 to = $coerce_to(to, $$$('String'), 'to_str').$to_s(); 8510 8511 if (from.length == 0 || from === to) { 8512 return self; 8513 } 8514 8515 var i, in_range, c, ch, start, end, length; 8516 var subs = {}; 8517 var from_chars = from.split(''); 8518 var from_length = from_chars.length; 8519 var to_chars = to.split(''); 8520 var to_length = to_chars.length; 8521 8522 var inverse = false; 8523 var global_sub = null; 8524 if (from_chars[0] === '^' && from_chars.length > 1) { 8525 inverse = true; 8526 from_chars.shift(); 8527 global_sub = to_chars[to_length - 1]; 8528 from_length -= 1; 8529 } 8530 8531 var from_chars_expanded = []; 8532 var last_from = null; 8533 in_range = false; 8534 for (i = 0; i < from_length; i++) { 8535 ch = from_chars[i]; 8536 if (last_from == null) { 8537 last_from = ch; 8538 from_chars_expanded.push(ch); 8539 } 8540 else if (ch === '-') { 8541 if (last_from === '-') { 8542 from_chars_expanded.push('-'); 8543 from_chars_expanded.push('-'); 8544 } 8545 else if (i == from_length - 1) { 8546 from_chars_expanded.push('-'); 8547 } 8548 else { 8549 in_range = true; 8550 } 8551 } 8552 else if (in_range) { 8553 start = last_from.charCodeAt(0); 8554 end = ch.charCodeAt(0); 8555 if (start > end) { 8556 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration"); 8557 } 8558 for (c = start + 1; c < end; c++) { 8559 from_chars_expanded.push(String.fromCharCode(c)); 8560 } 8561 from_chars_expanded.push(ch); 8562 in_range = null; 8563 last_from = null; 8564 } 8565 else { 8566 from_chars_expanded.push(ch); 8567 } 8568 } 8569 8570 from_chars = from_chars_expanded; 8571 from_length = from_chars.length; 8572 8573 if (inverse) { 8574 for (i = 0; i < from_length; i++) { 8575 subs[from_chars[i]] = true; 8576 } 8577 } 8578 else { 8579 if (to_length > 0) { 8580 var to_chars_expanded = []; 8581 var last_to = null; 8582 in_range = false; 8583 for (i = 0; i < to_length; i++) { 8584 ch = to_chars[i]; 8585 if (last_to == null) { 8586 last_to = ch; 8587 to_chars_expanded.push(ch); 8588 } 8589 else if (ch === '-') { 8590 if (last_to === '-') { 8591 to_chars_expanded.push('-'); 8592 to_chars_expanded.push('-'); 8593 } 8594 else if (i == to_length - 1) { 8595 to_chars_expanded.push('-'); 8596 } 8597 else { 8598 in_range = true; 8599 } 8600 } 8601 else if (in_range) { 8602 start = last_to.charCodeAt(0); 8603 end = ch.charCodeAt(0); 8604 if (start > end) { 8605 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration"); 8606 } 8607 for (c = start + 1; c < end; c++) { 8608 to_chars_expanded.push(String.fromCharCode(c)); 8609 } 8610 to_chars_expanded.push(ch); 8611 in_range = null; 8612 last_to = null; 8613 } 8614 else { 8615 to_chars_expanded.push(ch); 8616 } 8617 } 8618 8619 to_chars = to_chars_expanded; 8620 to_length = to_chars.length; 8621 } 8622 8623 var length_diff = from_length - to_length; 8624 if (length_diff > 0) { 8625 var pad_char = (to_length > 0 ? to_chars[to_length - 1] : ''); 8626 for (i = 0; i < length_diff; i++) { 8627 to_chars.push(pad_char); 8628 } 8629 } 8630 8631 for (i = 0; i < from_length; i++) { 8632 subs[from_chars[i]] = to_chars[i]; 8633 } 8634 } 8635 8636 var new_str = ''; 8637 for (i = 0, length = self.length; i < length; i++) { 8638 ch = self.charAt(i); 8639 var sub = subs[ch]; 8640 if (inverse) { 8641 new_str += (sub == null ? global_sub : ch); 8642 } 8643 else { 8644 new_str += (sub != null ? sub : ch); 8645 } 8646 } 8647 return new_str; 8648 8649 }); 8650 8651 $def(self, '$tr_s', function $$tr_s(from, to) { 8652 var self = this; 8653 8654 8655 from = $coerce_to(from, $$$('String'), 'to_str').$to_s(); 8656 to = $coerce_to(to, $$$('String'), 'to_str').$to_s(); 8657 8658 if (from.length == 0) { 8659 return self; 8660 } 8661 8662 var i, in_range, c, ch, start, end, length; 8663 var subs = {}; 8664 var from_chars = from.split(''); 8665 var from_length = from_chars.length; 8666 var to_chars = to.split(''); 8667 var to_length = to_chars.length; 8668 8669 var inverse = false; 8670 var global_sub = null; 8671 if (from_chars[0] === '^' && from_chars.length > 1) { 8672 inverse = true; 8673 from_chars.shift(); 8674 global_sub = to_chars[to_length - 1]; 8675 from_length -= 1; 8676 } 8677 8678 var from_chars_expanded = []; 8679 var last_from = null; 8680 in_range = false; 8681 for (i = 0; i < from_length; i++) { 8682 ch = from_chars[i]; 8683 if (last_from == null) { 8684 last_from = ch; 8685 from_chars_expanded.push(ch); 8686 } 8687 else if (ch === '-') { 8688 if (last_from === '-') { 8689 from_chars_expanded.push('-'); 8690 from_chars_expanded.push('-'); 8691 } 8692 else if (i == from_length - 1) { 8693 from_chars_expanded.push('-'); 8694 } 8695 else { 8696 in_range = true; 8697 } 8698 } 8699 else if (in_range) { 8700 start = last_from.charCodeAt(0); 8701 end = ch.charCodeAt(0); 8702 if (start > end) { 8703 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration"); 8704 } 8705 for (c = start + 1; c < end; c++) { 8706 from_chars_expanded.push(String.fromCharCode(c)); 8707 } 8708 from_chars_expanded.push(ch); 8709 in_range = null; 8710 last_from = null; 8711 } 8712 else { 8713 from_chars_expanded.push(ch); 8714 } 8715 } 8716 8717 from_chars = from_chars_expanded; 8718 from_length = from_chars.length; 8719 8720 if (inverse) { 8721 for (i = 0; i < from_length; i++) { 8722 subs[from_chars[i]] = true; 8723 } 8724 } 8725 else { 8726 if (to_length > 0) { 8727 var to_chars_expanded = []; 8728 var last_to = null; 8729 in_range = false; 8730 for (i = 0; i < to_length; i++) { 8731 ch = to_chars[i]; 8732 if (last_from == null) { 8733 last_from = ch; 8734 to_chars_expanded.push(ch); 8735 } 8736 else if (ch === '-') { 8737 if (last_to === '-') { 8738 to_chars_expanded.push('-'); 8739 to_chars_expanded.push('-'); 8740 } 8741 else if (i == to_length - 1) { 8742 to_chars_expanded.push('-'); 8743 } 8744 else { 8745 in_range = true; 8746 } 8747 } 8748 else if (in_range) { 8749 start = last_from.charCodeAt(0); 8750 end = ch.charCodeAt(0); 8751 if (start > end) { 8752 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (String.fromCharCode(start)) + "-" + (String.fromCharCode(end)) + "\" in string transliteration"); 8753 } 8754 for (c = start + 1; c < end; c++) { 8755 to_chars_expanded.push(String.fromCharCode(c)); 8756 } 8757 to_chars_expanded.push(ch); 8758 in_range = null; 8759 last_from = null; 8760 } 8761 else { 8762 to_chars_expanded.push(ch); 8763 } 8764 } 8765 8766 to_chars = to_chars_expanded; 8767 to_length = to_chars.length; 8768 } 8769 8770 var length_diff = from_length - to_length; 8771 if (length_diff > 0) { 8772 var pad_char = (to_length > 0 ? to_chars[to_length - 1] : ''); 8773 for (i = 0; i < length_diff; i++) { 8774 to_chars.push(pad_char); 8775 } 8776 } 8777 8778 for (i = 0; i < from_length; i++) { 8779 subs[from_chars[i]] = to_chars[i]; 8780 } 8781 } 8782 var new_str = ''; 8783 var last_substitute = null; 8784 for (i = 0, length = self.length; i < length; i++) { 8785 ch = self.charAt(i); 8786 var sub = subs[ch]; 8787 if (inverse) { 8788 if (sub == null) { 8789 if (last_substitute == null) { 8790 new_str += global_sub; 8791 last_substitute = true; 8792 } 8793 } 8794 else { 8795 new_str += ch; 8796 last_substitute = null; 8797 } 8798 } 8799 else { 8800 if (sub != null) { 8801 if (last_substitute == null || last_substitute !== sub) { 8802 new_str += sub; 8803 last_substitute = sub; 8804 } 8805 } 8806 else { 8807 new_str += ch; 8808 last_substitute = null; 8809 } 8810 } 8811 } 8812 return new_str; 8813 8814 }); 8815 8816 $def(self, '$upcase', function $$upcase() { 8817 var self = this; 8818 8819 return self.toUpperCase(); 8820 }); 8821 8822 $def(self, '$upto', function $$upto(stop, excl) { 8823 var block = $$upto.$$p || nil, self = this; 8824 8825 $$upto.$$p = null; 8826 if (excl == null) excl = false; 8827 if (!(block !== nil)) { 8828 return self.$enum_for("upto", stop, excl) 8829 } 8830 var a, b, s = self.toString(); 8831 8832 stop = $coerce_to(stop, $$$('String'), 'to_str'); 8833 8834 if (s.length === 1 && stop.length === 1) { 8835 8836 a = s.charCodeAt(0); 8837 b = stop.charCodeAt(0); 8838 8839 while (a <= b) { 8840 if (excl && a === b) { 8841 break; 8842 } 8843 8844 block(String.fromCharCode(a)); 8845 8846 a += 1; 8847 } 8848 8849 } else if (parseInt(s, 10).toString() === s && parseInt(stop, 10).toString() === stop) { 8850 8851 a = parseInt(s, 10); 8852 b = parseInt(stop, 10); 8853 8854 while (a <= b) { 8855 if (excl && a === b) { 8856 break; 8857 } 8858 8859 block(a.toString()); 8860 8861 a += 1; 8862 } 8863 8864 } else { 8865 8866 while (s.length <= stop.length && s <= stop) { 8867 if (excl && s === stop) { 8868 break; 8869 } 8870 8871 block(s); 8872 8873 s = (s).$succ(); 8874 } 8875 8876 } 8877 return self; 8878 }, -2); 8879 8880 function char_class_from_char_sets(sets) { 8881 function explode_sequences_in_character_set(set) { 8882 var result = '', 8883 i, len = set.length, 8884 curr_char, 8885 skip_next_dash, 8886 char_code_from, 8887 char_code_upto, 8888 char_code; 8889 for (i = 0; i < len; i++) { 8890 curr_char = set.charAt(i); 8891 if (curr_char === '-' && i > 0 && i < (len - 1) && !skip_next_dash) { 8892 char_code_from = set.charCodeAt(i - 1); 8893 char_code_upto = set.charCodeAt(i + 1); 8894 if (char_code_from > char_code_upto) { 8895 $Kernel.$raise($$$('ArgumentError'), "invalid range \"" + (char_code_from) + "-" + (char_code_upto) + "\" in string transliteration"); 8896 } 8897 for (char_code = char_code_from + 1; char_code < char_code_upto + 1; char_code++) { 8898 result += String.fromCharCode(char_code); 8899 } 8900 skip_next_dash = true; 8901 i++; 8902 } else { 8903 skip_next_dash = (curr_char === '\\'); 8904 result += curr_char; 8905 } 8906 } 8907 return result; 8908 } 8909 8910 function intersection(setA, setB) { 8911 if (setA.length === 0) { 8912 return setB; 8913 } 8914 var result = '', 8915 i, len = setA.length, 8916 chr; 8917 for (i = 0; i < len; i++) { 8918 chr = setA.charAt(i); 8919 if (setB.indexOf(chr) !== -1) { 8920 result += chr; 8921 } 8922 } 8923 return result; 8924 } 8925 8926 var i, len, set, neg, chr, tmp, 8927 pos_intersection = '', 8928 neg_intersection = ''; 8929 8930 for (i = 0, len = sets.length; i < len; i++) { 8931 set = $coerce_to(sets[i], $$$('String'), 'to_str'); 8932 neg = (set.charAt(0) === '^' && set.length > 1); 8933 set = explode_sequences_in_character_set(neg ? set.slice(1) : set); 8934 if (neg) { 8935 neg_intersection = intersection(neg_intersection, set); 8936 } else { 8937 pos_intersection = intersection(pos_intersection, set); 8938 } 8939 } 8940 8941 if (pos_intersection.length > 0 && neg_intersection.length > 0) { 8942 tmp = ''; 8943 for (i = 0, len = pos_intersection.length; i < len; i++) { 8944 chr = pos_intersection.charAt(i); 8945 if (neg_intersection.indexOf(chr) === -1) { 8946 tmp += chr; 8947 } 8948 } 8949 pos_intersection = tmp; 8950 neg_intersection = ''; 8951 } 8952 8953 if (pos_intersection.length > 0) { 8954 return '[' + $$$('Regexp').$escape(pos_intersection) + ']'; 8955 } 8956 8957 if (neg_intersection.length > 0) { 8958 return '[^' + $$$('Regexp').$escape(neg_intersection) + ']'; 8959 } 8960 8961 return null; 8962 } 8963 8964 $def(self, '$instance_variables', function $$instance_variables() { 8965 8966 return [] 8967 }); 8968 $defs(self, '$_load', function $$_load($a) { 8969 var $post_args, args, self = this; 8970 8971 8972 $post_args = $slice(arguments); 8973 args = $post_args; 8974 return $send(self, 'new', $to_a(args)); 8975 }, -1); 8976 8977 $def(self, '$unicode_normalize', function $$unicode_normalize(form) { 8978 var self = this; 8979 8980 8981 if (form == null) form = "nfc"; 8982 if (!$truthy(["nfc", "nfd", "nfkc", "nfkd"]['$include?'](form))) { 8983 $Kernel.$raise($$$('ArgumentError'), "Invalid normalization form " + (form)); 8984 } return self.normalize(form.$upcase()); 8985 }, -1); 8986 8987 $def(self, '$unicode_normalized?', function $String_unicode_normalized$ques$18(form) { 8988 var self = this; 8989 8990 8991 if (form == null) form = "nfc"; 8992 return self.$unicode_normalize(form)['$=='](self); 8993 }, -1); 8994 8995 $def(self, '$unpack', function $$unpack(format) { 8996 8997 return $Kernel.$raise("To use String#unpack, you must first require 'corelib/string/unpack'.") 8998 }); 8999 9000 $def(self, '$unpack1', function $$unpack1(format) { 9001 9002 return $Kernel.$raise("To use String#unpack1, you must first require 'corelib/string/unpack'.") 9003 }); 9004 9005 $def(self, '$freeze', function $$freeze() { 9006 var self = this; 9007 9008 9009 if (typeof self === 'string') { return self; } 9010 $prop(self, "$$frozen", true); 9011 return self; 9012 9013 }); 9014 9015 $def(self, '$-@', function $String_$minus$$19() { 9016 var self = this; 9017 9018 9019 if (typeof self === 'string') return self; 9020 if (self.$$frozen) return self; 9021 if (self.encoding.name == 'UTF-8' && self.internal_encoding.name == 'UTF-8') return self.toString(); 9022 return self.$dup().$freeze(); 9023 9024 }); 9025 9026 $def(self, '$frozen?', function $String_frozen$ques$20() { 9027 var self = this; 9028 9029 return typeof self === 'string' || self.$$frozen === true; 9030 }); 9031 $alias(self, "+@", "dup"); 9032 $alias(self, "===", "=="); 9033 $alias(self, "byteslice", "[]"); 9034 $alias(self, "eql?", "=="); 9035 $alias(self, "equal?", "==="); 9036 $alias(self, "object_id", "__id__"); 9037 $alias(self, "slice", "[]"); 9038 $alias(self, "succ", "next"); 9039 $alias(self, "to_str", "to_s"); 9040 $alias(self, "to_sym", "intern"); 9041 return $Opal.$pristine(self, "initialize"); 9042 })('::', String, $nesting); 9043 return $const_set($nesting[0], 'Symbol', $$('String')); 9044}; 9045 9046Opal.modules["corelib/enumerable"] = function(Opal) {/* Generated by Opal 1.7.3 */ 9047 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.$$$; 9048 9049 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'); 9050 return (function($base, $parent_nesting) { 9051 var self = $module($base, 'Enumerable'); 9052 9053 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 9054 9055 9056 9057 function comparableForPattern(value) { 9058 if (value.length === 0) { 9059 value = [nil]; 9060 } 9061 9062 if (value.length > 1) { 9063 value = [value]; 9064 } 9065 9066 return value; 9067 } 9068 9069 $def(self, '$all?', function $Enumerable_all$ques$1(pattern) {try { var $t_return = $thrower('return'); 9070 var block = $Enumerable_all$ques$1.$$p || nil, self = this; 9071 9072 $Enumerable_all$ques$1.$$p = null; 9073 9074 ; 9075 ; 9076 if ($truthy(pattern !== undefined)) { 9077 $send(self, 'each', [], function $$2($a){var $post_args, value, comparable = nil; 9078 9079 9080 $post_args = $slice(arguments); 9081 value = $post_args; 9082 comparable = comparableForPattern(value); 9083 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 9084 return nil 9085 } else { 9086 $t_return.$throw(false); 9087 };}, {$$arity: -1, $$ret: $t_return}); 9088 } else if ((block !== nil)) { 9089 $send(self, 'each', [], function $$3($a){var $post_args, value; 9090 9091 9092 $post_args = $slice(arguments); 9093 value = $post_args; 9094 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 9095 return nil 9096 } else { 9097 $t_return.$throw(false); 9098 };}, {$$arity: -1, $$ret: $t_return}); 9099 } else { 9100 $send(self, 'each', [], function $$4($a){var $post_args, value; 9101 9102 9103 $post_args = $slice(arguments); 9104 value = $post_args; 9105 if ($truthy($Opal.$destructure(value))) { 9106 return nil 9107 } else { 9108 $t_return.$throw(false); 9109 };}, {$$arity: -1, $$ret: $t_return}); 9110 }; 9111 return true;} catch($e) { 9112 if ($e === $t_return) return $e.$v; 9113 throw $e; 9114 } 9115 }, -1); 9116 9117 $def(self, '$any?', function $Enumerable_any$ques$5(pattern) {try { var $t_return = $thrower('return'); 9118 var block = $Enumerable_any$ques$5.$$p || nil, self = this; 9119 9120 $Enumerable_any$ques$5.$$p = null; 9121 9122 ; 9123 ; 9124 if ($truthy(pattern !== undefined)) { 9125 $send(self, 'each', [], function $$6($a){var $post_args, value, comparable = nil; 9126 9127 9128 $post_args = $slice(arguments); 9129 value = $post_args; 9130 comparable = comparableForPattern(value); 9131 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 9132 $t_return.$throw(true); 9133 } else { 9134 return nil 9135 };}, {$$arity: -1, $$ret: $t_return}); 9136 } else if ((block !== nil)) { 9137 $send(self, 'each', [], function $$7($a){var $post_args, value; 9138 9139 9140 $post_args = $slice(arguments); 9141 value = $post_args; 9142 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 9143 $t_return.$throw(true); 9144 } else { 9145 return nil 9146 };}, {$$arity: -1, $$ret: $t_return}); 9147 } else { 9148 $send(self, 'each', [], function $$8($a){var $post_args, value; 9149 9150 9151 $post_args = $slice(arguments); 9152 value = $post_args; 9153 if ($truthy($Opal.$destructure(value))) { 9154 $t_return.$throw(true); 9155 } else { 9156 return nil 9157 };}, {$$arity: -1, $$ret: $t_return}); 9158 }; 9159 return false;} catch($e) { 9160 if ($e === $t_return) return $e.$v; 9161 throw $e; 9162 } 9163 }, -1); 9164 9165 $def(self, '$chunk', function $$chunk() { 9166 var block = $$chunk.$$p || nil, self = this; 9167 9168 $$chunk.$$p = null; 9169 if (!(block !== nil)) { 9170 return $send(self, 'to_enum', ["chunk"], function $$9(){var self = $$9.$$s == null ? this : $$9.$$s; 9171 9172 return self.$enumerator_size()}, {$$s: self}) 9173 } return $send($$$('Enumerator'), 'new', [], function $$10(yielder){var self = $$10.$$s == null ? this : $$10.$$s; 9174 9175 9176 if (yielder == null) yielder = nil; 9177 9178 var previous = nil, accumulate = []; 9179 9180 function releaseAccumulate() { 9181 if (accumulate.length > 0) { 9182 yielder.$yield(previous, accumulate); 9183 } 9184 } 9185 9186 self.$each.$$p = function(value) { 9187 var key = $yield1(block, value); 9188 9189 if (key === nil) { 9190 releaseAccumulate(); 9191 accumulate = []; 9192 previous = nil; 9193 } else { 9194 if (previous === nil || previous === key) { 9195 accumulate.push(value); 9196 } else { 9197 releaseAccumulate(); 9198 accumulate = [value]; 9199 } 9200 9201 previous = key; 9202 } 9203 }; 9204 9205 self.$each(); 9206 9207 releaseAccumulate(); 9208}, {$$s: self}); 9209 }); 9210 9211 $def(self, '$chunk_while', function $$chunk_while() { 9212 var block = $$chunk_while.$$p || nil, self = this; 9213 9214 $$chunk_while.$$p = null; 9215 if (!(block !== nil)) { 9216 $Kernel.$raise($$$('ArgumentError'), "no block given"); 9217 } return $send(self, 'slice_when', [], function $$11(before, after){ 9218 9219 if (before == null) before = nil; 9220 if (after == null) after = nil; 9221 return Opal.yieldX(block, [before, after])['$!']();}); 9222 }); 9223 9224 $def(self, '$collect', function $$collect() { 9225 var block = $$collect.$$p || nil, self = this; 9226 9227 $$collect.$$p = null; 9228 if (!(block !== nil)) { 9229 return $send(self, 'enum_for', ["collect"], function $$12(){var self = $$12.$$s == null ? this : $$12.$$s; 9230 9231 return self.$enumerator_size()}, {$$s: self}) 9232 } 9233 var result = []; 9234 9235 self.$each.$$p = function() { 9236 var value = $yieldX(block, arguments); 9237 9238 result.push(value); 9239 }; 9240 9241 self.$each(); 9242 9243 return result; 9244 }); 9245 9246 $def(self, '$collect_concat', function $$collect_concat() { 9247 var block = $$collect_concat.$$p || nil, self = this; 9248 9249 $$collect_concat.$$p = null; 9250 if (!(block !== nil)) { 9251 return $send(self, 'enum_for', ["collect_concat"], function $$13(){var self = $$13.$$s == null ? this : $$13.$$s; 9252 9253 return self.$enumerator_size()}, {$$s: self}) 9254 } return $send(self, 'map', [], block.$to_proc()).$flatten(1); 9255 }); 9256 9257 $def(self, '$compact', function $$compact() { 9258 var self = this; 9259 9260 return self.$to_a().$compact() 9261 }); 9262 9263 $def(self, '$count', function $$count(object) { 9264 var block = $$count.$$p || nil, self = this, result = nil; 9265 9266 $$count.$$p = null; 9267 result = 0; 9268 9269 if (object != null && block !== nil) { 9270 self.$warn("warning: given block not used"); 9271 } 9272 if ($truthy(object != null)) { 9273 block = $send($Kernel, 'proc', [], function $$14($a){var $post_args, args; 9274 9275 9276 $post_args = $slice(arguments); 9277 args = $post_args; 9278 return $Opal.$destructure(args)['$=='](object);}, -1); 9279 } else if ($truthy(block['$nil?']())) { 9280 block = $send($Kernel, 'proc', [], $return_val(true)); 9281 } $send(self, 'each', [], function $$15($a){var $post_args, args; 9282 9283 9284 $post_args = $slice(arguments); 9285 args = $post_args; 9286 if ($truthy($yieldX(block, args))) { 9287 return result++; 9288 } else { 9289 return nil 9290 }}, -1); 9291 return result; 9292 }, -1); 9293 9294 $def(self, '$cycle', function $$cycle(n) { 9295 var block = $$cycle.$$p || nil, self = this; 9296 9297 $$cycle.$$p = null; 9298 if (n == null) n = nil; 9299 if (!(block !== nil)) { 9300 return $send(self, 'enum_for', ["cycle", n], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 9301 9302 if ($truthy(n['$nil?']())) { 9303 if ($truthy(self['$respond_to?']("size"))) { 9304 return $$$($$$('Float'), 'INFINITY') 9305 } else { 9306 return nil 9307 } 9308 } else { 9309 9310 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 9311 if ($truthy($rb_gt(n, 0))) { 9312 return $rb_times(self.$enumerator_size(), n) 9313 } else { 9314 return 0 9315 } }}, {$$s: self}) 9316 } if (!$truthy(n['$nil?']())) { 9317 9318 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 9319 if ($truthy(n <= 0)) { 9320 return nil 9321 } } 9322 var all = [], i, length; 9323 9324 self.$each.$$p = function() { 9325 var param = $Opal.$destructure(arguments); 9326 $yield1(block, param); 9327 9328 all.push(param); 9329 }; 9330 9331 self.$each(); 9332 9333 if (all.length === 0) { 9334 return nil; 9335 } 9336 9337 if (n === nil) { 9338 while (true) { 9339 for (i = 0, length = all.length; i < length; i++) { 9340 $yield1(block, all[i]); 9341 } 9342 } 9343 } 9344 else { 9345 while (n > 1) { 9346 for (i = 0, length = all.length; i < length; i++) { 9347 $yield1(block, all[i]); 9348 } 9349 9350 n--; 9351 } 9352 } 9353 }, -1); 9354 9355 $def(self, '$detect', function $$detect(ifnone) {try { var $t_return = $thrower('return'); 9356 var block = $$detect.$$p || nil, self = this; 9357 9358 $$detect.$$p = null; 9359 9360 ; 9361 ; 9362 if (!(block !== nil)) { 9363 return self.$enum_for("detect", ifnone) 9364 }; 9365 $send(self, 'each', [], function $$17($a){var $post_args, args, value = nil; 9366 9367 9368 $post_args = $slice(arguments); 9369 args = $post_args; 9370 value = $Opal.$destructure(args); 9371 if ($truthy(Opal.yield1(block, value))) { 9372 $t_return.$throw(value); 9373 } else { 9374 return nil 9375 };}, {$$arity: -1, $$ret: $t_return}); 9376 9377 if (ifnone !== undefined) { 9378 if (typeof(ifnone) === 'function') { 9379 return ifnone(); 9380 } else { 9381 return ifnone; 9382 } 9383 } 9384 ; 9385 return nil;} catch($e) { 9386 if ($e === $t_return) return $e.$v; 9387 throw $e; 9388 } 9389 }, -1); 9390 9391 $def(self, '$drop', function $$drop(number) { 9392 var self = this; 9393 9394 9395 number = $coerce_to(number, $$$('Integer'), 'to_int'); 9396 if ($truthy(number < 0)) { 9397 $Kernel.$raise($$$('ArgumentError'), "attempt to drop negative size"); 9398 } 9399 var result = [], 9400 current = 0; 9401 9402 self.$each.$$p = function() { 9403 if (number <= current) { 9404 result.push($Opal.$destructure(arguments)); 9405 } 9406 9407 current++; 9408 }; 9409 9410 self.$each(); 9411 9412 return result; 9413 }); 9414 9415 $def(self, '$drop_while', function $$drop_while() { 9416 var block = $$drop_while.$$p || nil, self = this; 9417 9418 $$drop_while.$$p = null; 9419 if (!(block !== nil)) { 9420 return self.$enum_for("drop_while") 9421 } 9422 var result = [], 9423 dropping = true; 9424 9425 self.$each.$$p = function() { 9426 var param = $Opal.$destructure(arguments); 9427 9428 if (dropping) { 9429 var value = $yield1(block, param); 9430 9431 if (!$truthy(value)) { 9432 dropping = false; 9433 result.push(param); 9434 } 9435 } 9436 else { 9437 result.push(param); 9438 } 9439 }; 9440 9441 self.$each(); 9442 9443 return result; 9444 }); 9445 9446 $def(self, '$each_cons', function $$each_cons(n) { 9447 var block = $$each_cons.$$p || nil, self = this; 9448 9449 $$each_cons.$$p = null; 9450 if ($truthy(arguments.length != 1)) { 9451 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 1)"); 9452 } n = $Opal.$try_convert(n, $$$('Integer'), "to_int"); 9453 if ($truthy(n <= 0)) { 9454 $Kernel.$raise($$$('ArgumentError'), "invalid size"); 9455 } if (!(block !== nil)) { 9456 return $send(self, 'enum_for', ["each_cons", n], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s, enum_size = nil; 9457 9458 9459 enum_size = self.$enumerator_size(); 9460 if ($truthy(enum_size['$nil?']())) { 9461 return nil 9462 } else if (($eqeq(enum_size, 0) || ($truthy($rb_lt(enum_size, n))))) { 9463 return 0 9464 } else { 9465 return $rb_plus($rb_minus(enum_size, n), 1) 9466 }}, {$$s: self}) 9467 } 9468 var buffer = []; 9469 9470 self.$each.$$p = function() { 9471 var element = $Opal.$destructure(arguments); 9472 buffer.push(element); 9473 if (buffer.length > n) { 9474 buffer.shift(); 9475 } 9476 if (buffer.length == n) { 9477 $yield1(block, buffer.slice(0, n)); 9478 } 9479 }; 9480 9481 self.$each(); 9482 9483 return self; 9484 }); 9485 9486 $def(self, '$each_entry', function $$each_entry($a) { 9487 var block = $$each_entry.$$p || nil, $post_args, data, self = this; 9488 9489 $$each_entry.$$p = null; 9490 $post_args = $slice(arguments); 9491 data = $post_args; 9492 if (!(block !== nil)) { 9493 return $send(self, 'to_enum', ["each_entry"].concat($to_a(data)), function $$19(){var self = $$19.$$s == null ? this : $$19.$$s; 9494 9495 return self.$enumerator_size()}, {$$s: self}) 9496 } 9497 self.$each.$$p = function() { 9498 var item = $Opal.$destructure(arguments); 9499 9500 $yield1(block, item); 9501 }; 9502 9503 self.$each.apply(self, data); 9504 9505 return self; 9506 }, -1); 9507 9508 $def(self, '$each_slice', function $$each_slice(n) { 9509 var block = $$each_slice.$$p || nil, self = this; 9510 9511 $$each_slice.$$p = null; 9512 n = $coerce_to(n, $$$('Integer'), 'to_int'); 9513 if ($truthy(n <= 0)) { 9514 $Kernel.$raise($$$('ArgumentError'), "invalid slice size"); 9515 } if (!(block !== nil)) { 9516 return $send(self, 'enum_for', ["each_slice", n], function $$20(){var self = $$20.$$s == null ? this : $$20.$$s; 9517 9518 if ($truthy(self['$respond_to?']("size"))) { 9519 return $rb_divide(self.$size(), n).$ceil() 9520 } else { 9521 return nil 9522 }}, {$$s: self}) 9523 } 9524 var slice = []; 9525 9526 self.$each.$$p = function() { 9527 var param = $Opal.$destructure(arguments); 9528 9529 slice.push(param); 9530 9531 if (slice.length === n) { 9532 $yield1(block, slice); 9533 slice = []; 9534 } 9535 }; 9536 9537 self.$each(); 9538 9539 // our "last" group, if smaller than n then won't have been yielded 9540 if (slice.length > 0) { 9541 $yield1(block, slice); 9542 } 9543 return self; 9544 }); 9545 9546 $def(self, '$each_with_index', function $$each_with_index($a) { 9547 var block = $$each_with_index.$$p || nil, $post_args, args, self = this; 9548 9549 $$each_with_index.$$p = null; 9550 $post_args = $slice(arguments); 9551 args = $post_args; 9552 if (!(block !== nil)) { 9553 return $send(self, 'enum_for', ["each_with_index"].concat($to_a(args)), function $$21(){var self = $$21.$$s == null ? this : $$21.$$s; 9554 9555 return self.$enumerator_size()}, {$$s: self}) 9556 } 9557 var index = 0; 9558 9559 self.$each.$$p = function() { 9560 var param = $Opal.$destructure(arguments); 9561 9562 block(param, index); 9563 9564 index++; 9565 }; 9566 9567 self.$each.apply(self, args); 9568 return self; 9569 }, -1); 9570 9571 $def(self, '$each_with_object', function $$each_with_object(object) { 9572 var block = $$each_with_object.$$p || nil, self = this; 9573 9574 $$each_with_object.$$p = null; 9575 if (!(block !== nil)) { 9576 return $send(self, 'enum_for', ["each_with_object", object], function $$22(){var self = $$22.$$s == null ? this : $$22.$$s; 9577 9578 return self.$enumerator_size()}, {$$s: self}) 9579 } 9580 self.$each.$$p = function() { 9581 var param = $Opal.$destructure(arguments); 9582 9583 block(param, object); 9584 }; 9585 9586 self.$each(); 9587 return object; 9588 }); 9589 9590 $def(self, '$entries', function $$entries($a) { 9591 var $post_args, args, self = this; 9592 9593 9594 $post_args = $slice(arguments); 9595 args = $post_args; 9596 9597 var result = []; 9598 9599 self.$each.$$p = function() { 9600 result.push($Opal.$destructure(arguments)); 9601 }; 9602 9603 self.$each.apply(self, args); 9604 9605 return result; 9606 }, -1); 9607 9608 $def(self, '$filter_map', function $$filter_map() { 9609 var block = $$filter_map.$$p || nil, self = this; 9610 9611 $$filter_map.$$p = null; 9612 if (!(block !== nil)) { 9613 return $send(self, 'enum_for', ["filter_map"], function $$23(){var self = $$23.$$s == null ? this : $$23.$$s; 9614 9615 return self.$enumerator_size()}, {$$s: self}) 9616 } return $send($send(self, 'map', [], block.$to_proc()), 'select', [], "itself".$to_proc()); 9617 }); 9618 9619 $def(self, '$find_all', function $$find_all() { 9620 var block = $$find_all.$$p || nil, self = this; 9621 9622 $$find_all.$$p = null; 9623 if (!(block !== nil)) { 9624 return $send(self, 'enum_for', ["find_all"], function $$24(){var self = $$24.$$s == null ? this : $$24.$$s; 9625 9626 return self.$enumerator_size()}, {$$s: self}) 9627 } 9628 var result = []; 9629 9630 self.$each.$$p = function() { 9631 var param = $Opal.$destructure(arguments), 9632 value = $yield1(block, param); 9633 9634 if ($truthy(value)) { 9635 result.push(param); 9636 } 9637 }; 9638 9639 self.$each(); 9640 9641 return result; 9642 }); 9643 9644 $def(self, '$find_index', function $$find_index(object) {try { var $t_return = $thrower('return'); 9645 var block = $$find_index.$$p || nil, self = this, index = nil; 9646 9647 $$find_index.$$p = null; 9648 9649 ; 9650 ; 9651 if ($truthy(object === undefined && block === nil)) { 9652 return self.$enum_for("find_index") 9653 }; 9654 9655 if (object != null && block !== nil) { 9656 self.$warn("warning: given block not used"); 9657 } 9658 ; 9659 index = 0; 9660 if ($truthy(object != null)) { 9661 $send(self, 'each', [], function $$25($a){var $post_args, value; 9662 9663 9664 $post_args = $slice(arguments); 9665 value = $post_args; 9666 if ($eqeq($Opal.$destructure(value), object)) { 9667 $t_return.$throw(index); 9668 }; 9669 return index += 1;;}, {$$arity: -1, $$ret: $t_return}); 9670 } else { 9671 $send(self, 'each', [], function $$26($a){var $post_args, value; 9672 9673 9674 $post_args = $slice(arguments); 9675 value = $post_args; 9676 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 9677 $t_return.$throw(index); 9678 }; 9679 return index += 1;;}, {$$arity: -1, $$ret: $t_return}); 9680 }; 9681 return nil;} catch($e) { 9682 if ($e === $t_return) return $e.$v; 9683 throw $e; 9684 } 9685 }, -1); 9686 9687 $def(self, '$first', function $$first(number) {try { var $t_return = $thrower('return'); 9688 var self = this, result = nil, current = nil; 9689 9690 9691 ; 9692 if ($truthy(number === undefined)) { 9693 return $send(self, 'each', [], function $$27(value){ 9694 9695 if (value == null) value = nil; 9696 $t_return.$throw(value);}, {$$ret: $t_return}) 9697 } else { 9698 9699 result = []; 9700 number = $coerce_to(number, $$$('Integer'), 'to_int'); 9701 if ($truthy(number < 0)) { 9702 $Kernel.$raise($$$('ArgumentError'), "attempt to take negative size"); 9703 }; 9704 if ($truthy(number == 0)) { 9705 return [] 9706 }; 9707 current = 0; 9708 $send(self, 'each', [], function $$28($a){var $post_args, args; 9709 9710 9711 $post_args = $slice(arguments); 9712 args = $post_args; 9713 result.push($Opal.$destructure(args)); 9714 if ($truthy(number <= ++current)) { 9715 $t_return.$throw(result); 9716 } else { 9717 return nil 9718 };}, {$$arity: -1, $$ret: $t_return}); 9719 return result; 9720 };} catch($e) { 9721 if ($e === $t_return) return $e.$v; 9722 throw $e; 9723 } 9724 }, -1); 9725 9726 $def(self, '$grep', function $$grep(pattern) { 9727 var block = $$grep.$$p || nil, self = this, result = nil; 9728 9729 $$grep.$$p = null; 9730 result = []; 9731 $send(self, 'each', [], function $$29($a){var $post_args, value, cmp = nil; 9732 9733 9734 $post_args = $slice(arguments); 9735 value = $post_args; 9736 cmp = comparableForPattern(value); 9737 if (!$truthy($send(pattern, '__send__', ["==="].concat($to_a(cmp))))) { 9738 return nil 9739 } if ((block !== nil)) { 9740 9741 if ($truthy($rb_gt(value.$length(), 1))) { 9742 value = [value]; 9743 } value = Opal.yieldX(block, $to_a(value)); 9744 } else if ($truthy($rb_le(value.$length(), 1))) { 9745 value = value['$[]'](0); 9746 } return result.$push(value);}, -1); 9747 return result; 9748 }); 9749 9750 $def(self, '$grep_v', function $$grep_v(pattern) { 9751 var block = $$grep_v.$$p || nil, self = this, result = nil; 9752 9753 $$grep_v.$$p = null; 9754 result = []; 9755 $send(self, 'each', [], function $$30($a){var $post_args, value, cmp = nil; 9756 9757 9758 $post_args = $slice(arguments); 9759 value = $post_args; 9760 cmp = comparableForPattern(value); 9761 if ($truthy($send(pattern, '__send__', ["==="].concat($to_a(cmp))))) { 9762 return nil 9763 } if ((block !== nil)) { 9764 9765 if ($truthy($rb_gt(value.$length(), 1))) { 9766 value = [value]; 9767 } value = Opal.yieldX(block, $to_a(value)); 9768 } else if ($truthy($rb_le(value.$length(), 1))) { 9769 value = value['$[]'](0); 9770 } return result.$push(value);}, -1); 9771 return result; 9772 }); 9773 9774 $def(self, '$group_by', function $$group_by() { 9775 var block = $$group_by.$$p || nil, $a, self = this, hash = nil, $ret_or_1 = nil; 9776 9777 $$group_by.$$p = null; 9778 if (!(block !== nil)) { 9779 return $send(self, 'enum_for', ["group_by"], function $$31(){var self = $$31.$$s == null ? this : $$31.$$s; 9780 9781 return self.$enumerator_size()}, {$$s: self}) 9782 } hash = $hash2([], {}); 9783 9784 self.$each.$$p = function() { 9785 var param = $Opal.$destructure(arguments), 9786 value = $yield1(block, param); 9787 9788 ($truthy(($ret_or_1 = hash['$[]'](value))) ? ($ret_or_1) : (($a = [value, []], $send(hash, '[]=', $a), $a[$a.length - 1])))['$<<'](param); 9789 }; 9790 9791 self.$each(); 9792 return hash; 9793 }); 9794 9795 $def(self, '$include?', function $Enumerable_include$ques$32(obj) {try { var $t_return = $thrower('return'); 9796 var self = this; 9797 9798 9799 $send(self, 'each', [], function $$33($a){var $post_args, args; 9800 9801 9802 $post_args = $slice(arguments); 9803 args = $post_args; 9804 if ($eqeq($Opal.$destructure(args), obj)) { 9805 $t_return.$throw(true); 9806 } else { 9807 return nil 9808 };}, {$$arity: -1, $$ret: $t_return}); 9809 return false;} catch($e) { 9810 if ($e === $t_return) return $e.$v; 9811 throw $e; 9812 } 9813 }); 9814 9815 $def(self, '$inject', function $$inject(object, sym) { 9816 var block = $$inject.$$p || nil, self = this; 9817 9818 $$inject.$$p = null; 9819 9820 var result = object; 9821 9822 if (block !== nil && sym === undefined) { 9823 self.$each.$$p = function() { 9824 var value = $Opal.$destructure(arguments); 9825 9826 if (result === undefined) { 9827 result = value; 9828 return; 9829 } 9830 9831 value = $yieldX(block, [result, value]); 9832 9833 result = value; 9834 }; 9835 } 9836 else { 9837 if (sym === undefined) { 9838 if (!$$$('Symbol')['$==='](object)) { 9839 $Kernel.$raise($$$('TypeError'), "" + (object.$inspect()) + " is not a Symbol"); 9840 } 9841 9842 sym = object; 9843 result = undefined; 9844 } 9845 9846 self.$each.$$p = function() { 9847 var value = $Opal.$destructure(arguments); 9848 9849 if (result === undefined) { 9850 result = value; 9851 return; 9852 } 9853 9854 result = (result).$__send__(sym, value); 9855 }; 9856 } 9857 9858 self.$each(); 9859 9860 return result == undefined ? nil : result; 9861 }, -1); 9862 9863 $def(self, '$lazy', function $$lazy() { 9864 var self = this; 9865 9866 return $send($$$($$$('Enumerator'), 'Lazy'), 'new', [self, self.$enumerator_size()], function $$34(enum$, $a){var $post_args, args; 9867 9868 9869 if (enum$ == null) enum$ = nil; 9870 $post_args = $slice(arguments, 1); 9871 args = $post_args; 9872 return $send(enum$, 'yield', $to_a(args));}, -2) 9873 }); 9874 9875 $def(self, '$enumerator_size', function $$enumerator_size() { 9876 var self = this; 9877 9878 if ($truthy(self['$respond_to?']("size"))) { 9879 return self.$size() 9880 } else { 9881 return nil 9882 } 9883 }); 9884 9885 $def(self, '$max', function $$max(n) { 9886 var block = $$max.$$p || nil, self = this; 9887 9888 $$max.$$p = null; 9889 9890 if (n === undefined || n === nil) { 9891 var result, value; 9892 9893 self.$each.$$p = function() { 9894 var item = $Opal.$destructure(arguments); 9895 9896 if (result === undefined) { 9897 result = item; 9898 return; 9899 } 9900 9901 if (block !== nil) { 9902 value = $yieldX(block, [item, result]); 9903 } else { 9904 value = (item)['$<=>'](result); 9905 } 9906 9907 if (value === nil) { 9908 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 9909 } 9910 9911 if (value > 0) { 9912 result = item; 9913 } 9914 }; 9915 9916 self.$each(); 9917 9918 if (result === undefined) { 9919 return nil; 9920 } else { 9921 return result; 9922 } 9923 } 9924 9925 n = $coerce_to(n, $$$('Integer'), 'to_int'); 9926 return $send(self, 'sort', [], block.$to_proc()).$reverse().$first(n); 9927 }, -1); 9928 9929 $def(self, '$max_by', function $$max_by(n) { 9930 var block = $$max_by.$$p || nil, self = this; 9931 9932 $$max_by.$$p = null; 9933 if (n == null) n = nil; 9934 if (!$truthy(block)) { 9935 return $send(self, 'enum_for', ["max_by", n], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 9936 9937 return self.$enumerator_size()}, {$$s: self}) 9938 } if (!$truthy(n['$nil?']())) { 9939 return $send(self, 'sort_by', [], block.$to_proc()).$reverse().$take(n) 9940 } 9941 var result, 9942 by; 9943 9944 self.$each.$$p = function() { 9945 var param = $Opal.$destructure(arguments), 9946 value = $yield1(block, param); 9947 9948 if (result === undefined) { 9949 result = param; 9950 by = value; 9951 return; 9952 } 9953 9954 if ((value)['$<=>'](by) > 0) { 9955 result = param; 9956 by = value; 9957 } 9958 }; 9959 9960 self.$each(); 9961 9962 return result === undefined ? nil : result; 9963 }, -1); 9964 9965 $def(self, '$min', function $$min(n) { 9966 var block = $$min.$$p || nil, self = this; 9967 9968 $$min.$$p = null; 9969 if (n == null) n = nil; 9970 if (!$truthy(n['$nil?']())) { 9971 if ((block !== nil)) { 9972 return $send(self, 'sort', [], function $$36(a, b){ 9973 9974 if (a == null) a = nil; 9975 if (b == null) b = nil; 9976 return Opal.yieldX(block, [a, b]);}).$take(n) 9977 } else { 9978 return self.$sort().$take(n) 9979 } 9980 } 9981 var result; 9982 9983 if (block !== nil) { 9984 self.$each.$$p = function() { 9985 var param = $Opal.$destructure(arguments); 9986 9987 if (result === undefined) { 9988 result = param; 9989 return; 9990 } 9991 9992 var value = block(param, result); 9993 9994 if (value === nil) { 9995 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 9996 } 9997 9998 if (value < 0) { 9999 result = param; 10000 } 10001 }; 10002 } 10003 else { 10004 self.$each.$$p = function() { 10005 var param = $Opal.$destructure(arguments); 10006 10007 if (result === undefined) { 10008 result = param; 10009 return; 10010 } 10011 10012 if ($Opal.$compare(param, result) < 0) { 10013 result = param; 10014 } 10015 }; 10016 } 10017 10018 self.$each(); 10019 10020 return result === undefined ? nil : result; 10021 }, -1); 10022 10023 $def(self, '$min_by', function $$min_by(n) { 10024 var block = $$min_by.$$p || nil, self = this; 10025 10026 $$min_by.$$p = null; 10027 if (n == null) n = nil; 10028 if (!$truthy(block)) { 10029 return $send(self, 'enum_for', ["min_by", n], function $$37(){var self = $$37.$$s == null ? this : $$37.$$s; 10030 10031 return self.$enumerator_size()}, {$$s: self}) 10032 } if (!$truthy(n['$nil?']())) { 10033 return $send(self, 'sort_by', [], block.$to_proc()).$take(n) 10034 } 10035 var result, 10036 by; 10037 10038 self.$each.$$p = function() { 10039 var param = $Opal.$destructure(arguments), 10040 value = $yield1(block, param); 10041 10042 if (result === undefined) { 10043 result = param; 10044 by = value; 10045 return; 10046 } 10047 10048 if ((value)['$<=>'](by) < 0) { 10049 result = param; 10050 by = value; 10051 } 10052 }; 10053 10054 self.$each(); 10055 10056 return result === undefined ? nil : result; 10057 }, -1); 10058 10059 $def(self, '$minmax', function $$minmax() { 10060 var block = $$minmax.$$p || nil, self = this, $ret_or_1 = nil; 10061 10062 $$minmax.$$p = null; 10063 block = ($truthy(($ret_or_1 = block)) ? ($ret_or_1) : ($send($Kernel, 'proc', [], function $$38(a, b){ 10064 10065 if (a == null) a = nil; 10066 if (b == null) b = nil; 10067 return a['$<=>'](b);}))); 10068 10069 var min = nil, max = nil, first_time = true; 10070 10071 self.$each.$$p = function() { 10072 var element = $Opal.$destructure(arguments); 10073 if (first_time) { 10074 min = max = element; 10075 first_time = false; 10076 } else { 10077 var min_cmp = block.$call(min, element); 10078 10079 if (min_cmp === nil) { 10080 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 10081 } else if (min_cmp > 0) { 10082 min = element; 10083 } 10084 10085 var max_cmp = block.$call(max, element); 10086 10087 if (max_cmp === nil) { 10088 $Kernel.$raise($$$('ArgumentError'), "comparison failed"); 10089 } else if (max_cmp < 0) { 10090 max = element; 10091 } 10092 } 10093 }; 10094 10095 self.$each(); 10096 10097 return [min, max]; 10098 }); 10099 10100 $def(self, '$minmax_by', function $$minmax_by() { 10101 var block = $$minmax_by.$$p || nil, self = this; 10102 10103 $$minmax_by.$$p = null; 10104 if (!$truthy(block)) { 10105 return $send(self, 'enum_for', ["minmax_by"], function $$39(){var self = $$39.$$s == null ? this : $$39.$$s; 10106 10107 return self.$enumerator_size()}, {$$s: self}) 10108 } 10109 var min_result = nil, 10110 max_result = nil, 10111 min_by, 10112 max_by; 10113 10114 self.$each.$$p = function() { 10115 var param = $Opal.$destructure(arguments), 10116 value = $yield1(block, param); 10117 10118 if ((min_by === undefined) || (value)['$<=>'](min_by) < 0) { 10119 min_result = param; 10120 min_by = value; 10121 } 10122 10123 if ((max_by === undefined) || (value)['$<=>'](max_by) > 0) { 10124 max_result = param; 10125 max_by = value; 10126 } 10127 }; 10128 10129 self.$each(); 10130 10131 return [min_result, max_result]; 10132 }); 10133 10134 $def(self, '$none?', function $Enumerable_none$ques$40(pattern) {try { var $t_return = $thrower('return'); 10135 var block = $Enumerable_none$ques$40.$$p || nil, self = this; 10136 10137 $Enumerable_none$ques$40.$$p = null; 10138 10139 ; 10140 ; 10141 if ($truthy(pattern !== undefined)) { 10142 $send(self, 'each', [], function $$41($a){var $post_args, value, comparable = nil; 10143 10144 10145 $post_args = $slice(arguments); 10146 value = $post_args; 10147 comparable = comparableForPattern(value); 10148 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 10149 $t_return.$throw(false); 10150 } else { 10151 return nil 10152 };}, {$$arity: -1, $$ret: $t_return}); 10153 } else if ((block !== nil)) { 10154 $send(self, 'each', [], function $$42($a){var $post_args, value; 10155 10156 10157 $post_args = $slice(arguments); 10158 value = $post_args; 10159 if ($truthy(Opal.yieldX(block, $to_a(value)))) { 10160 $t_return.$throw(false); 10161 } else { 10162 return nil 10163 };}, {$$arity: -1, $$ret: $t_return}); 10164 } else { 10165 $send(self, 'each', [], function $$43($a){var $post_args, value, item = nil; 10166 10167 10168 $post_args = $slice(arguments); 10169 value = $post_args; 10170 item = $Opal.$destructure(value); 10171 if ($truthy(item)) { 10172 $t_return.$throw(false); 10173 } else { 10174 return nil 10175 };}, {$$arity: -1, $$ret: $t_return}); 10176 }; 10177 return true;} catch($e) { 10178 if ($e === $t_return) return $e.$v; 10179 throw $e; 10180 } 10181 }, -1); 10182 10183 $def(self, '$one?', function $Enumerable_one$ques$44(pattern) {try { var $t_return = $thrower('return'); 10184 var block = $Enumerable_one$ques$44.$$p || nil, self = this, count = nil; 10185 10186 $Enumerable_one$ques$44.$$p = null; 10187 10188 ; 10189 ; 10190 count = 0; 10191 if ($truthy(pattern !== undefined)) { 10192 $send(self, 'each', [], function $$45($a){var $post_args, value, comparable = nil; 10193 10194 10195 $post_args = $slice(arguments); 10196 value = $post_args; 10197 comparable = comparableForPattern(value); 10198 if ($truthy($send(pattern, 'public_send', ["==="].concat($to_a(comparable))))) { 10199 10200 count = $rb_plus(count, 1); 10201 if ($truthy($rb_gt(count, 1))) { 10202 $t_return.$throw(false); 10203 } else { 10204 return nil 10205 }; 10206 } else { 10207 return nil 10208 };}, {$$arity: -1, $$ret: $t_return}); 10209 } else if ((block !== nil)) { 10210 $send(self, 'each', [], function $$46($a){var $post_args, value; 10211 10212 10213 $post_args = $slice(arguments); 10214 value = $post_args; 10215 if (!$truthy(Opal.yieldX(block, $to_a(value)))) { 10216 return nil 10217 }; 10218 count = $rb_plus(count, 1); 10219 if ($truthy($rb_gt(count, 1))) { 10220 $t_return.$throw(false); 10221 } else { 10222 return nil 10223 };}, {$$arity: -1, $$ret: $t_return}); 10224 } else { 10225 $send(self, 'each', [], function $$47($a){var $post_args, value; 10226 10227 10228 $post_args = $slice(arguments); 10229 value = $post_args; 10230 if (!$truthy($Opal.$destructure(value))) { 10231 return nil 10232 }; 10233 count = $rb_plus(count, 1); 10234 if ($truthy($rb_gt(count, 1))) { 10235 $t_return.$throw(false); 10236 } else { 10237 return nil 10238 };}, {$$arity: -1, $$ret: $t_return}); 10239 }; 10240 return count['$=='](1);} catch($e) { 10241 if ($e === $t_return) return $e.$v; 10242 throw $e; 10243 } 10244 }, -1); 10245 10246 $def(self, '$partition', function $$partition() { 10247 var block = $$partition.$$p || nil, self = this; 10248 10249 $$partition.$$p = null; 10250 if (!(block !== nil)) { 10251 return $send(self, 'enum_for', ["partition"], function $$48(){var self = $$48.$$s == null ? this : $$48.$$s; 10252 10253 return self.$enumerator_size()}, {$$s: self}) 10254 } 10255 var truthy = [], falsy = []; 10256 10257 self.$each.$$p = function() { 10258 var param = $Opal.$destructure(arguments), 10259 value = $yield1(block, param); 10260 10261 if ($truthy(value)) { 10262 truthy.push(param); 10263 } 10264 else { 10265 falsy.push(param); 10266 } 10267 }; 10268 10269 self.$each(); 10270 10271 return [truthy, falsy]; 10272 }); 10273 10274 $def(self, '$reject', function $$reject() { 10275 var block = $$reject.$$p || nil, self = this; 10276 10277 $$reject.$$p = null; 10278 if (!(block !== nil)) { 10279 return $send(self, 'enum_for', ["reject"], function $$49(){var self = $$49.$$s == null ? this : $$49.$$s; 10280 10281 return self.$enumerator_size()}, {$$s: self}) 10282 } 10283 var result = []; 10284 10285 self.$each.$$p = function() { 10286 var param = $Opal.$destructure(arguments), 10287 value = $yield1(block, param); 10288 10289 if (!$truthy(value)) { 10290 result.push(param); 10291 } 10292 }; 10293 10294 self.$each(); 10295 10296 return result; 10297 }); 10298 10299 $def(self, '$reverse_each', function $$reverse_each() { 10300 var block = $$reverse_each.$$p || nil, self = this; 10301 10302 $$reverse_each.$$p = null; 10303 if (!(block !== nil)) { 10304 return $send(self, 'enum_for', ["reverse_each"], function $$50(){var self = $$50.$$s == null ? this : $$50.$$s; 10305 10306 return self.$enumerator_size()}, {$$s: self}) 10307 } 10308 var result = []; 10309 10310 self.$each.$$p = function() { 10311 result.push(arguments); 10312 }; 10313 10314 self.$each(); 10315 10316 for (var i = result.length - 1; i >= 0; i--) { 10317 $yieldX(block, result[i]); 10318 } 10319 10320 return result; 10321 }); 10322 10323 $def(self, '$slice_before', function $$slice_before(pattern) { 10324 var block = $$slice_before.$$p || nil, self = this; 10325 10326 $$slice_before.$$p = null; 10327 if ($truthy(pattern === undefined && block === nil)) { 10328 $Kernel.$raise($$$('ArgumentError'), "both pattern and block are given"); 10329 } if ($truthy(pattern !== undefined && block !== nil || arguments.length > 1)) { 10330 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " expected 1)"); 10331 } return $send($$$('Enumerator'), 'new', [], function $$51(e){var self = $$51.$$s == null ? this : $$51.$$s; 10332 10333 10334 if (e == null) e = nil; 10335 10336 var slice = []; 10337 10338 if (block !== nil) { 10339 if (pattern === undefined) { 10340 self.$each.$$p = function() { 10341 var param = $Opal.$destructure(arguments), 10342 value = $yield1(block, param); 10343 10344 if ($truthy(value) && slice.length > 0) { 10345 e['$<<'](slice); 10346 slice = []; 10347 } 10348 10349 slice.push(param); 10350 }; 10351 } 10352 else { 10353 self.$each.$$p = function() { 10354 var param = $Opal.$destructure(arguments), 10355 value = block(param, pattern.$dup()); 10356 10357 if ($truthy(value) && slice.length > 0) { 10358 e['$<<'](slice); 10359 slice = []; 10360 } 10361 10362 slice.push(param); 10363 }; 10364 } 10365 } 10366 else { 10367 self.$each.$$p = function() { 10368 var param = $Opal.$destructure(arguments), 10369 value = pattern['$==='](param); 10370 10371 if ($truthy(value) && slice.length > 0) { 10372 e['$<<'](slice); 10373 slice = []; 10374 } 10375 10376 slice.push(param); 10377 }; 10378 } 10379 10380 self.$each(); 10381 10382 if (slice.length > 0) { 10383 e['$<<'](slice); 10384 } 10385}, {$$s: self}); 10386 }, -1); 10387 10388 $def(self, '$slice_after', function $$slice_after(pattern) { 10389 var block = $$slice_after.$$p || nil, self = this; 10390 10391 $$slice_after.$$p = null; 10392 if ($truthy(pattern === undefined && block === nil)) { 10393 $Kernel.$raise($$$('ArgumentError'), "both pattern and block are given"); 10394 } if ($truthy(pattern !== undefined && block !== nil || arguments.length > 1)) { 10395 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " expected 1)"); 10396 } if ($truthy(pattern !== undefined)) { 10397 block = $send($Kernel, 'proc', [], function $$52(e){ 10398 10399 if (e == null) e = nil; 10400 return pattern['$==='](e);}); 10401 } return $send($$$('Enumerator'), 'new', [], function $$53(yielder){var self = $$53.$$s == null ? this : $$53.$$s; 10402 10403 10404 if (yielder == null) yielder = nil; 10405 10406 var accumulate; 10407 10408 self.$each.$$p = function() { 10409 var element = $Opal.$destructure(arguments), 10410 end_chunk = $yield1(block, element); 10411 10412 if (accumulate == null) { 10413 accumulate = []; 10414 } 10415 10416 if ($truthy(end_chunk)) { 10417 accumulate.push(element); 10418 yielder.$yield(accumulate); 10419 accumulate = null; 10420 } else { 10421 accumulate.push(element); 10422 } 10423 }; 10424 10425 self.$each(); 10426 10427 if (accumulate != null) { 10428 yielder.$yield(accumulate); 10429 } 10430}, {$$s: self}); 10431 }, -1); 10432 10433 $def(self, '$slice_when', function $$slice_when() { 10434 var block = $$slice_when.$$p || nil, self = this; 10435 10436 $$slice_when.$$p = null; 10437 if (!(block !== nil)) { 10438 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1)"); 10439 } return $send($$$('Enumerator'), 'new', [], function $$54(yielder){var self = $$54.$$s == null ? this : $$54.$$s; 10440 10441 10442 if (yielder == null) yielder = nil; 10443 10444 var slice = nil, last_after = nil; 10445 10446 self.$each_cons.$$p = function() { 10447 var params = $Opal.$destructure(arguments), 10448 before = params[0], 10449 after = params[1], 10450 match = $yieldX(block, [before, after]); 10451 10452 last_after = after; 10453 10454 if (slice === nil) { 10455 slice = []; 10456 } 10457 10458 if ($truthy(match)) { 10459 slice.push(before); 10460 yielder.$yield(slice); 10461 slice = []; 10462 } else { 10463 slice.push(before); 10464 } 10465 }; 10466 10467 self.$each_cons(2); 10468 10469 if (slice !== nil) { 10470 slice.push(last_after); 10471 yielder.$yield(slice); 10472 } 10473}, {$$s: self}); 10474 }); 10475 10476 $def(self, '$sort', function $$sort() { 10477 var block = $$sort.$$p || nil, self = this, ary = nil; 10478 10479 $$sort.$$p = null; 10480 ary = self.$to_a(); 10481 if (!(block !== nil)) { 10482 block = $lambda(function $$55(a, b){ 10483 10484 if (a == null) a = nil; 10485 if (b == null) b = nil; 10486 return a['$<=>'](b);}); 10487 } return $send(ary, 'sort', [], block.$to_proc()); 10488 }); 10489 10490 $def(self, '$sort_by', function $$sort_by() { 10491 var block = $$sort_by.$$p || nil, self = this, dup = nil; 10492 10493 $$sort_by.$$p = null; 10494 if (!(block !== nil)) { 10495 return $send(self, 'enum_for', ["sort_by"], function $$56(){var self = $$56.$$s == null ? this : $$56.$$s; 10496 10497 return self.$enumerator_size()}, {$$s: self}) 10498 } dup = $send(self, 'map', [], function $$57(){var arg = nil; 10499 10500 10501 arg = $Opal.$destructure(arguments); 10502 return [Opal.yield1(block, arg), arg];}); 10503 $send(dup, 'sort!', [], function $$58(a, b){ 10504 10505 if (a == null) a = nil; 10506 if (b == null) b = nil; 10507 return (a[0])['$<=>'](b[0]);}); 10508 return $send(dup, 'map!', [], function $$59(i){ 10509 10510 if (i == null) i = nil; 10511 return i[1];}); 10512 }); 10513 10514 $def(self, '$sum', function $$sum(initial) { 10515 var $yield = $$sum.$$p || nil, self = this, result = nil, compensation = nil; 10516 10517 $$sum.$$p = null; 10518 10519 if (initial == null) initial = 0; 10520 result = initial; 10521 compensation = 0; 10522 $send(self, 'each', [], function $$60($a){var $post_args, args, item = nil, y = nil, t = nil; 10523 10524 10525 $post_args = $slice(arguments); 10526 args = $post_args; 10527 item = (($yield !== nil) ? (Opal.yieldX($yield, $to_a(args))) : ($Opal.$destructure(args))); 10528 if (($not([$$$($$$('Float'), 'INFINITY'), $$$($$$('Float'), 'INFINITY')['$-@']()]['$include?'](item)) && ($truthy(item['$respond_to?']("-"))))) { 10529 10530 y = $rb_minus(item, compensation); 10531 t = $rb_plus(result, y); 10532 compensation = $rb_minus($rb_minus(t, result), y); 10533 return (result = t); 10534 } else { 10535 return (result = $rb_plus(result, item)) 10536 }}, -1); 10537 return result; 10538 }, -1); 10539 10540 $def(self, '$take', function $$take(num) { 10541 var self = this; 10542 10543 return self.$first(num) 10544 }); 10545 10546 $def(self, '$take_while', function $$take_while() {try { var $t_return = $thrower('return'); 10547 var block = $$take_while.$$p || nil, self = this, result = nil; 10548 10549 $$take_while.$$p = null; 10550 10551 ; 10552 if (!$truthy(block)) { 10553 return self.$enum_for("take_while") 10554 }; 10555 result = []; 10556 return $send(self, 'each', [], function $$61($a){var $post_args, args, value = nil; 10557 10558 10559 $post_args = $slice(arguments); 10560 args = $post_args; 10561 value = $Opal.$destructure(args); 10562 if (!$truthy(Opal.yield1(block, value))) { 10563 $t_return.$throw(result); 10564 }; 10565 return result.push(value);;}, {$$arity: -1, $$ret: $t_return});} catch($e) { 10566 if ($e === $t_return) return $e.$v; 10567 throw $e; 10568 } 10569 }); 10570 10571 $def(self, '$uniq', function $$uniq() { 10572 var block = $$uniq.$$p || nil, self = this, hash = nil; 10573 10574 $$uniq.$$p = null; 10575 hash = $hash2([], {}); 10576 $send(self, 'each', [], function $$62($a){var $post_args, args, $b, value = nil, produced = nil; 10577 10578 10579 $post_args = $slice(arguments); 10580 args = $post_args; 10581 value = $Opal.$destructure(args); 10582 produced = ((block !== nil) ? (Opal.yield1(block, value)) : (value)); 10583 if ($truthy(hash['$key?'](produced))) { 10584 return nil 10585 } else { 10586 return ($b = [produced, value], $send(hash, '[]=', $b), $b[$b.length - 1]) 10587 }}, -1); 10588 return hash.$values(); 10589 }); 10590 10591 $def(self, '$tally', function $$tally(hash) { 10592 var self = this, out = nil; 10593 if (hash && hash !== nil) { $deny_frozen_access(hash); } out = $send($send(self, 'group_by', [], "itself".$to_proc()), 'transform_values', [], "count".$to_proc()); 10594 if ($truthy(hash)) { 10595 10596 $send(out, 'each', [], function $$63(k, v){var $a; 10597 10598 10599 if (k == null) k = nil; 10600 if (v == null) v = nil; 10601 return ($a = [k, $rb_plus(hash.$fetch(k, 0), v)], $send(hash, '[]=', $a), $a[$a.length - 1]);}); 10602 return hash; 10603 } else { 10604 return out 10605 } }, -1); 10606 10607 $def(self, '$to_h', function $$to_h($a) { 10608 var block = $$to_h.$$p || nil, $post_args, args, self = this; 10609 10610 $$to_h.$$p = null; 10611 $post_args = $slice(arguments); 10612 args = $post_args; 10613 if ((block !== nil)) { 10614 return $send($send(self, 'map', [], block.$to_proc()), 'to_h', $to_a(args)) 10615 } 10616 var hash = $hash2([], {}); 10617 10618 self.$each.$$p = function() { 10619 var param = $Opal.$destructure(arguments); 10620 var ary = $Opal['$coerce_to?'](param, $$$('Array'), "to_ary"), key, val; 10621 if (!ary.$$is_array) { 10622 $Kernel.$raise($$$('TypeError'), "wrong element type " + ((ary).$class()) + " (expected array)"); 10623 } 10624 if (ary.length !== 2) { 10625 $Kernel.$raise($$$('ArgumentError'), "wrong array length (expected 2, was " + ((ary).$length()) + ")"); 10626 } 10627 key = ary[0]; 10628 val = ary[1]; 10629 10630 Opal.hash_put(hash, key, val); 10631 }; 10632 10633 self.$each.apply(self, args); 10634 10635 return hash; 10636 }, -1); 10637 10638 $def(self, '$to_set', function $$to_set($a, $b) { 10639 var block = $$to_set.$$p || nil, $post_args, klass, args, self = this; 10640 10641 $$to_set.$$p = null; 10642 $post_args = $slice(arguments); 10643 10644 if ($post_args.length > 0) klass = $post_args.shift();if (klass == null) klass = $$('Set'); 10645 args = $post_args; 10646 return $send(klass, 'new', [self].concat($to_a(args)), block.$to_proc()); 10647 }, -1); 10648 10649 $def(self, '$zip', function $$zip($a) { 10650 var $post_args, others, self = this; 10651 10652 $$zip.$$p = null; 10653 $post_args = $slice(arguments); 10654 others = $post_args; 10655 return $send(self.$to_a(), 'zip', $to_a(others)); 10656 }, -1); 10657 $alias(self, "find", "detect"); 10658 $alias(self, "filter", "find_all"); 10659 $alias(self, "flat_map", "collect_concat"); 10660 $alias(self, "map", "collect"); 10661 $alias(self, "member?", "include?"); 10662 $alias(self, "reduce", "inject"); 10663 $alias(self, "select", "find_all"); 10664 return $alias(self, "to_a", "entries"); 10665 })('::', $nesting) 10666}; 10667 10668Opal.modules["corelib/enumerator/arithmetic_sequence"] = function(Opal) {/* Generated by Opal 1.7.3 */ 10669 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.$$$; 10670 10671 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'); 10672 return (function($base, $super, $parent_nesting) { 10673 var self = $klass($base, $super, 'Enumerator'); 10674 10675 var $nesting = [self].concat($parent_nesting); 10676 10677 return (function($base, $super, $parent_nesting) { 10678 var self = $klass($base, $super, 'ArithmeticSequence'); 10679 10680 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 10681 10682 $proto.step_arg2 = $proto.receiver_num = $proto.step_arg1 = $proto.step = $proto.range = $proto.topfx = $proto.bypfx = $proto.creation_method = $proto.skipped_arg = nil; 10683 10684 Opal.prop(self.$$prototype, '$$is_arithmetic_seq', true); 10685 var inf = Infinity; 10686 10687 $def(self, '$initialize', function $$initialize(range, step, creation_method) { 10688 var $a, self = this, $ret_or_1 = nil; 10689 if (creation_method == null) creation_method = "step"; 10690 self.creation_method = creation_method; 10691 if ($truthy(range['$is_a?']($$$('Array')))) { 10692 10693 $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])); 10694 self.receiver_num = step; 10695 self.step = 1; 10696 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)))); 10697 } else { 10698 10699 if (!$truthy(step)) { 10700 self.skipped_arg = true; 10701 } $a = [range, ($truthy(($ret_or_1 = step)) ? ($ret_or_1) : (1))], (self.range = $a[0]), (self.step = $a[1]); 10702 } self.object = self; 10703 if ($eqeq(self.step, 0)) { 10704 $Kernel.$raise($$('ArgumentError'), "step can't be 0"); 10705 } if ($truthy(self.step['$respond_to?']("to_int"))) { 10706 return nil 10707 } else { 10708 return $Kernel.$raise($$('ArgumentError'), "" + ("no implicit conversion of " + (self.step.$class()) + " ") + "into Integer") 10709 } }, -2); 10710 self.$attr_reader("step"); 10711 10712 $def(self, '$begin', function $$begin() { 10713 var self = this; 10714 10715 return self.range.$begin() 10716 }); 10717 10718 $def(self, '$end', function $$end() { 10719 var self = this; 10720 10721 return self.range.$end() 10722 }); 10723 10724 $def(self, '$exclude_end?', function $ArithmeticSequence_exclude_end$ques$1() { 10725 var self = this; 10726 10727 return self.range['$exclude_end?']() 10728 }); 10729 10730 $def(self, '$_lesser_than_end?', function $ArithmeticSequence__lesser_than_end$ques$2(val) { 10731 var self = this, end_ = nil, $ret_or_1 = nil; 10732 10733 10734 end_ = ($truthy(($ret_or_1 = self.$end())) ? ($ret_or_1) : (inf)); 10735 if ($truthy($rb_gt(self.$step(), 0))) { 10736 if ($truthy(self['$exclude_end?']())) { 10737 return $rb_lt(val, end_) 10738 } else { 10739 return $rb_le(val, end_) 10740 } 10741 } else if ($truthy(self['$exclude_end?']())) { 10742 return $rb_gt(val, end_) 10743 } else { 10744 return $rb_ge(val, end_) 10745 } }); 10746 10747 $def(self, '$_greater_than_begin?', function $ArithmeticSequence__greater_than_begin$ques$3(val) { 10748 var self = this, begin_ = nil, $ret_or_1 = nil; 10749 10750 10751 begin_ = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 10752 if ($truthy($rb_gt(self.$step(), 0))) { 10753 return $rb_gt(val, begin_) 10754 } else { 10755 return $rb_lt(val, begin_) 10756 } }); 10757 10758 $def(self, '$first', function $$first(count) { 10759 var self = this, iter = nil, $ret_or_1 = nil, out = nil; 10760 iter = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 10761 if (!$truthy(count)) { 10762 return ($truthy(self['$_lesser_than_end?'](iter)) ? (iter) : (nil)) 10763 } out = []; 10764 while ($truthy(($truthy(($ret_or_1 = self['$_lesser_than_end?'](iter))) ? ($rb_gt(count, 0)) : ($ret_or_1)))) { 10765 10766 out['$<<'](iter); 10767 iter = $rb_plus(iter, self.$step()); 10768 count = $rb_minus(count, 1); 10769 } return out; 10770 }, -1); 10771 10772 $def(self, '$each', function $$each() { 10773 var block = $$each.$$p || nil, self = this, $ret_or_1 = nil, iter = nil; 10774 10775 $$each.$$p = null; 10776 if (!(block !== nil)) { 10777 return self 10778 } if ($eqeqeq(nil, ($ret_or_1 = self.$begin()))) { 10779 $Kernel.$raise($$('TypeError'), "nil can't be coerced into Integer"); 10780 } iter = ($truthy(($ret_or_1 = self.$begin())) ? ($ret_or_1) : ((inf)['$-@']())); 10781 while ($truthy(self['$_lesser_than_end?'](iter))) { 10782 10783 Opal.yield1(block, iter); 10784 iter = $rb_plus(iter, self.$step()); 10785 } return self; 10786 }); 10787 10788 $def(self, '$last', function $$last(count) { 10789 var self = this, $ret_or_1 = nil, iter = nil, out = nil; 10790 if (($eqeqeq(inf, ($ret_or_1 = self.$end())) || ($eqeqeq((inf)['$-@'](), $ret_or_1)))) { 10791 $Kernel.$raise($$$('FloatDomainError'), self.$end()); 10792 } else if ($eqeqeq(nil, $ret_or_1)) { 10793 $Kernel.$raise($$$('RangeError'), "cannot get the last element of endless arithmetic sequence"); 10794 } else ; iter = $rb_minus(self.$end(), $rb_minus(self.$end(), self.$begin())['$%'](self.$step())); 10795 if (!$truthy(self['$_lesser_than_end?'](iter))) { 10796 iter = $rb_minus(iter, self.$step()); 10797 } if (!$truthy(count)) { 10798 return ($truthy(self['$_greater_than_begin?'](iter)) ? (iter) : (nil)) 10799 } out = []; 10800 while ($truthy(($truthy(($ret_or_1 = self['$_greater_than_begin?'](iter))) ? ($rb_gt(count, 0)) : ($ret_or_1)))) { 10801 10802 out['$<<'](iter); 10803 iter = $rb_minus(iter, self.$step()); 10804 count = $rb_minus(count, 1); 10805 } return out.$reverse(); 10806 }, -1); 10807 10808 $def(self, '$size', function $$size() { 10809 var self = this, step_sign = nil, iter = nil; 10810 10811 10812 step_sign = ($truthy($rb_gt(self.$step(), 0)) ? (1) : (-1)); 10813 if ($not(self['$_lesser_than_end?'](self.$begin()))) { 10814 return 0 10815 } else if ($truthy([(inf)['$-@'](), inf]['$include?'](self.$step()))) { 10816 return 1 10817 } else if (($truthy([$rb_times((inf)['$-@'](), step_sign), nil]['$include?'](self.$begin())) || ($truthy([$rb_times(inf, step_sign), nil]['$include?'](self.$end()))))) { 10818 return inf; 10819 } else { 10820 10821 iter = $rb_minus(self.$end(), $rb_minus(self.$end(), self.$begin())['$%'](self.$step())); 10822 if (!$truthy(self['$_lesser_than_end?'](iter))) { 10823 iter = $rb_minus(iter, self.$step()); 10824 } return $rb_plus($rb_divide($rb_minus(iter, self.$begin()), self.$step()).$abs().$to_i(), 1); 10825 } }); 10826 10827 $def(self, '$==', function $ArithmeticSequence_$eq_eq$4(other) { 10828 var self = this, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, $ret_or_4 = nil; 10829 10830 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))))) { 10831 return self['$exclude_end?']()['$=='](other['$exclude_end?']()) 10832 } else { 10833 return $ret_or_1 10834 } 10835 }); 10836 10837 $def(self, '$hash', function $$hash() { 10838 var self = this; 10839 10840 return [self.$begin(), self.$end(), self.$step(), self['$exclude_end?']()].$hash() 10841 }); 10842 10843 $def(self, '$inspect', function $$inspect() { 10844 var self = this, args = nil; 10845 10846 if ($truthy(self.receiver_num)) { 10847 10848 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)); 10849 return "(" + (self.receiver_num.$inspect()) + "." + (self.creation_method) + (args) + ")"; 10850 } else { 10851 10852 args = ($truthy(self.skipped_arg) ? (nil) : ("(" + (self.step) + ")")); 10853 return "((" + (self.range.$inspect()) + ")." + (self.creation_method) + (args) + ")"; 10854 } 10855 }); 10856 $alias(self, "===", "=="); 10857 return $alias(self, "eql?", "=="); 10858 })(self, self, $nesting) 10859 })('::', null, $nesting) 10860}; 10861 10862Opal.modules["corelib/enumerator/chain"] = function(Opal) {/* Generated by Opal 1.7.3 */ 10863 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.$$$; 10864 10865 Opal.add_stubs('to_enum,size,each,<<,to_proc,include?,+,reverse_each,respond_to?,rewind,inspect'); 10866 return (function($base, $super) { 10867 var self = $klass($base, $super, 'Enumerator'); 10868 10869 10870 return (function($base, $super) { 10871 var self = $klass($base, $super, 'Chain'); 10872 10873 var $proto = self.$$prototype; 10874 10875 $proto.enums = $proto.iterated = nil; 10876 10877 10878 $def(self, '$initialize', function $$initialize($a) { 10879 var $post_args, enums, self = this; 10880 10881 10882 $post_args = $slice(arguments); 10883 enums = $post_args; 10884 $deny_frozen_access(self); 10885 self.enums = enums; 10886 self.iterated = []; 10887 return (self.object = self); 10888 }, -1); 10889 10890 $def(self, '$each', function $$each($a) { 10891 var block = $$each.$$p || nil, $post_args, args, self = this; 10892 10893 $$each.$$p = null; 10894 $post_args = $slice(arguments); 10895 args = $post_args; 10896 if (!(block !== nil)) { 10897 return $send(self, 'to_enum', ["each"].concat($to_a(args)), function $$1(){var self = $$1.$$s == null ? this : $$1.$$s; 10898 10899 return self.$size()}, {$$s: self}) 10900 } $send(self.enums, 'each', [], function $$2(enum$){var self = $$2.$$s == null ? this : $$2.$$s; 10901 if (self.iterated == null) self.iterated = nil; 10902 10903 10904 if (enum$ == null) enum$ = nil; 10905 self.iterated['$<<'](enum$); 10906 return $send(enum$, 'each', $to_a(args), block.$to_proc());}, {$$s: self}); 10907 return self; 10908 }, -1); 10909 10910 $def(self, '$size', function $$size($a) {try { var $t_return = $thrower('return'); 10911 var $post_args, args, self = this, accum = nil; 10912 10913 10914 $post_args = $slice(arguments); 10915 args = $post_args; 10916 accum = 0; 10917 $send(self.enums, 'each', [], function $$3(enum$){var size = nil; 10918 10919 10920 if (enum$ == null) enum$ = nil; 10921 size = $send(enum$, 'size', $to_a(args)); 10922 if ($truthy([nil, $$$($$$('Float'), 'INFINITY')]['$include?'](size))) { 10923 $t_return.$throw(size); 10924 }; 10925 return (accum = $rb_plus(accum, size));}, {$$ret: $t_return}); 10926 return accum;} catch($e) { 10927 if ($e === $t_return) return $e.$v; 10928 throw $e; 10929 } 10930 }, -1); 10931 10932 $def(self, '$rewind', function $$rewind() { 10933 var self = this; 10934 10935 10936 $send(self.iterated, 'reverse_each', [], function $$4(enum$){ 10937 10938 if (enum$ == null) enum$ = nil; 10939 if ($truthy(enum$['$respond_to?']("rewind"))) { 10940 return enum$.$rewind() 10941 } else { 10942 return nil 10943 }}); 10944 self.iterated = []; 10945 return self; 10946 }); 10947 return $def(self, '$inspect', function $$inspect() { 10948 var self = this; 10949 10950 return "#<Enumerator::Chain: " + (self.enums.$inspect()) + ">" 10951 }); 10952 })(self, self) 10953 })('::', null) 10954}; 10955 10956Opal.modules["corelib/enumerator/generator"] = function(Opal) {/* Generated by Opal 1.7.3 */ 10957 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.$$$; 10958 10959 Opal.add_stubs('include,raise,new,to_proc'); 10960 return (function($base, $super, $parent_nesting) { 10961 var self = $klass($base, $super, 'Enumerator'); 10962 10963 var $nesting = [self].concat($parent_nesting); 10964 10965 return (function($base, $super, $parent_nesting) { 10966 var self = $klass($base, $super, 'Generator'); 10967 10968 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 10969 10970 $proto.block = nil; 10971 10972 self.$include($$$('Enumerable')); 10973 10974 $def(self, '$initialize', function $$initialize() { 10975 var block = $$initialize.$$p || nil, self = this; 10976 10977 $$initialize.$$p = null; 10978 $deny_frozen_access(self); 10979 if (!$truthy(block)) { 10980 $Kernel.$raise($$$('LocalJumpError'), "no block given"); 10981 } return (self.block = block); 10982 }); 10983 return $def(self, '$each', function $$each($a) { 10984 var block = $$each.$$p || nil, $post_args, args, self = this, yielder = nil; 10985 10986 $$each.$$p = null; 10987 $post_args = $slice(arguments); 10988 args = $post_args; 10989 yielder = $send($$('Yielder'), 'new', [], block.$to_proc()); 10990 10991 try { 10992 args.unshift(yielder); 10993 10994 Opal.yieldX(self.block, args); 10995 } 10996 catch (e) { 10997 if (e && e.$thrower_type == "breaker") { 10998 return e.$v; 10999 } 11000 else { 11001 throw e; 11002 } 11003 } 11004 return self; 11005 }, -1); 11006 })($nesting[0], null, $nesting) 11007 })($nesting[0], null, $nesting) 11008}; 11009 11010Opal.modules["corelib/enumerator/lazy"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11011 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.$$$; 11012 11013 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'); 11014 return (function($base, $super, $parent_nesting) { 11015 var self = $klass($base, $super, 'Enumerator'); 11016 11017 var $nesting = [self].concat($parent_nesting); 11018 11019 return (function($base, $super, $parent_nesting) { 11020 var self = $klass($base, $super, 'Lazy'); 11021 11022 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11023 11024 $proto.enumerator = nil; 11025 11026 $klass(self, $$$('Exception'), 'StopLazyError'); 11027 $defs(self, '$for', function $Lazy_for$1(object, $a) { 11028 var $post_args, $fwd_rest, $yield = $Lazy_for$1.$$p || nil, self = this, lazy = nil; 11029 11030 $Lazy_for$1.$$p = null; 11031 11032 $post_args = $slice(arguments, 1); 11033 $fwd_rest = $post_args; 11034 lazy = $send2(self, $find_super(self, 'for', $Lazy_for$1, false, true), 'for', [object].concat($to_a($fwd_rest)), $yield); 11035 lazy.enumerator = object; 11036 return lazy; 11037 }, -2); 11038 11039 $def(self, '$initialize', function $$initialize(object, size) { 11040 var block = $$initialize.$$p || nil, self = this; 11041 11042 $$initialize.$$p = null; 11043 if (size == null) size = nil; 11044 $deny_frozen_access(self); 11045 if (!(block !== nil)) { 11046 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy new without a block"); 11047 } self.enumerator = object; 11048 return $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [size], function $$2(yielder, $a){var $post_args, each_args; 11049 11050 11051 if (yielder == null) yielder = nil; 11052 $post_args = $slice(arguments, 1); 11053 each_args = $post_args; 11054 try { 11055 return $send(object, 'each', $to_a(each_args), function $$3($b){var $post_args, args; 11056 11057 11058 $post_args = $slice(arguments); 11059 args = $post_args; 11060 11061 args.unshift(yielder); 11062 11063 $yieldX(block, args); 11064 ;}, -1) 11065 } catch ($err) { 11066 if (Opal.rescue($err, [$$('StopLazyError')])) { 11067 try { 11068 return nil 11069 } finally { Opal.pop_exception(); } 11070 } else { throw $err; } 11071 }}, -2); 11072 }, -2); 11073 11074 $def(self, '$lazy', $return_self); 11075 11076 $def(self, '$collect', function $$collect() { 11077 var block = $$collect.$$p || nil, self = this; 11078 11079 $$collect.$$p = null; 11080 if (!$truthy(block)) { 11081 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy map without a block"); 11082 } return $send($$('Lazy'), 'new', [self, self.$enumerator_size()], function $$4(enum$, $a){var $post_args, args; 11083 11084 11085 if (enum$ == null) enum$ = nil; 11086 $post_args = $slice(arguments, 1); 11087 args = $post_args; 11088 11089 var value = $yieldX(block, args); 11090 11091 enum$.$yield(value); 11092}, -2); 11093 }); 11094 11095 $def(self, '$collect_concat', function $$collect_concat() { 11096 var block = $$collect_concat.$$p || nil, self = this; 11097 11098 $$collect_concat.$$p = null; 11099 if (!$truthy(block)) { 11100 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy map without a block"); 11101 } return $send($$('Lazy'), 'new', [self, nil], function $$5(enum$, $a){var $post_args, args; 11102 11103 11104 if (enum$ == null) enum$ = nil; 11105 $post_args = $slice(arguments, 1); 11106 args = $post_args; 11107 11108 var value = $yieldX(block, args); 11109 11110 if ((value)['$respond_to?']("force") && (value)['$respond_to?']("each")) { 11111 $send((value), 'each', [], function $$6(v){ 11112 11113 if (v == null) v = nil; 11114 return enum$.$yield(v);}); 11115 } 11116 else { 11117 var array = $Opal.$try_convert(value, $$$('Array'), "to_ary"); 11118 11119 if (array === nil) { 11120 enum$.$yield(value); 11121 } 11122 else { 11123 $send((value), 'each', [], function $$7(v){ 11124 11125 if (v == null) v = nil; 11126 return enum$.$yield(v);}); 11127 } 11128 } 11129}, -2); 11130 }); 11131 11132 $def(self, '$drop', function $$drop(n) { 11133 var self = this, current_size = nil, set_size = nil, dropped = nil; 11134 11135 11136 n = $coerce_to(n, $$$('Integer'), 'to_int'); 11137 if ($truthy($rb_lt(n, 0))) { 11138 $Kernel.$raise($$$('ArgumentError'), "attempt to drop negative size"); 11139 } current_size = self.$enumerator_size(); 11140 set_size = ($eqeqeq($$$('Integer'), current_size) ? (($truthy($rb_lt(n, current_size)) ? (n) : (current_size))) : (current_size)); 11141 dropped = 0; 11142 return $send($$('Lazy'), 'new', [self, set_size], function $$8(enum$, $a){var $post_args, args; 11143 11144 11145 if (enum$ == null) enum$ = nil; 11146 $post_args = $slice(arguments, 1); 11147 args = $post_args; 11148 if ($truthy($rb_lt(dropped, n))) { 11149 return (dropped = $rb_plus(dropped, 1)) 11150 } else { 11151 return $send(enum$, 'yield', $to_a(args)) 11152 }}, -2); 11153 }); 11154 11155 $def(self, '$drop_while', function $$drop_while() { 11156 var block = $$drop_while.$$p || nil, self = this, succeeding = nil; 11157 11158 $$drop_while.$$p = null; 11159 if (!$truthy(block)) { 11160 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy drop_while without a block"); 11161 } succeeding = true; 11162 return $send($$('Lazy'), 'new', [self, nil], function $$9(enum$, $a){var $post_args, args; 11163 11164 11165 if (enum$ == null) enum$ = nil; 11166 $post_args = $slice(arguments, 1); 11167 args = $post_args; 11168 if ($truthy(succeeding)) { 11169 11170 var value = $yieldX(block, args); 11171 11172 if (!$truthy(value)) { 11173 succeeding = false; 11174 11175 $send(enum$, 'yield', $to_a(args)); 11176 } 11177 11178 } else { 11179 return $send(enum$, 'yield', $to_a(args)) 11180 }}, -2); 11181 }); 11182 11183 $def(self, '$enum_for', function $$enum_for($a, $b) { 11184 var block = $$enum_for.$$p || nil, $post_args, method, args, self = this; 11185 11186 $$enum_for.$$p = null; 11187 $post_args = $slice(arguments); 11188 11189 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 11190 args = $post_args; 11191 return $send(self.$class(), 'for', [self, method].concat($to_a(args)), block.$to_proc()); 11192 }, -1); 11193 11194 $def(self, '$find_all', function $$find_all() { 11195 var block = $$find_all.$$p || nil, self = this; 11196 11197 $$find_all.$$p = null; 11198 if (!$truthy(block)) { 11199 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy select without a block"); 11200 } return $send($$('Lazy'), 'new', [self, nil], function $$10(enum$, $a){var $post_args, args; 11201 11202 11203 if (enum$ == null) enum$ = nil; 11204 $post_args = $slice(arguments, 1); 11205 args = $post_args; 11206 11207 var value = $yieldX(block, args); 11208 11209 if ($truthy(value)) { 11210 $send(enum$, 'yield', $to_a(args)); 11211 } 11212}, -2); 11213 }); 11214 11215 $def(self, '$grep', function $$grep(pattern) { 11216 var block = $$grep.$$p || nil, self = this; 11217 11218 $$grep.$$p = null; 11219 if ($truthy(block)) { 11220 return $send($$('Lazy'), 'new', [self, nil], function $$11(enum$, $a){var $post_args, args; 11221 11222 11223 if (enum$ == null) enum$ = nil; 11224 $post_args = $slice(arguments, 1); 11225 args = $post_args; 11226 11227 var param = $Opal.$destructure(args), 11228 value = pattern['$==='](param); 11229 11230 if ($truthy(value)) { 11231 value = $yield1(block, param); 11232 11233 enum$.$yield($yield1(block, param)); 11234 } 11235}, -2) 11236 } else { 11237 return $send($$('Lazy'), 'new', [self, nil], function $$12(enum$, $a){var $post_args, args; 11238 11239 11240 if (enum$ == null) enum$ = nil; 11241 $post_args = $slice(arguments, 1); 11242 args = $post_args; 11243 11244 var param = $Opal.$destructure(args), 11245 value = pattern['$==='](param); 11246 11247 if ($truthy(value)) { 11248 enum$.$yield(param); 11249 } 11250}, -2) 11251 } }); 11252 11253 $def(self, '$reject', function $$reject() { 11254 var block = $$reject.$$p || nil, self = this; 11255 11256 $$reject.$$p = null; 11257 if (!$truthy(block)) { 11258 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy reject without a block"); 11259 } return $send($$('Lazy'), 'new', [self, nil], function $$13(enum$, $a){var $post_args, args; 11260 11261 11262 if (enum$ == null) enum$ = nil; 11263 $post_args = $slice(arguments, 1); 11264 args = $post_args; 11265 11266 var value = $yieldX(block, args); 11267 11268 if (!$truthy(value)) { 11269 $send(enum$, 'yield', $to_a(args)); 11270 } 11271}, -2); 11272 }); 11273 11274 $def(self, '$take', function $$take(n) { 11275 var self = this, current_size = nil, set_size = nil, taken = nil; 11276 11277 11278 n = $coerce_to(n, $$$('Integer'), 'to_int'); 11279 if ($truthy($rb_lt(n, 0))) { 11280 $Kernel.$raise($$$('ArgumentError'), "attempt to take negative size"); 11281 } current_size = self.$enumerator_size(); 11282 set_size = ($eqeqeq($$$('Integer'), current_size) ? (($truthy($rb_lt(n, current_size)) ? (n) : (current_size))) : (current_size)); 11283 taken = 0; 11284 return $send($$('Lazy'), 'new', [self, set_size], function $$14(enum$, $a){var $post_args, args; 11285 11286 11287 if (enum$ == null) enum$ = nil; 11288 $post_args = $slice(arguments, 1); 11289 args = $post_args; 11290 if ($truthy($rb_lt(taken, n))) { 11291 11292 $send(enum$, 'yield', $to_a(args)); 11293 return (taken = $rb_plus(taken, 1)); 11294 } else { 11295 return $Kernel.$raise($$('StopLazyError')) 11296 }}, -2); 11297 }); 11298 11299 $def(self, '$take_while', function $$take_while() { 11300 var block = $$take_while.$$p || nil, self = this; 11301 11302 $$take_while.$$p = null; 11303 if (!$truthy(block)) { 11304 $Kernel.$raise($$$('ArgumentError'), "tried to call lazy take_while without a block"); 11305 } return $send($$('Lazy'), 'new', [self, nil], function $$15(enum$, $a){var $post_args, args; 11306 11307 11308 if (enum$ == null) enum$ = nil; 11309 $post_args = $slice(arguments, 1); 11310 args = $post_args; 11311 11312 var value = $yieldX(block, args); 11313 11314 if ($truthy(value)) { 11315 $send(enum$, 'yield', $to_a(args)); 11316 } 11317 else { 11318 $Kernel.$raise($$('StopLazyError')); 11319 } 11320}, -2); 11321 }); 11322 11323 $def(self, '$inspect', function $$inspect() { 11324 var self = this; 11325 11326 return "#<" + (self.$class()) + ": " + (self.enumerator.$inspect()) + ">" 11327 }); 11328 $alias(self, "force", "to_a"); 11329 $alias(self, "filter", "find_all"); 11330 $alias(self, "flat_map", "collect_concat"); 11331 $alias(self, "map", "collect"); 11332 $alias(self, "select", "find_all"); 11333 return $alias(self, "to_enum", "enum_for"); 11334 })(self, self, $nesting) 11335 })('::', null, $nesting) 11336}; 11337 11338Opal.modules["corelib/enumerator/yielder"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11339 var $klass = Opal.klass, $def = Opal.def, $slice = Opal.slice, $send = Opal.send, $to_a = Opal.to_a, $nesting = [], nil = Opal.nil; 11340 11341 Opal.add_stubs('yield,proc'); 11342 return (function($base, $super, $parent_nesting) { 11343 var self = $klass($base, $super, 'Enumerator'); 11344 11345 var $nesting = [self].concat($parent_nesting); 11346 11347 return (function($base, $super) { 11348 var self = $klass($base, $super, 'Yielder'); 11349 11350 var $proto = self.$$prototype; 11351 11352 $proto.block = nil; 11353 11354 11355 $def(self, '$initialize', function $$initialize() { 11356 var block = $$initialize.$$p || nil, self = this; 11357 11358 $$initialize.$$p = null; 11359 self.block = block; 11360 return self; 11361 }); 11362 11363 $def(self, '$yield', function $Yielder_yield$1($a) { 11364 var $post_args, values, self = this; 11365 11366 11367 $post_args = $slice(arguments); 11368 values = $post_args; 11369 11370 var value = Opal.yieldX(self.block, values); 11371 11372 if (value && value.$thrower_type == "break") { 11373 throw value; 11374 } 11375 11376 return value; 11377 }, -1); 11378 11379 $def(self, '$<<', function $Yielder_$lt$lt$2(value) { 11380 var self = this; 11381 11382 11383 self.$yield(value); 11384 return self; 11385 }); 11386 return $def(self, '$to_proc', function $$to_proc() { 11387 var self = this; 11388 11389 return $send(self, 'proc', [], function $$3($a){var $post_args, values, self = $$3.$$s == null ? this : $$3.$$s; 11390 11391 11392 $post_args = $slice(arguments); 11393 values = $post_args; 11394 return $send(self, 'yield', $to_a(values));}, {$$arity: -1, $$s: self}) 11395 }); 11396 })($nesting[0], null) 11397 })($nesting[0], null, $nesting) 11398}; 11399 11400Opal.modules["corelib/enumerator"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11401 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.$$$; 11402 11403 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'); 11404 11405 self.$require("corelib/enumerable"); 11406 return (function($base, $super, $parent_nesting) { 11407 var self = $klass($base, $super, 'Enumerator'); 11408 11409 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 11410 11411 $proto.size = $proto.args = $proto.object = $proto.method = $proto.values = $proto.cursor = nil; 11412 11413 self.$include($$$('Enumerable')); 11414 self.$$prototype.$$is_enumerator = true; 11415 $defs(self, '$for', function $Enumerator_for$1(object, $a, $b) { 11416 var block = $Enumerator_for$1.$$p || nil, $post_args, method, args, self = this; 11417 11418 $Enumerator_for$1.$$p = null; 11419 $post_args = $slice(arguments, 1); 11420 11421 if ($post_args.length > 0) method = $post_args.shift();if (method == null) method = "each"; 11422 args = $post_args; 11423 11424 var obj = self.$allocate(); 11425 11426 obj.object = object; 11427 obj.size = block; 11428 obj.method = method; 11429 obj.args = args; 11430 obj.cursor = 0; 11431 11432 return obj; 11433 }, -2); 11434 11435 $def(self, '$initialize', function $$initialize($a) { 11436 var block = $$initialize.$$p || nil, self = this; 11437 11438 $$initialize.$$p = null; 11439 $slice(arguments); 11440 $deny_frozen_access(self); 11441 self.cursor = 0; 11442 if ($truthy(block)) { 11443 11444 self.object = $send($$('Generator'), 'new', [], block.$to_proc()); 11445 self.method = "each"; 11446 self.args = []; 11447 self.size = arguments[0] || nil; 11448 if (($truthy(self.size) && ($not(self.size['$respond_to?']("call"))))) { 11449 return (self.size = $coerce_to(self.size, $$$('Integer'), 'to_int')) 11450 } else { 11451 return nil 11452 } } else { 11453 11454 self.object = arguments[0]; 11455 self.method = arguments[1] || "each"; 11456 self.args = $slice(arguments, 2); 11457 return (self.size = nil); 11458 } }, -1); 11459 11460 $def(self, '$each', function $$each($a) { 11461 var block = $$each.$$p || nil, $post_args, args, self = this; 11462 11463 $$each.$$p = null; 11464 $post_args = $slice(arguments); 11465 args = $post_args; 11466 if (($truthy(block['$nil?']()) && ($truthy(args['$empty?']())))) { 11467 return self 11468 } args = $rb_plus(self.args, args); 11469 if ($truthy(block['$nil?']())) { 11470 return $send(self.$class(), 'new', [self.object, self.method].concat($to_a(args))) 11471 } return $send(self.object, '__send__', [self.method].concat($to_a(args)), block.$to_proc()); 11472 }, -1); 11473 11474 $def(self, '$size', function $$size() { 11475 var self = this; 11476 11477 if ($truthy(self.size['$respond_to?']("call"))) { 11478 return $send(self.size, 'call', $to_a(self.args)) 11479 } else { 11480 return self.size 11481 } 11482 }); 11483 11484 $def(self, '$with_index', function $$with_index(offset) { 11485 var block = $$with_index.$$p || nil, self = this; 11486 11487 $$with_index.$$p = null; 11488 if (offset == null) offset = 0; 11489 offset = ($truthy(offset) ? ($coerce_to(offset, $$$('Integer'), 'to_int')) : (0)); 11490 if (!$truthy(block)) { 11491 return $send(self, 'enum_for', ["with_index", offset], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 11492 11493 return self.$size()}, {$$s: self}) 11494 } 11495 var index = offset; 11496 11497 self.$each.$$p = function() { 11498 var param = $Opal.$destructure(arguments), 11499 value = block(param, index); 11500 11501 index++; 11502 11503 return value; 11504 }; 11505 11506 return self.$each(); 11507 }, -1); 11508 11509 $def(self, '$each_with_index', function $$each_with_index() { 11510 var block = $$each_with_index.$$p || nil, self = this; 11511 11512 $$each_with_index.$$p = null; 11513 if (!(block !== nil)) { 11514 return $send(self, 'enum_for', ["each_with_index"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 11515 11516 return self.$size()}, {$$s: self}) 11517 } $send2(self, $find_super(self, 'each_with_index', $$each_with_index, false, true), 'each_with_index', [], block); 11518 return self.object; 11519 }); 11520 11521 $def(self, '$rewind', function $$rewind() { 11522 var self = this; 11523 11524 11525 self.cursor = 0; 11526 return self; 11527 }); 11528 11529 $def(self, '$peek_values', function $$peek_values() { 11530 var self = this, $ret_or_1 = nil; 11531 11532 11533 self.values = ($truthy(($ret_or_1 = self.values)) ? ($ret_or_1) : ($send(self, 'map', [], function $$4($a){var $post_args, i; 11534 11535 11536 $post_args = $slice(arguments); 11537 i = $post_args; 11538 return i;}, -1))); 11539 if ($truthy($rb_ge(self.cursor, self.values.$length()))) { 11540 $Kernel.$raise($$$('StopIteration'), "iteration reached an end"); 11541 } return self.values['$[]'](self.cursor); 11542 }); 11543 11544 $def(self, '$peek', function $$peek() { 11545 var self = this, values = nil; 11546 11547 11548 values = self.$peek_values(); 11549 if ($truthy($rb_le(values.$length(), 1))) { 11550 return values['$[]'](0) 11551 } else { 11552 return values 11553 } }); 11554 11555 $def(self, '$next_values', function $$next_values() { 11556 var self = this, out = nil; 11557 11558 11559 out = self.$peek_values(); 11560 self.cursor = $rb_plus(self.cursor, 1); 11561 return out; 11562 }); 11563 11564 $def(self, '$next', function $$next() { 11565 var self = this, values = nil; 11566 11567 11568 values = self.$next_values(); 11569 if ($truthy($rb_le(values.$length(), 1))) { 11570 return values['$[]'](0) 11571 } else { 11572 return values 11573 } }); 11574 11575 $def(self, '$feed', function $$feed(arg) { 11576 var self = this; 11577 11578 return self.$raise($$('NotImplementedError'), "Opal doesn't support Enumerator#feed") 11579 }); 11580 11581 $def(self, '$+', function $Enumerator_$plus$5(other) { 11582 var self = this; 11583 11584 return $$$($$$('Enumerator'), 'Chain').$new(self, other) 11585 }); 11586 11587 $def(self, '$inspect', function $$inspect() { 11588 var self = this, result = nil; 11589 11590 11591 result = "#<" + (self.$class()) + ": " + (self.object.$inspect()) + ":" + (self.method); 11592 if ($truthy(self.args['$any?']())) { 11593 result = $rb_plus(result, "(" + (self.args.$inspect()['$[]']($$$('Range').$new(1, -2))) + ")"); 11594 } return $rb_plus(result, ">"); 11595 }); 11596 $alias(self, "with_object", "each_with_object"); 11597 self.$autoload("ArithmeticSequence", "corelib/enumerator/arithmetic_sequence"); 11598 self.$autoload("Chain", "corelib/enumerator/chain"); 11599 self.$autoload("Generator", "corelib/enumerator/generator"); 11600 self.$autoload("Lazy", "corelib/enumerator/lazy"); 11601 return self.$autoload("Yielder", "corelib/enumerator/yielder"); 11602 })('::', null, $nesting); 11603}; 11604 11605Opal.modules["corelib/numeric"] = function(Opal) {/* Generated by Opal 1.7.3 */ 11606 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.$$$; 11607 11608 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'); 11609 11610 self.$require("corelib/comparable"); 11611 return (function($base, $super) { 11612 var self = $klass($base, $super, 'Numeric'); 11613 11614 11615 11616 self.$include($$$('Comparable')); 11617 11618 $def(self, '$coerce', function $$coerce(other) { 11619 var self = this; 11620 11621 11622 if ($truthy(other['$instance_of?'](self.$class()))) { 11623 return [other, self] 11624 } return [$Kernel.$Float(other), $Kernel.$Float(self)]; 11625 }); 11626 11627 $def(self, '$__coerced__', function $$__coerced__(method, other) { 11628 var $a, $b, self = this, a = nil, b = nil; 11629 11630 if ($truthy(other['$respond_to?']("coerce"))) { 11631 11632 $b = other.$coerce(self), $a = $to_ary($b), (a = ($a[0] == null ? nil : $a[0])), (b = ($a[1] == null ? nil : $a[1])); 11633 return a.$__send__(method, b); 11634 } else 11635 switch (method) { 11636 case "+": 11637 case "-": 11638 case "*": 11639 case "/": 11640 case "%": 11641 case "&": 11642 case "|": 11643 case "^": 11644 case "**": 11645 return $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Numeric") 11646 case ">": 11647 case ">=": 11648 case "<": 11649 case "<=": 11650 case "<=>": 11651 return $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (other.$class()) + " failed") 11652 default: 11653 return nil 11654 } 11655 }); 11656 11657 $def(self, '$<=>', function $Numeric_$lt_eq_gt$1(other) { 11658 var self = this; 11659 11660 11661 if ($truthy(self['$equal?'](other))) { 11662 return 0 11663 } return nil; 11664 }); 11665 11666 $def(self, '$+@', $return_self); 11667 11668 $def(self, '$-@', function $Numeric_$minus$$2() { 11669 var self = this; 11670 11671 return $rb_minus(0, self) 11672 }); 11673 11674 $def(self, '$%', function $Numeric_$percent$3(other) { 11675 var self = this; 11676 11677 return $rb_minus(self, $rb_times(other, self.$div(other))) 11678 }); 11679 11680 $def(self, '$abs', function $$abs() { 11681 var self = this; 11682 11683 if ($rb_lt(self, 0)) { 11684 return self['$-@']() 11685 } else { 11686 return self 11687 } 11688 }); 11689 11690 $def(self, '$abs2', function $$abs2() { 11691 var self = this; 11692 11693 return $rb_times(self, self) 11694 }); 11695 11696 $def(self, '$angle', function $$angle() { 11697 var self = this; 11698 11699 if ($rb_lt(self, 0)) { 11700 return $$$($$$('Math'), 'PI') 11701 } else { 11702 return 0 11703 } 11704 }); 11705 11706 $def(self, '$ceil', function $$ceil(ndigits) { 11707 var self = this; 11708 11709 11710 if (ndigits == null) ndigits = 0; 11711 return self.$to_f().$ceil(ndigits); 11712 }, -1); 11713 11714 $def(self, '$conj', $return_self); 11715 11716 $def(self, '$denominator', function $$denominator() { 11717 var self = this; 11718 11719 return self.$to_r().$denominator() 11720 }); 11721 11722 $def(self, '$div', function $$div(other) { 11723 var self = this; 11724 11725 11726 if ($eqeq(other, 0)) { 11727 $Kernel.$raise($$$('ZeroDivisionError'), "divided by o"); 11728 } return $rb_divide(self, other).$floor(); 11729 }); 11730 11731 $def(self, '$divmod', function $$divmod(other) { 11732 var self = this; 11733 11734 return [self.$div(other), self['$%'](other)] 11735 }); 11736 11737 $def(self, '$fdiv', function $$fdiv(other) { 11738 var self = this; 11739 11740 return $rb_divide(self.$to_f(), other) 11741 }); 11742 11743 $def(self, '$floor', function $$floor(ndigits) { 11744 var self = this; 11745 11746 11747 if (ndigits == null) ndigits = 0; 11748 return self.$to_f().$floor(ndigits); 11749 }, -1); 11750 11751 $def(self, '$i', function $$i() { 11752 var self = this; 11753 11754 return $Kernel.$Complex(0, self) 11755 }); 11756 11757 $def(self, '$imag', $return_val(0)); 11758 11759 $def(self, '$integer?', $return_val(false)); 11760 11761 $def(self, '$nonzero?', function $Numeric_nonzero$ques$4() { 11762 var self = this; 11763 11764 if ($truthy(self['$zero?']())) { 11765 return nil 11766 } else { 11767 return self 11768 } 11769 }); 11770 11771 $def(self, '$numerator', function $$numerator() { 11772 var self = this; 11773 11774 return self.$to_r().$numerator() 11775 }); 11776 11777 $def(self, '$polar', function $$polar() { 11778 var self = this; 11779 11780 return [self.$abs(), self.$arg()] 11781 }); 11782 11783 $def(self, '$quo', function $$quo(other) { 11784 var self = this; 11785 11786 return $rb_divide($Opal['$coerce_to!'](self, $$$('Rational'), "to_r"), other) 11787 }); 11788 11789 $def(self, '$real', $return_self); 11790 11791 $def(self, '$real?', $return_val(true)); 11792 11793 $def(self, '$rect', function $$rect() { 11794 var self = this; 11795 11796 return [self, 0] 11797 }); 11798 11799 $def(self, '$round', function $$round(digits) { 11800 var self = this; 11801 return self.$to_f().$round(digits); 11802 }, -1); 11803 11804 $def(self, '$step', function $$step($a, $b, $c) { 11805 var block = $$step.$$p || nil, $post_args, $kwargs, limit, step, to, by, self = this, counter = nil; 11806 11807 $$step.$$p = null; 11808 $post_args = $slice(arguments); 11809 $kwargs = $extract_kwargs($post_args); 11810 $kwargs = $ensure_kwargs($kwargs); 11811 11812 if ($post_args.length > 0) limit = $post_args.shift(); 11813 if ($post_args.length > 0) step = $post_args.shift(); 11814 to = $kwargs.$$smap["to"]; 11815 by = $kwargs.$$smap["by"]; 11816 if (limit !== undefined && to !== undefined) { 11817 $Kernel.$raise($$$('ArgumentError'), "to is given twice"); 11818 } 11819 11820 if (step !== undefined && by !== undefined) { 11821 $Kernel.$raise($$$('ArgumentError'), "step is given twice"); 11822 } 11823 11824 if (to !== undefined) { 11825 limit = to; 11826 } 11827 11828 if (by !== undefined) { 11829 step = by; 11830 } 11831 11832 if (limit === undefined) { 11833 limit = nil; 11834 } 11835 11836 function validateParameters() { 11837 if (step === nil) { 11838 $Kernel.$raise($$$('TypeError'), "step must be numeric"); 11839 } 11840 11841 if (step != null && step['$=='](0)) { 11842 $Kernel.$raise($$$('ArgumentError'), "step can't be 0"); 11843 } 11844 11845 if (step === nil || step == null) { 11846 step = 1; 11847 } 11848 11849 var sign = step['$<=>'](0); 11850 11851 if (sign === nil) { 11852 $Kernel.$raise($$$('ArgumentError'), "0 can't be coerced into " + (step.$class())); 11853 } 11854 11855 if (limit === nil || limit == null) { 11856 limit = sign > 0 ? $$$($$$('Float'), 'INFINITY') : $$$($$$('Float'), 'INFINITY')['$-@'](); 11857 } 11858 11859 $Opal.$compare(self, limit); 11860 } 11861 11862 function stepFloatSize() { 11863 if ((step > 0 && self > limit) || (step < 0 && self < limit)) { 11864 return 0; 11865 } else if (step === Infinity || step === -Infinity) { 11866 return 1; 11867 } else { 11868 var abs = Math.abs, floor = Math.floor, 11869 err = (abs(self) + abs(limit) + abs(limit - self)) / abs(step) * $$$($$$('Float'), 'EPSILON'); 11870 11871 if (err === Infinity || err === -Infinity) { 11872 return 0; 11873 } else { 11874 if (err > 0.5) { 11875 err = 0.5; 11876 } 11877 11878 return floor((limit - self) / step + err) + 1 11879 } 11880 } 11881 } 11882 11883 function stepSize() { 11884 validateParameters(); 11885 11886 if (step === 0) { 11887 return Infinity; 11888 } 11889 11890 if (step % 1 !== 0) { 11891 return stepFloatSize(); 11892 } else if ((step > 0 && self > limit) || (step < 0 && self < limit)) { 11893 return 0; 11894 } else { 11895 var ceil = Math.ceil, abs = Math.abs, 11896 lhs = abs(self - limit) + 1, 11897 rhs = abs(step); 11898 11899 return ceil(lhs / rhs); 11900 } 11901 } 11902 if (!(block !== nil)) { 11903 if ((($not(limit) || ($truthy(limit['$is_a?']($$$('Numeric'))))) && (($not(step) || ($truthy(step['$is_a?']($$$('Numeric')))))))) { 11904 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new([limit, step, ($truthy(to) ? ("to: ") : nil), ($truthy(by) ? ("by: ") : nil)], self) 11905 } else { 11906 return $send(self, 'enum_for', ["step", limit, step], (stepSize).$to_proc()) 11907 } 11908 } 11909 validateParameters(); 11910 11911 var isDesc = step['$negative?'](), 11912 isInf = step['$=='](0) || 11913 (limit === Infinity && !isDesc) || 11914 (limit === -Infinity && isDesc); 11915 11916 if (self.$$is_number && step.$$is_number && limit.$$is_number) { 11917 if (self % 1 === 0 && (isInf || limit % 1 === 0) && step % 1 === 0) { 11918 var value = self; 11919 11920 if (isInf) { 11921 for (;; value += step) { 11922 block(value); 11923 } 11924 } else if (isDesc) { 11925 for (; value >= limit; value += step) { 11926 block(value); 11927 } 11928 } else { 11929 for (; value <= limit; value += step) { 11930 block(value); 11931 } 11932 } 11933 11934 return self; 11935 } else { 11936 var begin = self.$to_f().valueOf(); 11937 step = step.$to_f().valueOf(); 11938 limit = limit.$to_f().valueOf(); 11939 11940 var n = stepFloatSize(); 11941 11942 if (!isFinite(step)) { 11943 if (n !== 0) block(begin); 11944 } else if (step === 0) { 11945 while (true) { 11946 block(begin); 11947 } 11948 } else { 11949 for (var i = 0; i < n; i++) { 11950 var d = i * step + self; 11951 if (step >= 0 ? limit < d : limit > d) { 11952 d = limit; 11953 } 11954 block(d); 11955 } 11956 } 11957 11958 return self; 11959 } 11960 } 11961 counter = self; 11962 while ($truthy(isDesc ? $rb_ge(counter, limit) : $rb_le(counter, limit))) { 11963 11964 Opal.yield1(block, counter); 11965 counter = $rb_plus(counter, step); 11966 } }, -1); 11967 11968 $def(self, '$to_c', function $$to_c() { 11969 var self = this; 11970 11971 return $Kernel.$Complex(self, 0) 11972 }); 11973 11974 $def(self, '$to_int', function $$to_int() { 11975 var self = this; 11976 11977 return self.$to_i() 11978 }); 11979 11980 $def(self, '$truncate', function $$truncate(ndigits) { 11981 var self = this; 11982 11983 11984 if (ndigits == null) ndigits = 0; 11985 return self.$to_f().$truncate(ndigits); 11986 }, -1); 11987 11988 $def(self, '$zero?', function $Numeric_zero$ques$5() { 11989 var self = this; 11990 11991 return self['$=='](0) 11992 }); 11993 11994 $def(self, '$positive?', function $Numeric_positive$ques$6() { 11995 var self = this; 11996 11997 return $rb_gt(self, 0) 11998 }); 11999 12000 $def(self, '$negative?', function $Numeric_negative$ques$7() { 12001 var self = this; 12002 12003 return $rb_lt(self, 0) 12004 }); 12005 12006 $def(self, '$dup', $return_self); 12007 12008 $def(self, '$clone', function $$clone($kwargs) { 12009 var self = this; 12010 12011 12012 $kwargs = $ensure_kwargs($kwargs); 12013 12014 $kwargs.$$smap["freeze"]; return self; 12015 }, -1); 12016 12017 $def(self, '$finite?', $return_val(true)); 12018 12019 $def(self, '$infinite?', $return_val(nil)); 12020 $alias(self, "arg", "angle"); 12021 $alias(self, "conjugate", "conj"); 12022 $alias(self, "imaginary", "imag"); 12023 $alias(self, "magnitude", "abs"); 12024 $alias(self, "modulo", "%"); 12025 $alias(self, "phase", "arg"); 12026 return $alias(self, "rectangular", "rect"); 12027 })('::', null); 12028}; 12029 12030Opal.modules["corelib/array"] = function(Opal) {/* Generated by Opal 1.7.3 */ 12031 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.$$$; 12032 12033 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'); 12034 12035 self.$require("corelib/enumerable"); 12036 self.$require("corelib/numeric"); 12037 return (function($base, $super, $parent_nesting) { 12038 var self = $klass($base, $super, 'Array'); 12039 12040 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 12041 12042 12043 self.$include($$$('Enumerable')); 12044 Opal.prop(self.$$prototype, '$$is_array', true); 12045 12046 // Recent versions of V8 (> 7.1) only use an optimized implementation when Array.prototype is unmodified. 12047 // For instance, "array-splice.tq" has a "fast path" (ExtractFastJSArray, defined in "src/codegen/code-stub-assembler.cc") 12048 // but it's only enabled when "IsPrototypeInitialArrayPrototype()" is true. 12049 // 12050 // Older versions of V8 were using relatively fast JS-with-extensions code even when Array.prototype is modified: 12051 // https://github.com/v8/v8/blob/7.0.1/src/js/array.js#L599-L642 12052 // 12053 // In short, Array operations are slow in recent versions of V8 when the Array.prototype has been tampered. 12054 // So, when possible, we are using faster open-coded version to boost the performance. 12055 12056 // As of V8 8.4, depending on the size of the array, this is up to ~25x times faster than Array#shift() 12057 // Implementation is heavily inspired by: https://github.com/nodejs/node/blob/ba684805b6c0eded76e5cd89ee00328ac7a59365/lib/internal/util.js#L341-L347 12058 function shiftNoArg(list) { 12059 var r = list[0]; 12060 var index = 1; 12061 var length = list.length; 12062 for (; index < length; index++) { 12063 list[index - 1] = list[index]; 12064 } 12065 list.pop(); 12066 return r; 12067 } 12068 12069 function toArraySubclass(obj, klass) { 12070 if (klass.$$name === Opal.Array) { 12071 return obj; 12072 } else { 12073 return klass.$allocate().$replace((obj).$to_a()); 12074 } 12075 } 12076 12077 // A helper for keep_if and delete_if, filter is either Opal.truthy 12078 // or Opal.falsy. 12079 function filterIf(self, filter, block) { 12080 var value, raised = null, updated = new Array(self.length); 12081 12082 for (var i = 0, i2 = 0, length = self.length; i < length; i++) { 12083 if (!raised) { 12084 try { 12085 value = $yield1(block, self[i]); 12086 } catch(error) { 12087 raised = error; 12088 } 12089 } 12090 12091 if (raised || filter(value)) { 12092 updated[i2] = self[i]; 12093 i2 += 1; 12094 } 12095 } 12096 12097 if (i2 !== i) { 12098 self.splice.apply(self, [0, updated.length].concat(updated)); 12099 self.splice(i2, updated.length); 12100 } 12101 12102 if (raised) throw raised; 12103 } 12104 $defs(self, '$[]', function $Array_$$$1($a) { 12105 var $post_args, objects, self = this; 12106 12107 12108 $post_args = $slice(arguments); 12109 objects = $post_args; 12110 return toArraySubclass(objects, self); }, -1); 12111 12112 $def(self, '$initialize', function $$initialize(size, obj) { 12113 var block = $$initialize.$$p || nil, self = this; 12114 12115 $$initialize.$$p = null; 12116 if (size == null) size = nil; 12117 if (obj == null) obj = nil; 12118 12119 $deny_frozen_access(self); 12120 12121 if (obj !== nil && block !== nil) { 12122 $Kernel.$warn("warning: block supersedes default value argument"); 12123 } 12124 12125 if (size > $$$($$$('Integer'), 'MAX')) { 12126 $Kernel.$raise($$$('ArgumentError'), "array size too big"); 12127 } 12128 12129 if (arguments.length > 2) { 12130 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..2)"); 12131 } 12132 12133 if (arguments.length === 0) { 12134 self.splice(0, self.length); 12135 return self; 12136 } 12137 12138 if (arguments.length === 1) { 12139 if (size.$$is_array) { 12140 self.$replace(size.$to_a()); 12141 return self; 12142 } else if (size['$respond_to?']("to_ary")) { 12143 self.$replace(size.$to_ary()); 12144 return self; 12145 } 12146 } 12147 12148 size = $coerce_to(size, $$$('Integer'), 'to_int'); 12149 12150 if (size < 0) { 12151 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 12152 } 12153 12154 self.splice(0, self.length); 12155 var i, value; 12156 12157 if (block === nil) { 12158 for (i = 0; i < size; i++) { 12159 self.push(obj); 12160 } 12161 } 12162 else { 12163 for (i = 0, value; i < size; i++) { 12164 value = block(i); 12165 self[i] = value; 12166 } 12167 } 12168 12169 return self; 12170 }, -1); 12171 $defs(self, '$try_convert', function $$try_convert(obj) { 12172 12173 return $Opal['$coerce_to?'](obj, $$$('Array'), "to_ary") 12174 }); 12175 12176 $def(self, '$&', function $Array_$$2(other) { 12177 var self = this; 12178 12179 12180 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12181 12182 var result = [], hash = $hash2([], {}), i, length, item; 12183 12184 for (i = 0, length = other.length; i < length; i++) { 12185 $hash_put(hash, other[i], true); 12186 } 12187 12188 for (i = 0, length = self.length; i < length; i++) { 12189 item = self[i]; 12190 if ($hash_delete(hash, item) !== undefined) { 12191 result.push(item); 12192 } 12193 } 12194 12195 return result; 12196 }); 12197 12198 $def(self, '$|', function $Array_$$3(other) { 12199 var self = this; 12200 12201 12202 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12203 12204 var hash = $hash2([], {}), i, length; 12205 12206 for (i = 0, length = self.length; i < length; i++) { 12207 $hash_put(hash, self[i], true); 12208 } 12209 12210 for (i = 0, length = other.length; i < length; i++) { 12211 $hash_put(hash, other[i], true); 12212 } 12213 12214 return hash.$keys(); 12215 }); 12216 12217 $def(self, '$*', function $Array_$$4(other) { 12218 var self = this; 12219 12220 12221 if ($truthy(other['$respond_to?']("to_str"))) { 12222 return self.$join(other.$to_str()) 12223 } other = $coerce_to(other, $$$('Integer'), 'to_int'); 12224 if ($truthy(other < 0)) { 12225 $Kernel.$raise($$$('ArgumentError'), "negative argument"); 12226 } 12227 var result = [], 12228 converted = self.$to_a(); 12229 12230 for (var i = 0; i < other; i++) { 12231 result = result.concat(converted); 12232 } 12233 12234 return result; 12235 }); 12236 12237 $def(self, '$+', function $Array_$plus$5(other) { 12238 var self = this; 12239 12240 12241 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12242 return self.concat(other); }); 12243 12244 $def(self, '$-', function $Array_$minus$6(other) { 12245 var self = this; 12246 12247 12248 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12249 if ($truthy(self.length === 0)) { 12250 return [] 12251 } if ($truthy(other.length === 0)) { 12252 return self.slice() 12253 } 12254 var result = [], hash = $hash2([], {}), i, length, item; 12255 12256 for (i = 0, length = other.length; i < length; i++) { 12257 $hash_put(hash, other[i], true); 12258 } 12259 12260 for (i = 0, length = self.length; i < length; i++) { 12261 item = self[i]; 12262 if ($hash_get(hash, item) === undefined) { 12263 result.push(item); 12264 } 12265 } 12266 12267 return result; 12268 }); 12269 12270 $def(self, '$<<', function $Array_$lt$lt$7(object) { 12271 var self = this; 12272 12273 12274 $deny_frozen_access(self); 12275 self.push(object); 12276 return self; 12277 }); 12278 12279 $def(self, '$<=>', function $Array_$lt_eq_gt$8(other) { 12280 var self = this; 12281 12282 12283 if ($eqeqeq($$$('Array'), other)) { 12284 other = other.$to_a(); 12285 } else if ($truthy(other['$respond_to?']("to_ary"))) { 12286 other = other.$to_ary().$to_a(); 12287 } else { 12288 return nil 12289 } 12290 if (self.$hash() === other.$hash()) { 12291 return 0; 12292 } 12293 12294 var count = Math.min(self.length, other.length); 12295 12296 for (var i = 0; i < count; i++) { 12297 var tmp = (self[i])['$<=>'](other[i]); 12298 12299 if (tmp !== 0) { 12300 return tmp; 12301 } 12302 } 12303 12304 return (self.length)['$<=>'](other.length); 12305 }); 12306 12307 $def(self, '$==', function $Array_$eq_eq$9(other) { 12308 var self = this; 12309 12310 12311 var recursed = {}; 12312 12313 function _eqeq(array, other) { 12314 var i, length, a, b; 12315 12316 if (array === other) 12317 return true; 12318 12319 if (!other.$$is_array) { 12320 if ($respond_to(other, '$to_ary')) { 12321 return (other)['$=='](array); 12322 } else { 12323 return false; 12324 } 12325 } 12326 12327 if (array.$$constructor !== Array) 12328 array = (array).$to_a(); 12329 if (other.$$constructor !== Array) 12330 other = (other).$to_a(); 12331 12332 if (array.length !== other.length) { 12333 return false; 12334 } 12335 12336 recursed[(array).$object_id()] = true; 12337 12338 for (i = 0, length = array.length; i < length; i++) { 12339 a = array[i]; 12340 b = other[i]; 12341 if (a.$$is_array) { 12342 if (b.$$is_array && b.length !== a.length) { 12343 return false; 12344 } 12345 if (!recursed.hasOwnProperty((a).$object_id())) { 12346 if (!_eqeq(a, b)) { 12347 return false; 12348 } 12349 } 12350 } else { 12351 if (!(a)['$=='](b)) { 12352 return false; 12353 } 12354 } 12355 } 12356 12357 return true; 12358 } 12359 12360 return _eqeq(self, other); 12361 12362 }); 12363 12364 function $array_slice_range(self, index) { 12365 var size = self.length, 12366 exclude, from, to, result; 12367 12368 exclude = index.excl; 12369 from = index.begin === nil ? 0 : $coerce_to(index.begin, Opal.Integer, 'to_int'); 12370 to = index.end === nil ? -1 : $coerce_to(index.end, Opal.Integer, 'to_int'); 12371 12372 if (from < 0) { 12373 from += size; 12374 12375 if (from < 0) { 12376 return nil; 12377 } 12378 } 12379 12380 if (index.excl_rev && index.begin !== nil) { 12381 from += 1; 12382 } 12383 12384 if (from > size) { 12385 return nil; 12386 } 12387 12388 if (to < 0) { 12389 to += size; 12390 12391 if (to < 0) { 12392 return []; 12393 } 12394 } 12395 12396 if (!exclude || index.end === nil) { 12397 to += 1; 12398 } 12399 12400 result = self.slice(from, to); 12401 return result; 12402 } 12403 12404 function $array_slice_arithmetic_seq(self, index) { 12405 var array, out = [], i = 0, pseudorange; 12406 12407 if (index.step < 0) { 12408 pseudorange = { 12409 begin: index.range.end, 12410 end: index.range.begin, 12411 excl: false, 12412 excl_rev: index.range.excl 12413 }; 12414 array = $array_slice_range(self, pseudorange).$reverse(); 12415 } 12416 else { 12417 array = $array_slice_range(self, index.range); 12418 } 12419 12420 while (i < array.length) { 12421 out.push(array[i]); 12422 i += Math.abs(index.step); 12423 } 12424 12425 return out; 12426 } 12427 12428 function $array_slice_index_length(self, index, length) { 12429 var size = self.length, 12430 result; 12431 12432 index = $coerce_to(index, Opal.Integer, 'to_int'); 12433 12434 if (index < 0) { 12435 index += size; 12436 12437 if (index < 0) { 12438 return nil; 12439 } 12440 } 12441 12442 if (length === undefined) { 12443 if (index >= size || index < 0) { 12444 return nil; 12445 } 12446 12447 return self[index]; 12448 } 12449 else { 12450 length = $coerce_to(length, Opal.Integer, 'to_int'); 12451 12452 if (length < 0 || index > size || index < 0) { 12453 return nil; 12454 } 12455 12456 result = self.slice(index, index + length); 12457 } 12458 return result; 12459 } 12460 12461 $def(self, '$[]', function $Array_$$$10(index, length) { 12462 var self = this; 12463 12464 if (index.$$is_range) { 12465 return $array_slice_range(self, index); 12466 } 12467 else if (index.$$is_arithmetic_seq) { 12468 return $array_slice_arithmetic_seq(self, index); 12469 } 12470 else { 12471 return $array_slice_index_length(self, index, length); 12472 } 12473 }, -2); 12474 12475 $def(self, '$[]=', function $Array_$$$eq$11(index, value, extra) { 12476 var self = this, data = nil, length = nil; 12477 $deny_frozen_access(self); 12478 data = nil; 12479 12480 var i, size = self.length; 12481 12482 if (index.$$is_range) { 12483 if (value.$$is_array) 12484 data = value.$to_a(); 12485 else if (value['$respond_to?']("to_ary")) 12486 data = value.$to_ary().$to_a(); 12487 else 12488 data = [value]; 12489 12490 var exclude = index.excl, 12491 from = index.begin === nil ? 0 : $coerce_to(index.begin, Opal.Integer, 'to_int'), 12492 to = index.end === nil ? -1 : $coerce_to(index.end, Opal.Integer, 'to_int'); 12493 12494 if (from < 0) { 12495 from += size; 12496 12497 if (from < 0) { 12498 $Kernel.$raise($$$('RangeError'), "" + (index.$inspect()) + " out of range"); 12499 } 12500 } 12501 12502 if (to < 0) { 12503 to += size; 12504 } 12505 12506 if (!exclude || index.end === nil) { 12507 to += 1; 12508 } 12509 12510 if (from > size) { 12511 for (i = size; i < from; i++) { 12512 self[i] = nil; 12513 } 12514 } 12515 12516 if (to < 0) { 12517 self.splice.apply(self, [from, 0].concat(data)); 12518 } 12519 else { 12520 self.splice.apply(self, [from, to - from].concat(data)); 12521 } 12522 12523 return value; 12524 } else { 12525 if (extra === undefined) { 12526 (length = 1); 12527 } else { 12528 length = value; 12529 value = extra; 12530 12531 if (value.$$is_array) 12532 data = value.$to_a(); 12533 else if (value['$respond_to?']("to_ary")) 12534 data = value.$to_ary().$to_a(); 12535 else 12536 data = [value]; 12537 } 12538 12539 var old; 12540 12541 index = $coerce_to(index, $$$('Integer'), 'to_int'); 12542 length = $coerce_to(length, $$$('Integer'), 'to_int'); 12543 12544 if (index < 0) { 12545 old = index; 12546 index += size; 12547 12548 if (index < 0) { 12549 $Kernel.$raise($$$('IndexError'), "index " + (old) + " too small for array; minimum " + (-self.length)); 12550 } 12551 } 12552 12553 if (length < 0) { 12554 $Kernel.$raise($$$('IndexError'), "negative length (" + (length) + ")"); 12555 } 12556 12557 if (index > size) { 12558 for (i = size; i < index; i++) { 12559 self[i] = nil; 12560 } 12561 } 12562 12563 if (extra === undefined) { 12564 self[index] = value; 12565 } 12566 else { 12567 self.splice.apply(self, [index, length].concat(data)); 12568 } 12569 12570 return value; 12571 } 12572 }, -3); 12573 12574 $def(self, '$any?', function $Array_any$ques$12(pattern) { 12575 var block = $Array_any$ques$12.$$p || nil, self = this; 12576 12577 $Array_any$ques$12.$$p = null; 12578 if (self.length === 0) return false; 12579 return $send2(self, $find_super(self, 'any?', $Array_any$ques$12, false, true), 'any?', [pattern], block); 12580 }, -1); 12581 12582 $def(self, '$assoc', function $$assoc(object) { 12583 var self = this; 12584 12585 12586 for (var i = 0, length = self.length, item; i < length; i++) { 12587 if (item = self[i], item.length && (item[0])['$=='](object)) { 12588 return item; 12589 } 12590 } 12591 12592 return nil; 12593 12594 }); 12595 12596 $def(self, '$at', function $$at(index) { 12597 var self = this; 12598 12599 12600 index = $coerce_to(index, $$$('Integer'), 'to_int'); 12601 12602 if (index < 0) { 12603 index += self.length; 12604 } 12605 12606 if (index < 0 || index >= self.length) { 12607 return nil; 12608 } 12609 12610 return self[index]; 12611 12612 }); 12613 12614 $def(self, '$bsearch_index', function $$bsearch_index() { 12615 var block = $$bsearch_index.$$p || nil, self = this; 12616 12617 $$bsearch_index.$$p = null; 12618 if (!(block !== nil)) { 12619 return self.$enum_for("bsearch_index") 12620 } 12621 var min = 0, 12622 max = self.length, 12623 mid, 12624 val, 12625 ret, 12626 smaller = false, 12627 satisfied = nil; 12628 12629 while (min < max) { 12630 mid = min + Math.floor((max - min) / 2); 12631 val = self[mid]; 12632 ret = $yield1(block, val); 12633 12634 if (ret === true) { 12635 satisfied = mid; 12636 smaller = true; 12637 } 12638 else if (ret === false || ret === nil) { 12639 smaller = false; 12640 } 12641 else if (ret.$$is_number) { 12642 if (ret === 0) { return mid; } 12643 smaller = (ret < 0); 12644 } 12645 else { 12646 $Kernel.$raise($$$('TypeError'), "wrong argument type " + ((ret).$class()) + " (must be numeric, true, false or nil)"); 12647 } 12648 12649 if (smaller) { max = mid; } else { min = mid + 1; } 12650 } 12651 12652 return satisfied; 12653 }); 12654 12655 $def(self, '$bsearch', function $$bsearch() { 12656 var block = $$bsearch.$$p || nil, self = this, index = nil; 12657 12658 $$bsearch.$$p = null; 12659 if (!(block !== nil)) { 12660 return self.$enum_for("bsearch") 12661 } index = $send(self, 'bsearch_index', [], block.$to_proc()); 12662 12663 if (index != null && index.$$is_number) { 12664 return self[index]; 12665 } else { 12666 return index; 12667 } 12668 }); 12669 12670 $def(self, '$cycle', function $$cycle(n) { 12671 var block = $$cycle.$$p || nil, self = this; 12672 12673 $$cycle.$$p = null; 12674 if (n == null) n = nil; 12675 if (!(block !== nil)) { 12676 return $send(self, 'enum_for', ["cycle", n], function $$13(){var self = $$13.$$s == null ? this : $$13.$$s; 12677 12678 if ($truthy(n['$nil?']())) { 12679 return $$$($$$('Float'), 'INFINITY') 12680 } else { 12681 12682 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 12683 if ($truthy($rb_gt(n, 0))) { 12684 return $rb_times(self.$enumerator_size(), n) 12685 } else { 12686 return 0 12687 } }}, {$$s: self}) 12688 } if (($truthy(self['$empty?']()) || ($eqeq(n, 0)))) { 12689 return nil 12690 } 12691 var i, length; 12692 12693 if (n === nil) { 12694 while (true) { 12695 for (i = 0, length = self.length; i < length; i++) { 12696 $yield1(block, self[i]); 12697 } 12698 } 12699 } 12700 else { 12701 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 12702 if (n <= 0) { 12703 return self; 12704 } 12705 12706 while (n > 0) { 12707 for (i = 0, length = self.length; i < length; i++) { 12708 $yield1(block, self[i]); 12709 } 12710 12711 n--; 12712 } 12713 } 12714 return self; 12715 }, -1); 12716 12717 $def(self, '$clear', function $$clear() { 12718 var self = this; 12719 12720 12721 $deny_frozen_access(self); 12722 self.splice(0, self.length); 12723 return self; 12724 }); 12725 12726 $def(self, '$count', function $$count(object) { 12727 var block = $$count.$$p || nil, self = this; 12728 12729 $$count.$$p = null; 12730 if (($truthy(object !== undefined) || ($truthy(block)))) { 12731 return $send2(self, $find_super(self, 'count', $$count, false, true), 'count', [object], block) 12732 } else { 12733 return self.$size() 12734 } }, -1); 12735 12736 $def(self, '$initialize_copy', function $$initialize_copy(other) { 12737 var self = this; 12738 12739 return self.$replace(other) 12740 }); 12741 12742 $def(self, '$collect', function $$collect() { 12743 var block = $$collect.$$p || nil, self = this; 12744 12745 $$collect.$$p = null; 12746 if (!(block !== nil)) { 12747 return $send(self, 'enum_for', ["collect"], function $$14(){var self = $$14.$$s == null ? this : $$14.$$s; 12748 12749 return self.$size()}, {$$s: self}) 12750 } 12751 var result = []; 12752 12753 for (var i = 0, length = self.length; i < length; i++) { 12754 var value = $yield1(block, self[i]); 12755 result.push(value); 12756 } 12757 12758 return result; 12759 }); 12760 12761 $def(self, '$collect!', function $Array_collect$excl$15() { 12762 var block = $Array_collect$excl$15.$$p || nil, self = this; 12763 12764 $Array_collect$excl$15.$$p = null; 12765 if (!(block !== nil)) { 12766 return $send(self, 'enum_for', ["collect!"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 12767 12768 return self.$size()}, {$$s: self}) 12769 } 12770 $deny_frozen_access(self); 12771 12772 for (var i = 0, length = self.length; i < length; i++) { 12773 var value = $yield1(block, self[i]); 12774 self[i] = value; 12775 } 12776 return self; 12777 }); 12778 12779 function binomial_coefficient(n, k) { 12780 if (n === k || k === 0) { 12781 return 1; 12782 } 12783 12784 if (k > 0 && n > k) { 12785 return binomial_coefficient(n - 1, k - 1) + binomial_coefficient(n - 1, k); 12786 } 12787 12788 return 0; 12789 } 12790 12791 $def(self, '$combination', function $$combination(n) { 12792 var $yield = $$combination.$$p || nil, self = this, num = nil; 12793 12794 $$combination.$$p = null; 12795 12796 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 12797 if (!($yield !== nil)) { 12798 return $send(self, 'enum_for', ["combination", num], function $$17(){var self = $$17.$$s == null ? this : $$17.$$s; 12799 12800 return binomial_coefficient(self.length, num)}, {$$s: self}) 12801 } 12802 var i, length, stack, chosen, lev, done, next; 12803 12804 if (num === 0) { 12805 Opal.yield1($yield, []); 12806 } else if (num === 1) { 12807 for (i = 0, length = self.length; i < length; i++) { 12808 Opal.yield1($yield, [self[i]]); 12809 } 12810 } 12811 else if (num === self.length) { 12812 Opal.yield1($yield, self.slice()); 12813 } 12814 else if (num >= 0 && num < self.length) { 12815 stack = []; 12816 for (i = 0; i <= num + 1; i++) { 12817 stack.push(0); 12818 } 12819 12820 chosen = []; 12821 lev = 0; 12822 done = false; 12823 stack[0] = -1; 12824 12825 while (!done) { 12826 chosen[lev] = self[stack[lev+1]]; 12827 while (lev < num - 1) { 12828 lev++; 12829 next = stack[lev+1] = stack[lev] + 1; 12830 chosen[lev] = self[next]; 12831 } 12832 Opal.yield1($yield, chosen.slice()); 12833 lev++; 12834 do { 12835 done = (lev === 0); 12836 stack[lev]++; 12837 lev--; 12838 } while ( stack[lev+1] + num === self.length + lev + 1 ); 12839 } 12840 } 12841 return self; 12842 }); 12843 12844 $def(self, '$repeated_combination', function $$repeated_combination(n) { 12845 var $yield = $$repeated_combination.$$p || nil, self = this, num = nil; 12846 12847 $$repeated_combination.$$p = null; 12848 12849 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 12850 if (!($yield !== nil)) { 12851 return $send(self, 'enum_for', ["repeated_combination", num], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 12852 12853 return binomial_coefficient(self.length + num - 1, num);}, {$$s: self}) 12854 } 12855 function iterate(max, from, buffer, self) { 12856 if (buffer.length == max) { 12857 var copy = buffer.slice(); 12858 Opal.yield1($yield, copy); 12859 return; 12860 } 12861 for (var i = from; i < self.length; i++) { 12862 buffer.push(self[i]); 12863 iterate(max, i, buffer, self); 12864 buffer.pop(); 12865 } 12866 } 12867 12868 if (num >= 0) { 12869 iterate(num, 0, [], self); 12870 } 12871 return self; 12872 }); 12873 12874 $def(self, '$compact', function $$compact() { 12875 var self = this; 12876 12877 12878 var result = []; 12879 12880 for (var i = 0, length = self.length, item; i < length; i++) { 12881 if ((item = self[i]) !== nil) { 12882 result.push(item); 12883 } 12884 } 12885 12886 return result; 12887 12888 }); 12889 12890 $def(self, '$compact!', function $Array_compact$excl$19() { 12891 var self = this; 12892 12893 12894 $deny_frozen_access(self); 12895 12896 var original = self.length; 12897 12898 for (var i = 0, length = self.length; i < length; i++) { 12899 if (self[i] === nil) { 12900 self.splice(i, 1); 12901 12902 length--; 12903 i--; 12904 } 12905 } 12906 12907 return self.length === original ? nil : self; 12908 12909 }); 12910 12911 $def(self, '$concat', function $$concat($a) { 12912 var $post_args, others, self = this; 12913 12914 12915 $post_args = $slice(arguments); 12916 others = $post_args; 12917 $deny_frozen_access(self); 12918 others = $send(others, 'map', [], function $$20(other){var self = $$20.$$s == null ? this : $$20.$$s; 12919 12920 12921 if (other == null) other = nil; 12922 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 12923 if ($truthy(other['$equal?'](self))) { 12924 other = other.$dup(); 12925 } return other;}, {$$s: self}); 12926 $send(others, 'each', [], function $$21(other){var self = $$21.$$s == null ? this : $$21.$$s; 12927 12928 12929 if (other == null) other = nil; 12930 12931 for (var i = 0, length = other.length; i < length; i++) { 12932 self.push(other[i]); 12933 } 12934}, {$$s: self}); 12935 return self; 12936 }, -1); 12937 12938 $def(self, '$delete', function $Array_delete$22(object) { 12939 var $yield = $Array_delete$22.$$p || nil, self = this; 12940 12941 $Array_delete$22.$$p = null; 12942 12943 var original = self.length; 12944 12945 for (var i = 0, length = original; i < length; i++) { 12946 if ((self[i])['$=='](object)) { 12947 $deny_frozen_access(self); 12948 12949 self.splice(i, 1); 12950 12951 length--; 12952 i--; 12953 } 12954 } 12955 12956 if (self.length === original) { 12957 if (($yield !== nil)) { 12958 return Opal.yieldX($yield, []); 12959 } 12960 return nil; 12961 } 12962 return object; 12963 12964 }); 12965 12966 $def(self, '$delete_at', function $$delete_at(index) { 12967 var self = this; 12968 12969 12970 $deny_frozen_access(self); 12971 12972 index = $coerce_to(index, $$$('Integer'), 'to_int'); 12973 12974 if (index < 0) { 12975 index += self.length; 12976 } 12977 12978 if (index < 0 || index >= self.length) { 12979 return nil; 12980 } 12981 12982 var result = self[index]; 12983 12984 self.splice(index, 1); 12985 12986 return result; 12987 12988 }); 12989 12990 $def(self, '$delete_if', function $$delete_if() { 12991 var block = $$delete_if.$$p || nil, self = this; 12992 12993 $$delete_if.$$p = null; 12994 if (!(block !== nil)) { 12995 return $send(self, 'enum_for', ["delete_if"], function $$23(){var self = $$23.$$s == null ? this : $$23.$$s; 12996 12997 return self.$size()}, {$$s: self}) 12998 } 12999 $deny_frozen_access(self); 13000 13001 filterIf(self, $falsy, block) 13002 ; 13003 return self; 13004 }); 13005 13006 $def(self, '$difference', function $$difference($a) { 13007 var $post_args, arrays, self = this; 13008 13009 13010 $post_args = $slice(arguments); 13011 arrays = $post_args; 13012 return $send(arrays, 'reduce', [self.$to_a().$dup()], function $$24(a, b){ 13013 13014 if (a == null) a = nil; 13015 if (b == null) b = nil; 13016 return $rb_minus(a, b);}); 13017 }, -1); 13018 13019 $def(self, '$dig', function $$dig(idx, $a) { 13020 var $post_args, idxs, self = this, item = nil; 13021 13022 13023 $post_args = $slice(arguments, 1); 13024 idxs = $post_args; 13025 item = self['$[]'](idx); 13026 13027 if (item === nil || idxs.length === 0) { 13028 return item; 13029 } 13030 if (!$truthy(item['$respond_to?']("dig"))) { 13031 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method"); 13032 } return $send(item, 'dig', $to_a(idxs)); 13033 }, -2); 13034 13035 $def(self, '$drop', function $$drop(number) { 13036 var self = this; 13037 13038 13039 number = $coerce_to(number, $$$('Integer'), 'to_int'); 13040 13041 if (number < 0) { 13042 $Kernel.$raise($$$('ArgumentError')); 13043 } 13044 13045 return self.slice(number); 13046 13047 }); 13048 13049 $def(self, '$dup', function $$dup() { 13050 var $yield = $$dup.$$p || nil, self = this; 13051 13052 $$dup.$$p = null; 13053 13054 13055 if (self.$$class === Opal.Array && 13056 self.$$class.$allocate.$$pristine && 13057 self.$copy_instance_variables.$$pristine && 13058 self.$initialize_dup.$$pristine) { 13059 return self.slice(0); 13060 } 13061 return $send2(self, $find_super(self, 'dup', $$dup, false, true), 'dup', [], $yield); 13062 }); 13063 13064 $def(self, '$each', function $$each() { 13065 var block = $$each.$$p || nil, self = this; 13066 13067 $$each.$$p = null; 13068 if (!(block !== nil)) { 13069 return $send(self, 'enum_for', ["each"], function $$25(){var self = $$25.$$s == null ? this : $$25.$$s; 13070 13071 return self.$size()}, {$$s: self}) 13072 } 13073 for (var i = 0, length = self.length; i < length; i++) { 13074 $yield1(block, self[i]); 13075 } 13076 return self; 13077 }); 13078 13079 $def(self, '$each_index', function $$each_index() { 13080 var block = $$each_index.$$p || nil, self = this; 13081 13082 $$each_index.$$p = null; 13083 if (!(block !== nil)) { 13084 return $send(self, 'enum_for', ["each_index"], function $$26(){var self = $$26.$$s == null ? this : $$26.$$s; 13085 13086 return self.$size()}, {$$s: self}) 13087 } 13088 for (var i = 0, length = self.length; i < length; i++) { 13089 $yield1(block, i); 13090 } 13091 return self; 13092 }); 13093 13094 $def(self, '$empty?', function $Array_empty$ques$27() { 13095 var self = this; 13096 13097 return self.length === 0; 13098 }); 13099 13100 $def(self, '$eql?', function $Array_eql$ques$28(other) { 13101 var self = this; 13102 13103 13104 var recursed = {}; 13105 13106 function _eql(array, other) { 13107 var i, length, a, b; 13108 13109 if (!other.$$is_array) { 13110 return false; 13111 } 13112 13113 other = other.$to_a(); 13114 13115 if (array.length !== other.length) { 13116 return false; 13117 } 13118 13119 recursed[(array).$object_id()] = true; 13120 13121 for (i = 0, length = array.length; i < length; i++) { 13122 a = array[i]; 13123 b = other[i]; 13124 if (a.$$is_array) { 13125 if (b.$$is_array && b.length !== a.length) { 13126 return false; 13127 } 13128 if (!recursed.hasOwnProperty((a).$object_id())) { 13129 if (!_eql(a, b)) { 13130 return false; 13131 } 13132 } 13133 } else { 13134 if (!(a)['$eql?'](b)) { 13135 return false; 13136 } 13137 } 13138 } 13139 13140 return true; 13141 } 13142 13143 return _eql(self, other); 13144 13145 }); 13146 13147 $def(self, '$fetch', function $$fetch(index, defaults) { 13148 var block = $$fetch.$$p || nil, self = this; 13149 13150 $$fetch.$$p = null; 13151 13152 var original = index; 13153 13154 index = $coerce_to(index, $$$('Integer'), 'to_int'); 13155 13156 if (index < 0) { 13157 index += self.length; 13158 } 13159 13160 if (index >= 0 && index < self.length) { 13161 return self[index]; 13162 } 13163 13164 if (block !== nil && defaults != null) { 13165 self.$warn("warning: block supersedes default value argument"); 13166 } 13167 13168 if (block !== nil) { 13169 return block(original); 13170 } 13171 13172 if (defaults != null) { 13173 return defaults; 13174 } 13175 13176 if (self.length === 0) { 13177 $Kernel.$raise($$$('IndexError'), "index " + (original) + " outside of array bounds: 0...0"); 13178 } 13179 else { 13180 $Kernel.$raise($$$('IndexError'), "index " + (original) + " outside of array bounds: -" + (self.length) + "..." + (self.length)); 13181 } 13182 }, -2); 13183 13184 $def(self, '$fill', function $$fill($a) { 13185 var block = $$fill.$$p || nil, $post_args, args, $b, $c, self = this, one = nil, two = nil, obj = nil, left = nil, right = nil; 13186 13187 $$fill.$$p = null; 13188 $post_args = $slice(arguments); 13189 args = $post_args; 13190 13191 $deny_frozen_access(self); 13192 13193 var i, value; 13194 if ($truthy(block)) { 13195 13196 if ($truthy(args.length > 2)) { 13197 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$length()) + " for 0..2)"); 13198 } $c = args, $b = $to_ary($c), (one = ($b[0] == null ? nil : $b[0])), (two = ($b[1] == null ? nil : $b[1])); 13199 } else { 13200 13201 if ($truthy(args.length == 0)) { 13202 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (0 for 1..3)"); 13203 } else if ($truthy(args.length > 3)) { 13204 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (args.$length()) + " for 1..3)"); 13205 } $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])); 13206 } if ($eqeqeq($$$('Range'), one)) { 13207 13208 if ($truthy(two)) { 13209 $Kernel.$raise($$$('TypeError'), "length invalid with range"); 13210 } left = one.begin === nil ? 0 : $coerce_to(one.begin, $$$('Integer'), 'to_int'); 13211 if ($truthy(left < 0)) { 13212 left += this.length; 13213 } if ($truthy(left < 0)) { 13214 $Kernel.$raise($$$('RangeError'), "" + (one.$inspect()) + " out of range"); 13215 } right = one.end === nil ? -1 : $coerce_to(one.end, $$$('Integer'), 'to_int'); 13216 if ($truthy(right < 0)) { 13217 right += this.length; 13218 } if (!$truthy(one['$exclude_end?']())) { 13219 right += 1; 13220 } if ($truthy(right <= left)) { 13221 return self 13222 } } else if ($truthy(one)) { 13223 13224 left = $coerce_to(one, $$$('Integer'), 'to_int'); 13225 if ($truthy(left < 0)) { 13226 left += this.length; 13227 } if ($truthy(left < 0)) { 13228 left = 0; 13229 } if ($truthy(two)) { 13230 13231 right = $coerce_to(two, $$$('Integer'), 'to_int'); 13232 if ($truthy(right == 0)) { 13233 return self 13234 } right += left; 13235 } else { 13236 right = this.length; 13237 } } else { 13238 13239 left = 0; 13240 right = this.length; 13241 } if ($truthy(left > this.length)) { 13242 13243 for (i = this.length; i < right; i++) { 13244 self[i] = nil; 13245 } 13246 13247 } if ($truthy(right > this.length)) { 13248 this.length = right; 13249 } if ($truthy(block)) { 13250 13251 for (this.length; left < right; left++) { 13252 value = block(left); 13253 self[left] = value; 13254 } 13255 13256 } else { 13257 13258 for (this.length; left < right; left++) { 13259 self[left] = obj; 13260 } 13261 13262 } return self; 13263 }, -1); 13264 13265 $def(self, '$first', function $$first(count) { 13266 var self = this; 13267 13268 if (count == null) { 13269 return self.length === 0 ? nil : self[0]; 13270 } 13271 13272 count = $coerce_to(count, $$$('Integer'), 'to_int'); 13273 13274 if (count < 0) { 13275 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 13276 } 13277 13278 return self.slice(0, count); 13279 }, -1); 13280 13281 $def(self, '$flatten', function $$flatten(level) { 13282 var self = this; 13283 13284 function _flatten(array, level) { 13285 var result = [], 13286 i, length, 13287 item, ary; 13288 13289 array = (array).$to_a(); 13290 13291 for (i = 0, length = array.length; i < length; i++) { 13292 item = array[i]; 13293 13294 if (!$respond_to(item, '$to_ary', true)) { 13295 result.push(item); 13296 continue; 13297 } 13298 13299 ary = (item).$to_ary(); 13300 13301 if (ary === nil) { 13302 result.push(item); 13303 continue; 13304 } 13305 13306 if (!ary.$$is_array) { 13307 $Kernel.$raise($$$('TypeError')); 13308 } 13309 13310 if (ary === self) { 13311 $Kernel.$raise($$$('ArgumentError')); 13312 } 13313 13314 switch (level) { 13315 case undefined: 13316 result = result.concat(_flatten(ary)); 13317 break; 13318 case 0: 13319 result.push(ary); 13320 break; 13321 default: 13322 result.push.apply(result, _flatten(ary, level - 1)); 13323 } 13324 } 13325 return result; 13326 } 13327 13328 if (level !== undefined) { 13329 level = $coerce_to(level, $$$('Integer'), 'to_int'); 13330 } 13331 13332 return _flatten(self, level); 13333 }, -1); 13334 13335 $def(self, '$flatten!', function $Array_flatten$excl$29(level) { 13336 var self = this; 13337 13338 $deny_frozen_access(self); 13339 13340 var flattened = self.$flatten(level); 13341 13342 if (self.length == flattened.length) { 13343 for (var i = 0, length = self.length; i < length; i++) { 13344 if (self[i] !== flattened[i]) { 13345 break; 13346 } 13347 } 13348 13349 if (i == length) { 13350 return nil; 13351 } 13352 } 13353 13354 self.$replace(flattened); 13355 return self; 13356 }, -1); 13357 13358 $def(self, '$freeze', function $$freeze() { 13359 var self = this; 13360 13361 13362 if ($truthy(self['$frozen?']())) { 13363 return self 13364 } return $freeze(self); }); 13365 13366 $def(self, '$hash', function $$hash() { 13367 var self = this; 13368 13369 13370 var top = ($hash_ids === undefined), 13371 result = ['A'], 13372 hash_id = self.$object_id(), 13373 item, i, key; 13374 13375 try { 13376 if (top) { 13377 $hash_ids = Object.create(null); 13378 } 13379 13380 // return early for recursive structures 13381 if ($hash_ids[hash_id]) { 13382 return 'self'; 13383 } 13384 13385 for (key in $hash_ids) { 13386 item = $hash_ids[key]; 13387 if (self['$eql?'](item)) { 13388 return 'self'; 13389 } 13390 } 13391 13392 $hash_ids[hash_id] = self; 13393 13394 for (i = 0; i < self.length; i++) { 13395 item = self[i]; 13396 result.push(item.$hash()); 13397 } 13398 13399 return result.join(','); 13400 } finally { 13401 if (top) { 13402 $hash_ids = undefined; 13403 } 13404 } 13405 13406 }); 13407 13408 $def(self, '$include?', function $Array_include$ques$30(member) { 13409 var self = this; 13410 13411 13412 for (var i = 0, length = self.length; i < length; i++) { 13413 if ((self[i])['$=='](member)) { 13414 return true; 13415 } 13416 } 13417 13418 return false; 13419 13420 }); 13421 13422 $def(self, '$index', function $$index(object) { 13423 var block = $$index.$$p || nil, self = this; 13424 13425 $$index.$$p = null; 13426 13427 var i, length, value; 13428 13429 if (object != null && block !== nil) { 13430 self.$warn("warning: given block not used"); 13431 } 13432 13433 if (object != null) { 13434 for (i = 0, length = self.length; i < length; i++) { 13435 if ((self[i])['$=='](object)) { 13436 return i; 13437 } 13438 } 13439 } 13440 else if (block !== nil) { 13441 for (i = 0, length = self.length; i < length; i++) { 13442 value = block(self[i]); 13443 13444 if (value !== false && value !== nil) { 13445 return i; 13446 } 13447 } 13448 } 13449 else { 13450 return self.$enum_for("index"); 13451 } 13452 13453 return nil; 13454 }, -1); 13455 13456 $def(self, '$insert', function $$insert(index, $a) { 13457 var $post_args, objects, self = this; 13458 13459 13460 $post_args = $slice(arguments, 1); 13461 objects = $post_args; 13462 13463 $deny_frozen_access(self); 13464 13465 index = $coerce_to(index, $$$('Integer'), 'to_int'); 13466 13467 if (objects.length > 0) { 13468 if (index < 0) { 13469 index += self.length + 1; 13470 13471 if (index < 0) { 13472 $Kernel.$raise($$$('IndexError'), "" + (index) + " is out of bounds"); 13473 } 13474 } 13475 if (index > self.length) { 13476 for (var i = self.length; i < index; i++) { 13477 self.push(nil); 13478 } 13479 } 13480 13481 self.splice.apply(self, [index, 0].concat(objects)); 13482 } 13483 return self; 13484 }, -2); 13485 var inspect_stack = []; 13486 13487 $def(self, '$inspect', function $$inspect() { 13488 var self = this; 13489 13490 13491 13492 var result = [], 13493 id = self.$__id__(), 13494 pushed = true; 13495 13496 return (function() { try { 13497 13498 13499 if (inspect_stack.indexOf(id) !== -1) { 13500 pushed = false; 13501 return '[...]'; 13502 } 13503 inspect_stack.push(id); 13504 13505 for (var i = 0, length = self.length; i < length; i++) { 13506 var item = self['$[]'](i); 13507 13508 result.push($$('Opal').$inspect(item)); 13509 } 13510 13511 return '[' + result.join(', ') + ']'; 13512 ; 13513 return nil; 13514 } finally { 13515 if (pushed) inspect_stack.pop(); 13516 } })(); }); 13517 13518 $def(self, '$intersection', function $$intersection($a) { 13519 var $post_args, arrays, self = this; 13520 13521 13522 $post_args = $slice(arguments); 13523 arrays = $post_args; 13524 return $send(arrays, 'reduce', [self.$to_a().$dup()], function $$31(a, b){ 13525 13526 if (a == null) a = nil; 13527 if (b == null) b = nil; 13528 return a['$&'](b);}); 13529 }, -1); 13530 13531 $def(self, '$intersect?', function $Array_intersect$ques$32(other) { 13532 var self = this; 13533 13534 return self.$intersection(other)['$empty?']()['$!']() 13535 }); 13536 13537 $def(self, '$join', function $$join(sep) { 13538 var self = this; 13539 if ($gvars[","] == null) $gvars[","] = nil; 13540 13541 13542 if (sep == null) sep = nil; 13543 if ($truthy(self.length === 0)) { 13544 return "" 13545 } if ($truthy(sep === nil)) { 13546 sep = $gvars[","]; 13547 } 13548 var result = []; 13549 var i, length, item, tmp; 13550 13551 for (i = 0, length = self.length; i < length; i++) { 13552 item = self[i]; 13553 13554 if ($respond_to(item, '$to_str')) { 13555 tmp = (item).$to_str(); 13556 13557 if (tmp !== nil) { 13558 result.push((tmp).$to_s()); 13559 13560 continue; 13561 } 13562 } 13563 13564 if ($respond_to(item, '$to_ary')) { 13565 tmp = (item).$to_ary(); 13566 13567 if (tmp === self) { 13568 $Kernel.$raise($$$('ArgumentError')); 13569 } 13570 13571 if (tmp !== nil) { 13572 result.push((tmp).$join(sep)); 13573 13574 continue; 13575 } 13576 } 13577 13578 if ($respond_to(item, '$to_s')) { 13579 tmp = (item).$to_s(); 13580 13581 if (tmp !== nil) { 13582 result.push(tmp); 13583 13584 continue; 13585 } 13586 } 13587 13588 $Kernel.$raise($$$('NoMethodError').$new("" + ($$('Opal').$inspect(self.$item())) + " doesn't respond to #to_str, #to_ary or #to_s", "to_str")); 13589 } 13590 13591 if (sep === nil) { 13592 return result.join(''); 13593 } 13594 else { 13595 return result.join($Opal['$coerce_to!'](sep, $$$('String'), "to_str").$to_s()); 13596 } 13597 }, -1); 13598 13599 $def(self, '$keep_if', function $$keep_if() { 13600 var block = $$keep_if.$$p || nil, self = this; 13601 13602 $$keep_if.$$p = null; 13603 if (!(block !== nil)) { 13604 return $send(self, 'enum_for', ["keep_if"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 13605 13606 return self.$size()}, {$$s: self}) 13607 } 13608 $deny_frozen_access(self); 13609 13610 filterIf(self, $truthy, block) 13611 ; 13612 return self; 13613 }); 13614 13615 $def(self, '$last', function $$last(count) { 13616 var self = this; 13617 13618 if (count == null) { 13619 return self.length === 0 ? nil : self[self.length - 1]; 13620 } 13621 13622 count = $coerce_to(count, $$$('Integer'), 'to_int'); 13623 13624 if (count < 0) { 13625 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 13626 } 13627 13628 if (count > self.length) { 13629 count = self.length; 13630 } 13631 13632 return self.slice(self.length - count, self.length); 13633 }, -1); 13634 13635 $def(self, '$length', function $$length() { 13636 var self = this; 13637 13638 return self.length; 13639 }); 13640 13641 $def(self, '$max', function $$max(n) { 13642 var block = $$max.$$p || nil, self = this; 13643 13644 $$max.$$p = null; 13645 return $send(self.$each(), 'max', [n], block.$to_proc()); 13646 }, -1); 13647 13648 $def(self, '$min', function $$min() { 13649 var block = $$min.$$p || nil, self = this; 13650 13651 $$min.$$p = null; 13652 return $send(self.$each(), 'min', [], block.$to_proc()); 13653 }); 13654 13655 // Returns the product of from, from-1, ..., from - how_many + 1. 13656 function descending_factorial(from, how_many) { 13657 var count = how_many >= 0 ? 1 : 0; 13658 while (how_many) { 13659 count *= from; 13660 from--; 13661 how_many--; 13662 } 13663 return count; 13664 } 13665 13666 $def(self, '$permutation', function $$permutation(num) { 13667 var block = $$permutation.$$p || nil, self = this, perm = nil, used = nil; 13668 13669 $$permutation.$$p = null; 13670 if (!(block !== nil)) { 13671 return $send(self, 'enum_for', ["permutation", num], function $$34(){var self = $$34.$$s == null ? this : $$34.$$s; 13672 13673 return descending_factorial(self.length, num === undefined ? self.length : num);}, {$$s: self}) 13674 } 13675 var permute, offensive, output; 13676 13677 if (num === undefined) { 13678 num = self.length; 13679 } 13680 else { 13681 num = $coerce_to(num, $$$('Integer'), 'to_int'); 13682 } 13683 13684 if (num < 0 || self.length < num) ; 13685 else if (num === 0) { 13686 // exactly one permutation: the zero-length array 13687 Opal.yield1(block, []); 13688 } 13689 else if (num === 1) { 13690 // this is a special, easy case 13691 for (var i = 0; i < self.length; i++) { 13692 Opal.yield1(block, [self[i]]); 13693 } 13694 } 13695 else { 13696 // this is the general case 13697 (perm = $$('Array').$new(num)); 13698 (used = $$('Array').$new(self.length, false)); 13699 13700 permute = function(num, perm, index, used, blk) { 13701 self = this; 13702 for(var i = 0; i < self.length; i++){ 13703 if(used['$[]'](i)['$!']()) { 13704 perm[index] = i; 13705 if(index < num - 1) { 13706 used[i] = true; 13707 permute.call(self, num, perm, index + 1, used, blk); 13708 used[i] = false; 13709 } 13710 else { 13711 output = []; 13712 for (var j = 0; j < perm.length; j++) { 13713 output.push(self[perm[j]]); 13714 } 13715 $yield1(blk, output); 13716 } 13717 } 13718 } 13719 }; 13720 13721 if ((block !== nil)) { 13722 // offensive (both definitions) copy. 13723 offensive = self.slice(); 13724 permute.call(offensive, num, perm, 0, used, block); 13725 } 13726 else { 13727 permute.call(self, num, perm, 0, used, block); 13728 } 13729 } 13730 return self; 13731 }, -1); 13732 13733 $def(self, '$repeated_permutation', function $$repeated_permutation(n) { 13734 var $yield = $$repeated_permutation.$$p || nil, self = this, num = nil; 13735 13736 $$repeated_permutation.$$p = null; 13737 13738 num = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 13739 if (!($yield !== nil)) { 13740 return $send(self, 'enum_for', ["repeated_permutation", num], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 13741 13742 if ($truthy($rb_ge(num, 0))) { 13743 return self.$size()['$**'](num) 13744 } else { 13745 return 0 13746 }}, {$$s: self}) 13747 } 13748 function iterate(max, buffer, self) { 13749 if (buffer.length == max) { 13750 var copy = buffer.slice(); 13751 Opal.yield1($yield, copy); 13752 return; 13753 } 13754 for (var i = 0; i < self.length; i++) { 13755 buffer.push(self[i]); 13756 iterate(max, buffer, self); 13757 buffer.pop(); 13758 } 13759 } 13760 13761 iterate(num, [], self.slice()); 13762 return self; 13763 }); 13764 13765 $def(self, '$pop', function $$pop(count) { 13766 var self = this; 13767 $deny_frozen_access(self); 13768 if ($truthy(count === undefined)) { 13769 13770 if ($truthy(self.length === 0)) { 13771 return nil 13772 } return self.pop(); 13773 } count = $coerce_to(count, $$$('Integer'), 'to_int'); 13774 if ($truthy(count < 0)) { 13775 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 13776 } if ($truthy(self.length === 0)) { 13777 return [] 13778 } if ($truthy(count === 1)) { 13779 return [self.pop()]; 13780 } else if ($truthy(count > self.length)) { 13781 return self.splice(0, self.length); 13782 } else { 13783 return self.splice(self.length - count, self.length); 13784 } }, -1); 13785 13786 $def(self, '$product', function $$product($a) { 13787 var block = $$product.$$p || nil, $post_args, args, self = this; 13788 13789 $$product.$$p = null; 13790 $post_args = $slice(arguments); 13791 args = $post_args; 13792 13793 var result = (block !== nil) ? null : [], 13794 n = args.length + 1, 13795 counters = new Array(n), 13796 lengths = new Array(n), 13797 arrays = new Array(n), 13798 i, m, subarray, len, resultlen = 1; 13799 13800 arrays[0] = self; 13801 for (i = 1; i < n; i++) { 13802 arrays[i] = $coerce_to(args[i - 1], $$$('Array'), 'to_ary'); 13803 } 13804 13805 for (i = 0; i < n; i++) { 13806 len = arrays[i].length; 13807 if (len === 0) { 13808 return result || self; 13809 } 13810 resultlen *= len; 13811 if (resultlen > 2147483647) { 13812 $Kernel.$raise($$$('RangeError'), "too big to product"); 13813 } 13814 lengths[i] = len; 13815 counters[i] = 0; 13816 } 13817 13818 outer_loop: for (;;) { 13819 subarray = []; 13820 for (i = 0; i < n; i++) { 13821 subarray.push(arrays[i][counters[i]]); 13822 } 13823 if (result) { 13824 result.push(subarray); 13825 } else { 13826 Opal.yield1(block, subarray); 13827 } 13828 m = n - 1; 13829 counters[m]++; 13830 while (counters[m] === lengths[m]) { 13831 counters[m] = 0; 13832 if (--m < 0) break outer_loop; 13833 counters[m]++; 13834 } 13835 } 13836 13837 return result || self; 13838 }, -1); 13839 13840 $def(self, '$push', function $$push($a) { 13841 var $post_args, objects, self = this; 13842 13843 13844 $post_args = $slice(arguments); 13845 objects = $post_args; 13846 13847 $deny_frozen_access(self); 13848 13849 for (var i = 0, length = objects.length; i < length; i++) { 13850 self.push(objects[i]); 13851 } 13852 return self; 13853 }, -1); 13854 13855 $def(self, '$rassoc', function $$rassoc(object) { 13856 var self = this; 13857 13858 13859 for (var i = 0, length = self.length, item; i < length; i++) { 13860 item = self[i]; 13861 13862 if (item.length && item[1] !== undefined) { 13863 if ((item[1])['$=='](object)) { 13864 return item; 13865 } 13866 } 13867 } 13868 13869 return nil; 13870 13871 }); 13872 13873 $def(self, '$reject', function $$reject() { 13874 var block = $$reject.$$p || nil, self = this; 13875 13876 $$reject.$$p = null; 13877 if (!(block !== nil)) { 13878 return $send(self, 'enum_for', ["reject"], function $$36(){var self = $$36.$$s == null ? this : $$36.$$s; 13879 13880 return self.$size()}, {$$s: self}) 13881 } 13882 var result = []; 13883 13884 for (var i = 0, length = self.length, value; i < length; i++) { 13885 value = block(self[i]); 13886 13887 if (value === false || value === nil) { 13888 result.push(self[i]); 13889 } 13890 } 13891 return result; 13892 }); 13893 13894 $def(self, '$reject!', function $Array_reject$excl$37() { 13895 var block = $Array_reject$excl$37.$$p || nil, self = this, original = nil; 13896 13897 $Array_reject$excl$37.$$p = null; 13898 if (!(block !== nil)) { 13899 return $send(self, 'enum_for', ["reject!"], function $$38(){var self = $$38.$$s == null ? this : $$38.$$s; 13900 13901 return self.$size()}, {$$s: self}) 13902 } $deny_frozen_access(self); 13903 original = self.$length(); 13904 $send(self, 'delete_if', [], block.$to_proc()); 13905 if ($eqeq(self.$length(), original)) { 13906 return nil 13907 } else { 13908 return self 13909 } }); 13910 13911 $def(self, '$replace', function $$replace(other) { 13912 var self = this; 13913 13914 13915 $deny_frozen_access(self); 13916 other = ($eqeqeq($$$('Array'), other) ? (other.$to_a()) : (($coerce_to(other, $$$('Array'), 'to_ary')).$to_a())); 13917 13918 self.splice(0, self.length); 13919 self.push.apply(self, other); 13920 return self; 13921 }); 13922 13923 $def(self, '$reverse', function $$reverse() { 13924 var self = this; 13925 13926 return self.slice(0).reverse(); 13927 }); 13928 13929 $def(self, '$reverse!', function $Array_reverse$excl$39() { 13930 var self = this; 13931 13932 13933 $deny_frozen_access(self); 13934 return self.reverse(); }); 13935 13936 $def(self, '$reverse_each', function $$reverse_each() { 13937 var block = $$reverse_each.$$p || nil, self = this; 13938 13939 $$reverse_each.$$p = null; 13940 if (!(block !== nil)) { 13941 return $send(self, 'enum_for', ["reverse_each"], function $$40(){var self = $$40.$$s == null ? this : $$40.$$s; 13942 13943 return self.$size()}, {$$s: self}) 13944 } $send(self.$reverse(), 'each', [], block.$to_proc()); 13945 return self; 13946 }); 13947 13948 $def(self, '$rindex', function $$rindex(object) { 13949 var block = $$rindex.$$p || nil, self = this; 13950 13951 $$rindex.$$p = null; 13952 13953 var i, value; 13954 13955 if (object != null && block !== nil) { 13956 self.$warn("warning: given block not used"); 13957 } 13958 13959 if (object != null) { 13960 for (i = self.length - 1; i >= 0; i--) { 13961 if (i >= self.length) { 13962 break; 13963 } 13964 if ((self[i])['$=='](object)) { 13965 return i; 13966 } 13967 } 13968 } 13969 else if (block !== nil) { 13970 for (i = self.length - 1; i >= 0; i--) { 13971 if (i >= self.length) { 13972 break; 13973 } 13974 13975 value = block(self[i]); 13976 13977 if (value !== false && value !== nil) { 13978 return i; 13979 } 13980 } 13981 } 13982 else if (object == null) { 13983 return self.$enum_for("rindex"); 13984 } 13985 13986 return nil; 13987 }, -1); 13988 13989 $def(self, '$rotate', function $$rotate(n) { 13990 var self = this; 13991 13992 13993 if (n == null) n = 1; 13994 13995 var ary, idx, firstPart, lastPart; 13996 13997 n = $coerce_to(n, $$$('Integer'), 'to_int'); 13998 13999 if (self.length === 1) { 14000 return self.slice(); 14001 } 14002 if (self.length === 0) { 14003 return []; 14004 } 14005 14006 ary = self.slice(); 14007 idx = n % ary.length; 14008 14009 firstPart = ary.slice(idx); 14010 lastPart = ary.slice(0, idx); 14011 return firstPart.concat(lastPart); 14012 }, -1); 14013 14014 $def(self, '$rotate!', function $Array_rotate$excl$41(cnt) { 14015 var self = this, ary = nil; 14016 14017 14018 if (cnt == null) cnt = 1; 14019 14020 $deny_frozen_access(self); 14021 14022 if (self.length === 0 || self.length === 1) { 14023 return self; 14024 } 14025 cnt = $coerce_to(cnt, $$$('Integer'), 'to_int'); 14026 ary = self.$rotate(cnt); 14027 return self.$replace(ary); 14028 }, -1); 14029 (function($base, $super) { 14030 var self = $klass($base, $super, 'SampleRandom'); 14031 14032 var $proto = self.$$prototype; 14033 14034 $proto.rng = nil; 14035 14036 14037 $def(self, '$initialize', $assign_ivar("rng")); 14038 return $def(self, '$rand', function $$rand(size) { 14039 var self = this, random = nil; 14040 14041 14042 random = $coerce_to(self.rng.$rand(size), $$$('Integer'), 'to_int'); 14043 if ($truthy(random < 0)) { 14044 $Kernel.$raise($$$('RangeError'), "random value must be >= 0"); 14045 } if (!$truthy(random < size)) { 14046 $Kernel.$raise($$$('RangeError'), "random value must be less than Array size"); 14047 } return random; 14048 }); 14049 })(self, null); 14050 14051 $def(self, '$sample', function $$sample(count, options) { 14052 var self = this, o = nil, rng = nil; 14053 if ($truthy(count === undefined)) { 14054 return self.$at($Kernel.$rand(self.length)) 14055 } if ($truthy(options === undefined)) { 14056 if ($truthy((o = $Opal['$coerce_to?'](count, $$$('Hash'), "to_hash")))) { 14057 14058 options = o; 14059 count = nil; 14060 } else { 14061 14062 options = nil; 14063 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14064 } 14065 } else { 14066 14067 count = $coerce_to(count, $$$('Integer'), 'to_int'); 14068 options = $coerce_to(options, $$$('Hash'), 'to_hash'); 14069 } if (($truthy(count) && ($truthy(count < 0)))) { 14070 $Kernel.$raise($$$('ArgumentError'), "count must be greater than 0"); 14071 } if ($truthy(options)) { 14072 rng = options['$[]']("random"); 14073 } rng = (($truthy(rng) && ($truthy(rng['$respond_to?']("rand")))) ? ($$('SampleRandom').$new(rng)) : ($Kernel)); 14074 if (!$truthy(count)) { 14075 return self[rng.$rand(self.length)] 14076 } 14077 14078 var abandon, spin, result, i, j, k, targetIndex, oldValue; 14079 14080 if (count > self.length) { 14081 count = self.length; 14082 } 14083 14084 switch (count) { 14085 case 0: 14086 return []; 14087 case 1: 14088 return [self[rng.$rand(self.length)]]; 14089 case 2: 14090 i = rng.$rand(self.length); 14091 j = rng.$rand(self.length - 1); 14092 if (i <= j) { 14093 j++; 14094 } 14095 return [self[i], self[j]]; 14096 default: 14097 if (self.length / count > 3) { 14098 abandon = false; 14099 spin = 0; 14100 14101 result = $$('Array').$new(count); 14102 i = 1; 14103 14104 result[0] = rng.$rand(self.length); 14105 while (i < count) { 14106 k = rng.$rand(self.length); 14107 j = 0; 14108 14109 while (j < i) { 14110 while (k === result[j]) { 14111 spin++; 14112 if (spin > 100) { 14113 abandon = true; 14114 break; 14115 } 14116 k = rng.$rand(self.length); 14117 } 14118 if (abandon) { break; } 14119 14120 j++; 14121 } 14122 14123 if (abandon) { break; } 14124 14125 result[i] = k; 14126 14127 i++; 14128 } 14129 14130 if (!abandon) { 14131 i = 0; 14132 while (i < count) { 14133 result[i] = self[result[i]]; 14134 i++; 14135 } 14136 14137 return result; 14138 } 14139 } 14140 14141 result = self.slice(); 14142 14143 for (var c = 0; c < count; c++) { 14144 targetIndex = rng.$rand(self.length - c) + c; 14145 oldValue = result[c]; 14146 result[c] = result[targetIndex]; 14147 result[targetIndex] = oldValue; 14148 } 14149 14150 return count === self.length ? result : (result)['$[]'](0, count); 14151 } 14152 }, -1); 14153 14154 $def(self, '$select', function $$select() { 14155 var block = $$select.$$p || nil, self = this; 14156 14157 $$select.$$p = null; 14158 if (!(block !== nil)) { 14159 return $send(self, 'enum_for', ["select"], function $$42(){var self = $$42.$$s == null ? this : $$42.$$s; 14160 14161 return self.$size()}, {$$s: self}) 14162 } 14163 var result = []; 14164 14165 for (var i = 0, length = self.length, item, value; i < length; i++) { 14166 item = self[i]; 14167 14168 value = $yield1(block, item); 14169 14170 if ($truthy(value)) { 14171 result.push(item); 14172 } 14173 } 14174 14175 return result; 14176 }); 14177 14178 $def(self, '$select!', function $Array_select$excl$43() { 14179 var block = $Array_select$excl$43.$$p || nil, self = this; 14180 14181 $Array_select$excl$43.$$p = null; 14182 if (!(block !== nil)) { 14183 return $send(self, 'enum_for', ["select!"], function $$44(){var self = $$44.$$s == null ? this : $$44.$$s; 14184 14185 return self.$size()}, {$$s: self}) 14186 } 14187 $deny_frozen_access(self); 14188 14189 var original = self.length; 14190 $send(self, 'keep_if', [], block.$to_proc()); 14191 return self.length === original ? nil : self; 14192 }); 14193 14194 $def(self, '$shift', function $$shift(count) { 14195 var self = this; 14196 $deny_frozen_access(self); 14197 if ($truthy(count === undefined)) { 14198 14199 if ($truthy(self.length === 0)) { 14200 return nil 14201 } return shiftNoArg(self); 14202 } count = $coerce_to(count, $$$('Integer'), 'to_int'); 14203 if ($truthy(count < 0)) { 14204 $Kernel.$raise($$$('ArgumentError'), "negative array size"); 14205 } if ($truthy(self.length === 0)) { 14206 return [] 14207 } return self.splice(0, count); }, -1); 14208 14209 $def(self, '$shuffle', function $$shuffle(rng) { 14210 var self = this; 14211 return self.$dup().$to_a()['$shuffle!'](rng); 14212 }, -1); 14213 14214 $def(self, '$shuffle!', function $Array_shuffle$excl$45(rng) { 14215 var self = this; 14216 14217 $deny_frozen_access(self); 14218 14219 var randgen, i = self.length, j, tmp; 14220 14221 if (rng !== undefined) { 14222 rng = $Opal['$coerce_to?'](rng, $$$('Hash'), "to_hash"); 14223 14224 if (rng !== nil) { 14225 rng = rng['$[]']("random"); 14226 14227 if (rng !== nil && rng['$respond_to?']("rand")) { 14228 randgen = rng; 14229 } 14230 } 14231 } 14232 14233 while (i) { 14234 if (randgen) { 14235 j = randgen.$rand(i).$to_int(); 14236 14237 if (j < 0) { 14238 $Kernel.$raise($$$('RangeError'), "random number too small " + (j)); 14239 } 14240 14241 if (j >= i) { 14242 $Kernel.$raise($$$('RangeError'), "random number too big " + (j)); 14243 } 14244 } 14245 else { 14246 j = self.$rand(i); 14247 } 14248 14249 tmp = self[--i]; 14250 self[i] = self[j]; 14251 self[j] = tmp; 14252 } 14253 14254 return self; 14255 }, -1); 14256 14257 $def(self, '$slice!', function $Array_slice$excl$46(index, length) { 14258 var self = this, result = nil, range = nil, range_start = nil, range_end = nil, start = nil; 14259 $deny_frozen_access(self); 14260 result = nil; 14261 if ($truthy(length === undefined)) { 14262 if ($eqeqeq($$$('Range'), index)) { 14263 14264 range = index; 14265 result = self['$[]'](range); 14266 range_start = range.begin === nil ? 0 : $coerce_to(range.begin, $$$('Integer'), 'to_int'); 14267 range_end = range.end === nil ? -1 : $coerce_to(range.end, $$$('Integer'), 'to_int'); 14268 14269 if (range_start < 0) { 14270 range_start += self.length; 14271 } 14272 14273 if (range_end < 0) { 14274 range_end += self.length; 14275 } else if (range_end >= self.length) { 14276 range_end = self.length - 1; 14277 if (range.excl) { 14278 range_end += 1; 14279 } 14280 } 14281 14282 var range_length = range_end - range_start; 14283 if (range.excl && range.end !== nil) { 14284 range_end -= 1; 14285 } else { 14286 range_length += 1; 14287 } 14288 14289 if (range_start < self.length && range_start >= 0 && range_end < self.length && range_end >= 0 && range_length > 0) { 14290 self.splice(range_start, range_length); 14291 } 14292 } else { 14293 14294 start = $coerce_to(index, $$$('Integer'), 'to_int'); 14295 14296 if (start < 0) { 14297 start += self.length; 14298 } 14299 14300 if (start < 0 || start >= self.length) { 14301 return nil; 14302 } 14303 14304 result = self[start]; 14305 14306 if (start === 0) { 14307 self.shift(); 14308 } else { 14309 self.splice(start, 1); 14310 } 14311 } 14312 } else { 14313 14314 start = $coerce_to(index, $$$('Integer'), 'to_int'); 14315 length = $coerce_to(length, $$$('Integer'), 'to_int'); 14316 14317 if (length < 0) { 14318 return nil; 14319 } 14320 14321 result = self['$[]'](start, length); 14322 14323 if (start < 0) { 14324 start += self.length; 14325 } 14326 14327 if (start + length > self.length) { 14328 length = self.length - start; 14329 } 14330 14331 if (start < self.length && start >= 0) { 14332 self.splice(start, length); 14333 } 14334 } return result; 14335 }, -2); 14336 14337 $def(self, '$sort', function $$sort() { 14338 var block = $$sort.$$p || nil, self = this; 14339 14340 $$sort.$$p = null; 14341 if (!$truthy(self.length > 1)) { 14342 return self 14343 } 14344 if (block === nil) { 14345 block = function(a, b) { 14346 return (a)['$<=>'](b); 14347 }; 14348 } 14349 14350 return self.slice().sort(function(x, y) { 14351 var ret = block(x, y); 14352 14353 if (ret === nil) { 14354 $Kernel.$raise($$$('ArgumentError'), "comparison of " + ((x).$inspect()) + " with " + ((y).$inspect()) + " failed"); 14355 } 14356 14357 return $rb_gt(ret, 0) ? 1 : ($rb_lt(ret, 0) ? -1 : 0); 14358 }); 14359 }); 14360 14361 $def(self, '$sort!', function $Array_sort$excl$47() { 14362 var block = $Array_sort$excl$47.$$p || nil, self = this; 14363 14364 $Array_sort$excl$47.$$p = null; 14365 14366 $deny_frozen_access(self); 14367 14368 var result; 14369 14370 if ((block !== nil)) { 14371 result = $send((self.slice()), 'sort', [], block.$to_proc()); 14372 } 14373 else { 14374 result = (self.slice()).$sort(); 14375 } 14376 14377 self.length = 0; 14378 for(var i = 0, length = result.length; i < length; i++) { 14379 self.push(result[i]); 14380 } 14381 14382 return self; 14383 }); 14384 14385 $def(self, '$sort_by!', function $Array_sort_by$excl$48() { 14386 var block = $Array_sort_by$excl$48.$$p || nil, self = this; 14387 14388 $Array_sort_by$excl$48.$$p = null; 14389 if (!(block !== nil)) { 14390 return $send(self, 'enum_for', ["sort_by!"], function $$49(){var self = $$49.$$s == null ? this : $$49.$$s; 14391 14392 return self.$size()}, {$$s: self}) 14393 } $deny_frozen_access(self); 14394 return self.$replace($send(self, 'sort_by', [], block.$to_proc())); 14395 }); 14396 14397 $def(self, '$take', function $$take(count) { 14398 var self = this; 14399 14400 14401 if (count < 0) { 14402 $Kernel.$raise($$$('ArgumentError')); 14403 } 14404 14405 return self.slice(0, count); 14406 14407 }); 14408 14409 $def(self, '$take_while', function $$take_while() { 14410 var block = $$take_while.$$p || nil, self = this; 14411 14412 $$take_while.$$p = null; 14413 14414 var result = []; 14415 14416 for (var i = 0, length = self.length, item, value; i < length; i++) { 14417 item = self[i]; 14418 14419 value = block(item); 14420 14421 if (value === false || value === nil) { 14422 return result; 14423 } 14424 14425 result.push(item); 14426 } 14427 14428 return result; 14429 }); 14430 14431 $def(self, '$to_a', function $$to_a() { 14432 var self = this; 14433 14434 14435 if (self.$$class === Opal.Array) { 14436 return self; 14437 } 14438 else { 14439 return Opal.Array.$new(self); 14440 } 14441 14442 }); 14443 14444 $def(self, '$to_ary', $return_self); 14445 14446 $def(self, '$to_h', function $$to_h() { 14447 var block = $$to_h.$$p || nil, self = this, array = nil; 14448 14449 $$to_h.$$p = null; 14450 array = self; 14451 if ((block !== nil)) { 14452 array = $send(array, 'map', [], block.$to_proc()); 14453 } 14454 var i, len = array.length, ary, key, val, hash = $hash2([], {}); 14455 14456 for (i = 0; i < len; i++) { 14457 ary = $Opal['$coerce_to?'](array[i], $$$('Array'), "to_ary"); 14458 if (!ary.$$is_array) { 14459 $Kernel.$raise($$$('TypeError'), "wrong element type " + ((ary).$class()) + " at " + (i) + " (expected array)"); 14460 } 14461 if (ary.length !== 2) { 14462 $Kernel.$raise($$$('ArgumentError'), "wrong array length at " + (i) + " (expected 2, was " + ((ary).$length()) + ")"); 14463 } 14464 key = ary[0]; 14465 val = ary[1]; 14466 $hash_put(hash, key, val); 14467 } 14468 14469 return hash; 14470 }); 14471 14472 $def(self, '$transpose', function $$transpose() { 14473 var self = this, result = nil, max = nil; 14474 14475 14476 if ($truthy(self['$empty?']())) { 14477 return [] 14478 } result = []; 14479 max = nil; 14480 $send(self, 'each', [], function $$50(row){var $ret_or_1 = nil; 14481 14482 14483 if (row == null) row = nil; 14484 row = ($eqeqeq($$$('Array'), row) ? (row.$to_a()) : (($coerce_to(row, $$$('Array'), 'to_ary')).$to_a())); 14485 max = ($truthy(($ret_or_1 = max)) ? ($ret_or_1) : (row.length)); 14486 if ($neqeq(row.length, max)) { 14487 $Kernel.$raise($$$('IndexError'), "element size differs (" + (row.length) + " should be " + (max) + ")"); 14488 } return $send((row.length), 'times', [], function $$51(i){var $a, entry = nil; 14489 14490 14491 if (i == null) i = nil; 14492 entry = ($truthy(($ret_or_1 = result['$[]'](i))) ? ($ret_or_1) : (($a = [i, []], $send(result, '[]=', $a), $a[$a.length - 1]))); 14493 return entry['$<<'](row.$at(i));});}); 14494 return result; 14495 }); 14496 14497 $def(self, '$union', function $$union($a) { 14498 var $post_args, arrays, self = this; 14499 14500 14501 $post_args = $slice(arguments); 14502 arrays = $post_args; 14503 return $send(arrays, 'reduce', [self.$uniq()], function $$52(a, b){ 14504 14505 if (a == null) a = nil; 14506 if (b == null) b = nil; 14507 return a['$|'](b);}); 14508 }, -1); 14509 14510 $def(self, '$uniq', function $$uniq() { 14511 var block = $$uniq.$$p || nil, self = this; 14512 14513 $$uniq.$$p = null; 14514 14515 var hash = $hash2([], {}), i, length, item, key; 14516 14517 if (block === nil) { 14518 for (i = 0, length = self.length; i < length; i++) { 14519 item = self[i]; 14520 if ($hash_get(hash, item) === undefined) { 14521 $hash_put(hash, item, item); 14522 } 14523 } 14524 } 14525 else { 14526 for (i = 0, length = self.length; i < length; i++) { 14527 item = self[i]; 14528 key = $yield1(block, item); 14529 if ($hash_get(hash, key) === undefined) { 14530 $hash_put(hash, key, item); 14531 } 14532 } 14533 } 14534 14535 return (hash).$values(); 14536 }); 14537 14538 $def(self, '$uniq!', function $Array_uniq$excl$53() { 14539 var block = $Array_uniq$excl$53.$$p || nil, self = this; 14540 14541 $Array_uniq$excl$53.$$p = null; 14542 14543 $deny_frozen_access(self); 14544 14545 var original_length = self.length, hash = $hash2([], {}), i, length, item, key; 14546 14547 for (i = 0, length = original_length; i < length; i++) { 14548 item = self[i]; 14549 key = (block === nil ? item : $yield1(block, item)); 14550 14551 if ($hash_get(hash, key) === undefined) { 14552 $hash_put(hash, key, item); 14553 continue; 14554 } 14555 14556 self.splice(i, 1); 14557 length--; 14558 i--; 14559 } 14560 14561 return self.length === original_length ? nil : self; 14562 }); 14563 14564 $def(self, '$unshift', function $$unshift($a) { 14565 var $post_args, objects, self = this; 14566 14567 14568 $post_args = $slice(arguments); 14569 objects = $post_args; 14570 14571 $deny_frozen_access(self); 14572 14573 var selfLength = self.length; 14574 var objectsLength = objects.length; 14575 if (objectsLength == 0) return self; 14576 var index = selfLength - objectsLength; 14577 for (var i = 0; i < objectsLength; i++) { 14578 self.push(self[index + i]); 14579 } 14580 var len = selfLength - 1; 14581 while (len - objectsLength >= 0) { 14582 self[len] = self[len - objectsLength]; 14583 len--; 14584 } 14585 for (var j = 0; j < objectsLength; j++) { 14586 self[j] = objects[j]; 14587 } 14588 return self; 14589 }, -1); 14590 14591 $def(self, '$values_at', function $$values_at($a) { 14592 var $post_args, args, self = this, out = nil; 14593 14594 14595 $post_args = $slice(arguments); 14596 args = $post_args; 14597 out = []; 14598 $send(args, 'each', [], function $$54(elem){var self = $$54.$$s == null ? this : $$54.$$s, finish = nil, start = nil, i = nil; 14599 14600 14601 if (elem == null) elem = nil; 14602 if ($truthy(elem['$is_a?']($$$('Range')))) { 14603 14604 finish = elem.$end() === nil ? -1 : $coerce_to(elem.$end(), $$$('Integer'), 'to_int'); 14605 start = elem.$begin() === nil ? 0 : $coerce_to(elem.$begin(), $$$('Integer'), 'to_int'); 14606 14607 if (start < 0) { 14608 start = start + self.length; 14609 return nil; 14610 } 14611 14612 if (finish < 0) { 14613 finish = finish + self.length; 14614 } 14615 if (elem['$exclude_end?']() && elem.$end() !== nil) { 14616 finish--; 14617 } 14618 if (finish < start) { 14619 return nil; 14620 } 14621 return $send(start, 'upto', [finish], function $$55(i){var self = $$55.$$s == null ? this : $$55.$$s; 14622 14623 14624 if (i == null) i = nil; 14625 return out['$<<'](self.$at(i));}, {$$s: self}); 14626 } else { 14627 14628 i = $coerce_to(elem, $$$('Integer'), 'to_int'); 14629 return out['$<<'](self.$at(i)); 14630 }}, {$$s: self}); 14631 return out; 14632 }, -1); 14633 14634 $def(self, '$zip', function $$zip($a) { 14635 var block = $$zip.$$p || nil, $post_args, others, self = this, $ret_or_1 = nil; 14636 14637 $$zip.$$p = null; 14638 $post_args = $slice(arguments); 14639 others = $post_args; 14640 14641 var result = [], size = self.length, part, o, i, j, jj; 14642 14643 for (j = 0, jj = others.length; j < jj; j++) { 14644 o = others[j]; 14645 if (o.$$is_array) { 14646 continue; 14647 } 14648 if (o.$$is_range || o.$$is_enumerator) { 14649 others[j] = o.$take(size); 14650 continue; 14651 } 14652 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(); 14653 } 14654 14655 for (i = 0; i < size; i++) { 14656 part = [self[i]]; 14657 14658 for (j = 0, jj = others.length; j < jj; j++) { 14659 o = others[j][i]; 14660 14661 if (o == null) { 14662 o = nil; 14663 } 14664 14665 part[j + 1] = o; 14666 } 14667 14668 result[i] = part; 14669 } 14670 14671 if (block !== nil) { 14672 for (i = 0; i < size; i++) { 14673 Opal.yield1(block, result[i]); 14674 } 14675 14676 return nil; 14677 } 14678 14679 return result; 14680 }, -1); 14681 $defs(self, '$inherited', function $$inherited(klass) { 14682 14683 14684 klass.$$prototype.$to_a = function() { 14685 return this.slice(0, this.length); 14686 }; 14687 14688 }); 14689 14690 $def(self, '$instance_variables', function $$instance_variables() { 14691 var $yield = $$instance_variables.$$p || nil, self = this; 14692 14693 $$instance_variables.$$p = null; 14694 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; 14695 14696 14697 if (ivar == null) ivar = nil; 14698 if ($truthy(($ret_or_1 = /^@\d+$/.test(ivar)))) { 14699 return $ret_or_1 14700 } else { 14701 return ivar['$==']("@length") 14702 }}) 14703 }); 14704 14705 $def(self, '$pack', function $$pack($a) { 14706 14707 14708 $slice(arguments); 14709 return $Kernel.$raise("To use Array#pack, you must first require 'corelib/array/pack'."); 14710 }, -1); 14711 $alias(self, "append", "push"); 14712 $alias(self, "filter", "select"); 14713 $alias(self, "filter!", "select!"); 14714 $alias(self, "map", "collect"); 14715 $alias(self, "map!", "collect!"); 14716 $alias(self, "prepend", "unshift"); 14717 $alias(self, "size", "length"); 14718 $alias(self, "slice", "[]"); 14719 $alias(self, "to_s", "inspect"); 14720 $Opal.$pristine(self.$singleton_class(), "allocate"); 14721 return $Opal.$pristine(self, "copy_instance_variables", "initialize_dup"); 14722 })('::', Array, $nesting); 14723}; 14724 14725Opal.modules["corelib/hash"] = function(Opal) {/* Generated by Opal 1.7.3 */ 14726 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.$$$; 14727 14728 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?'); 14729 14730 self.$require("corelib/enumerable"); 14731 return (function($base, $super, $parent_nesting) { 14732 var self = $klass($base, $super, 'Hash'); 14733 14734 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 14735 14736 14737 self.$include($$$('Enumerable')); 14738 self.$$prototype.$$is_hash = true; 14739 $defs(self, '$[]', function $Hash_$$$1($a) { 14740 var $post_args, argv, self = this; 14741 14742 14743 $post_args = $slice(arguments); 14744 argv = $post_args; 14745 14746 var hash, argc = argv.length, i; 14747 14748 if (argc === 1) { 14749 hash = $Opal['$coerce_to?'](argv['$[]'](0), $$$('Hash'), "to_hash"); 14750 if (hash !== nil) { 14751 return self.$allocate()['$merge!'](hash); 14752 } 14753 14754 argv = $Opal['$coerce_to?'](argv['$[]'](0), $$$('Array'), "to_ary"); 14755 if (argv === nil) { 14756 $Kernel.$raise($$$('ArgumentError'), "odd number of arguments for Hash"); 14757 } 14758 14759 argc = argv.length; 14760 hash = self.$allocate(); 14761 14762 for (i = 0; i < argc; i++) { 14763 if (!argv[i].$$is_array) continue; 14764 switch(argv[i].length) { 14765 case 1: 14766 hash.$store(argv[i][0], nil); 14767 break; 14768 case 2: 14769 hash.$store(argv[i][0], argv[i][1]); 14770 break; 14771 default: 14772 $Kernel.$raise($$$('ArgumentError'), "invalid number of elements (" + (argv[i].length) + " for 1..2)"); 14773 } 14774 } 14775 14776 return hash; 14777 } 14778 14779 if (argc % 2 !== 0) { 14780 $Kernel.$raise($$$('ArgumentError'), "odd number of arguments for Hash"); 14781 } 14782 14783 hash = self.$allocate(); 14784 14785 for (i = 0; i < argc; i += 2) { 14786 hash.$store(argv[i], argv[i + 1]); 14787 } 14788 14789 return hash; 14790 }, -1); 14791 $defs(self, '$allocate', function $$allocate() { 14792 var self = this; 14793 14794 14795 var hash = new self.$$constructor(); 14796 14797 $hash_init(hash); 14798 14799 hash.$$none = nil; 14800 hash.$$proc = nil; 14801 14802 return hash; 14803 14804 }); 14805 $defs(self, '$try_convert', function $$try_convert(obj) { 14806 14807 return $Opal['$coerce_to?'](obj, $$$('Hash'), "to_hash") 14808 }); 14809 14810 $def(self, '$initialize', function $$initialize(defaults) { 14811 var block = $$initialize.$$p || nil, self = this; 14812 14813 $$initialize.$$p = null; 14814 14815 $deny_frozen_access(self); 14816 14817 if (defaults !== undefined && block !== nil) { 14818 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (1 for 0)"); 14819 } 14820 self.$$none = (defaults === undefined ? nil : defaults); 14821 self.$$proc = block; 14822 14823 return self; 14824 }, -1); 14825 14826 $def(self, '$==', function $Hash_$eq_eq$2(other) { 14827 var self = this; 14828 14829 14830 if (self === other) { 14831 return true; 14832 } 14833 14834 if (!other.$$is_hash) { 14835 return false; 14836 } 14837 14838 if (self.$$keys.length !== other.$$keys.length) { 14839 return false; 14840 } 14841 14842 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, other_value; i < length; i++) { 14843 key = keys[i]; 14844 14845 if (key.$$is_string) { 14846 value = self.$$smap[key]; 14847 other_value = other.$$smap[key]; 14848 } else { 14849 value = key.value; 14850 other_value = $hash_get(other, key.key); 14851 } 14852 14853 if (other_value === undefined || !value['$eql?'](other_value)) { 14854 return false; 14855 } 14856 } 14857 14858 return true; 14859 14860 }); 14861 14862 $def(self, '$>=', function $Hash_$gt_eq$3(other) { 14863 var self = this, result = nil; 14864 14865 14866 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 14867 14868 if (self.$$keys.length < other.$$keys.length) { 14869 return false 14870 } 14871 result = true; 14872 $send(other, 'each', [], function $$4(other_key, other_val){var self = $$4.$$s == null ? this : $$4.$$s, val = nil; 14873 14874 14875 if (other_key == null) other_key = nil; 14876 if (other_val == null) other_val = nil; 14877 val = self.$fetch(other_key, null); 14878 14879 if (val == null || val !== other_val) { 14880 result = false; 14881 return; 14882 } 14883}, {$$s: self}); 14884 return result; 14885 }); 14886 14887 $def(self, '$>', function $Hash_$gt$5(other) { 14888 var self = this; 14889 14890 14891 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 14892 14893 if (self.$$keys.length <= other.$$keys.length) { 14894 return false 14895 } 14896 return $rb_ge(self, other); 14897 }); 14898 14899 $def(self, '$<', function $Hash_$lt$6(other) { 14900 var self = this; 14901 14902 14903 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 14904 return $rb_gt(other, self); 14905 }); 14906 14907 $def(self, '$<=', function $Hash_$lt_eq$7(other) { 14908 var self = this; 14909 14910 14911 other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 14912 return $rb_ge(other, self); 14913 }); 14914 14915 $def(self, '$[]', function $Hash_$$$8(key) { 14916 var self = this; 14917 14918 14919 var value = $hash_get(self, key); 14920 14921 if (value !== undefined) { 14922 return value; 14923 } 14924 14925 return self.$default(key); 14926 14927 }); 14928 14929 $def(self, '$[]=', function $Hash_$$$eq$9(key, value) { 14930 var self = this; 14931 14932 14933 $deny_frozen_access(self); 14934 14935 $hash_put(self, key, value); 14936 return value; 14937 14938 }); 14939 14940 $def(self, '$assoc', function $$assoc(object) { 14941 var self = this; 14942 14943 14944 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 14945 key = keys[i]; 14946 14947 if (key.$$is_string) { 14948 if ((key)['$=='](object)) { 14949 return [key, self.$$smap[key]]; 14950 } 14951 } else { 14952 if ((key.key)['$=='](object)) { 14953 return [key.key, key.value]; 14954 } 14955 } 14956 } 14957 14958 return nil; 14959 14960 }); 14961 14962 $def(self, '$clear', function $$clear() { 14963 var self = this; 14964 14965 14966 $deny_frozen_access(self); 14967 14968 $hash_init(self); 14969 return self; 14970 14971 }); 14972 14973 $def(self, '$clone', function $$clone() { 14974 var self = this; 14975 14976 14977 var hash = new self.$$class(); 14978 14979 $hash_init(hash); 14980 Opal.hash_clone(self, hash); 14981 14982 return hash; 14983 14984 }); 14985 14986 $def(self, '$compact', function $$compact() { 14987 var self = this; 14988 14989 14990 var hash = $hash(); 14991 14992 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 14993 key = keys[i]; 14994 14995 if (key.$$is_string) { 14996 value = self.$$smap[key]; 14997 } else { 14998 value = key.value; 14999 key = key.key; 15000 } 15001 15002 if (value !== nil) { 15003 $hash_put(hash, key, value); 15004 } 15005 } 15006 15007 return hash; 15008 15009 }); 15010 15011 $def(self, '$compact!', function $Hash_compact$excl$10() { 15012 var self = this; 15013 15014 15015 $deny_frozen_access(self); 15016 15017 var changes_were_made = false; 15018 15019 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15020 key = keys[i]; 15021 15022 if (key.$$is_string) { 15023 value = self.$$smap[key]; 15024 } else { 15025 value = key.value; 15026 key = key.key; 15027 } 15028 15029 if (value === nil) { 15030 if ($hash_delete(self, key) !== undefined) { 15031 changes_were_made = true; 15032 length--; 15033 i--; 15034 } 15035 } 15036 } 15037 15038 return changes_were_made ? self : nil; 15039 15040 }); 15041 15042 $def(self, '$compare_by_identity', function $$compare_by_identity() { 15043 var self = this; 15044 15045 15046 $deny_frozen_access(self); 15047 15048 var i, ii, key, keys = self.$$keys, identity_hash; 15049 15050 if (self.$$by_identity) return self; 15051 if (self.$$keys.length === 0) { 15052 self.$$by_identity = true; 15053 return self; 15054 } 15055 15056 identity_hash = $hash2([], {}).$compare_by_identity(); 15057 for(i = 0, ii = keys.length; i < ii; i++) { 15058 key = keys[i]; 15059 if (!key.$$is_string) key = key.key; 15060 $hash_put(identity_hash, key, $hash_get(self, key)); 15061 } 15062 15063 self.$$by_identity = true; 15064 self.$$map = identity_hash.$$map; 15065 self.$$smap = identity_hash.$$smap; 15066 return self; 15067 15068 }); 15069 15070 $def(self, '$compare_by_identity?', function $Hash_compare_by_identity$ques$11() { 15071 var self = this; 15072 15073 return self.$$by_identity === true; 15074 }); 15075 15076 $def(self, '$default', function $Hash_default$12(key) { 15077 var self = this; 15078 15079 if (key !== undefined && self.$$proc !== nil && self.$$proc !== undefined) { 15080 return self.$$proc.$call(self, key); 15081 } 15082 if (self.$$none === undefined) { 15083 return nil; 15084 } 15085 return self.$$none; 15086 }, -1); 15087 15088 $def(self, '$default=', function $Hash_default$eq$13(object) { 15089 var self = this; 15090 15091 15092 $deny_frozen_access(self); 15093 15094 self.$$proc = nil; 15095 self.$$none = object; 15096 15097 return object; 15098 15099 }); 15100 15101 $def(self, '$default_proc', function $$default_proc() { 15102 var self = this; 15103 15104 15105 if (self.$$proc !== undefined) { 15106 return self.$$proc; 15107 } 15108 return nil; 15109 15110 }); 15111 15112 $def(self, '$default_proc=', function $Hash_default_proc$eq$14(default_proc) { 15113 var self = this; 15114 15115 15116 $deny_frozen_access(self); 15117 15118 var proc = default_proc; 15119 15120 if (proc !== nil) { 15121 proc = $Opal['$coerce_to!'](proc, $$$('Proc'), "to_proc"); 15122 15123 if ((proc)['$lambda?']() && (proc).$arity().$abs() !== 2) { 15124 $Kernel.$raise($$$('TypeError'), "default_proc takes two arguments"); 15125 } 15126 } 15127 15128 self.$$none = nil; 15129 self.$$proc = proc; 15130 15131 return default_proc; 15132 15133 }); 15134 15135 $def(self, '$delete', function $Hash_delete$15(key) { 15136 var block = $Hash_delete$15.$$p || nil, self = this; 15137 15138 $Hash_delete$15.$$p = null; 15139 15140 $deny_frozen_access(self); 15141 var value = $hash_delete(self, key); 15142 15143 if (value !== undefined) { 15144 return value; 15145 } 15146 15147 if (block !== nil) { 15148 return Opal.yield1(block, key); 15149 } 15150 15151 return nil; 15152 }); 15153 15154 $def(self, '$delete_if', function $$delete_if() { 15155 var block = $$delete_if.$$p || nil, self = this; 15156 15157 $$delete_if.$$p = null; 15158 if (!$truthy(block)) { 15159 return $send(self, 'enum_for', ["delete_if"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 15160 15161 return self.$size()}, {$$s: self}) 15162 } 15163 $deny_frozen_access(self); 15164 15165 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15166 key = keys[i]; 15167 15168 if (key.$$is_string) { 15169 value = self.$$smap[key]; 15170 } else { 15171 value = key.value; 15172 key = key.key; 15173 } 15174 15175 obj = block(key, value); 15176 15177 if (obj !== false && obj !== nil) { 15178 if ($hash_delete(self, key) !== undefined) { 15179 length--; 15180 i--; 15181 } 15182 } 15183 } 15184 15185 return self; 15186 }); 15187 15188 $def(self, '$dig', function $$dig(key, $a) { 15189 var $post_args, keys, self = this, item = nil; 15190 15191 15192 $post_args = $slice(arguments, 1); 15193 keys = $post_args; 15194 item = self['$[]'](key); 15195 15196 if (item === nil || keys.length === 0) { 15197 return item; 15198 } 15199 if (!$truthy(item['$respond_to?']("dig"))) { 15200 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method"); 15201 } return $send(item, 'dig', $to_a(keys)); 15202 }, -2); 15203 15204 $def(self, '$each', function $$each() { 15205 var block = $$each.$$p || nil, self = this; 15206 15207 $$each.$$p = null; 15208 if (!$truthy(block)) { 15209 return $send(self, 'enum_for', ["each"], function $$17(){var self = $$17.$$s == null ? this : $$17.$$s; 15210 15211 return self.$size()}, {$$s: self}) 15212 } 15213 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key, value; i < length; i++) { 15214 key = keys[i]; 15215 15216 if (key.$$is_string) { 15217 value = self.$$smap[key]; 15218 } else { 15219 value = key.value; 15220 key = key.key; 15221 } 15222 15223 $yield1(block, [key, value]); 15224 } 15225 15226 return self; 15227 }); 15228 15229 $def(self, '$each_key', function $$each_key() { 15230 var block = $$each_key.$$p || nil, self = this; 15231 15232 $$each_key.$$p = null; 15233 if (!$truthy(block)) { 15234 return $send(self, 'enum_for', ["each_key"], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 15235 15236 return self.$size()}, {$$s: self}) 15237 } 15238 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) { 15239 key = keys[i]; 15240 15241 block(key.$$is_string ? key : key.key); 15242 } 15243 15244 return self; 15245 }); 15246 15247 $def(self, '$each_value', function $$each_value() { 15248 var block = $$each_value.$$p || nil, self = this; 15249 15250 $$each_value.$$p = null; 15251 if (!$truthy(block)) { 15252 return $send(self, 'enum_for', ["each_value"], function $$19(){var self = $$19.$$s == null ? this : $$19.$$s; 15253 15254 return self.$size()}, {$$s: self}) 15255 } 15256 for (var i = 0, keys = self.$$keys.slice(), length = keys.length, key; i < length; i++) { 15257 key = keys[i]; 15258 15259 block(key.$$is_string ? self.$$smap[key] : key.value); 15260 } 15261 15262 return self; 15263 }); 15264 15265 $def(self, '$empty?', function $Hash_empty$ques$20() { 15266 var self = this; 15267 15268 return self.$$keys.length === 0; 15269 }); 15270 15271 $def(self, '$except', function $$except($a) { 15272 var $post_args, keys, self = this; 15273 15274 15275 $post_args = $slice(arguments); 15276 keys = $post_args; 15277 return $send(self.$dup(), 'except!', $to_a(keys)); 15278 }, -1); 15279 15280 $def(self, '$except!', function $Hash_except$excl$21($a) { 15281 var $post_args, keys, self = this; 15282 15283 15284 $post_args = $slice(arguments); 15285 keys = $post_args; 15286 $send(keys, 'each', [], function $$22(key){var self = $$22.$$s == null ? this : $$22.$$s; 15287 15288 15289 if (key == null) key = nil; 15290 return self.$delete(key);}, {$$s: self}); 15291 return self; 15292 }, -1); 15293 15294 $def(self, '$fetch', function $$fetch(key, defaults) { 15295 var block = $$fetch.$$p || nil, self = this; 15296 15297 $$fetch.$$p = null; 15298 15299 var value = $hash_get(self, key); 15300 15301 if (value !== undefined) { 15302 return value; 15303 } 15304 15305 if (block !== nil) { 15306 return block(key); 15307 } 15308 15309 if (defaults !== undefined) { 15310 return defaults; 15311 } 15312 return $Kernel.$raise($$$('KeyError').$new("key not found: " + (key.$inspect()), $hash2(["key", "receiver"], {"key": key, "receiver": self}))); 15313 }, -2); 15314 15315 $def(self, '$fetch_values', function $$fetch_values($a) { 15316 var block = $$fetch_values.$$p || nil, $post_args, keys, self = this; 15317 15318 $$fetch_values.$$p = null; 15319 $post_args = $slice(arguments); 15320 keys = $post_args; 15321 return $send(keys, 'map', [], function $$23(key){var self = $$23.$$s == null ? this : $$23.$$s; 15322 15323 15324 if (key == null) key = nil; 15325 return $send(self, 'fetch', [key], block.$to_proc());}, {$$s: self}); 15326 }, -1); 15327 15328 $def(self, '$flatten', function $$flatten(level) { 15329 var self = this; 15330 15331 15332 if (level == null) level = 1; 15333 level = $Opal['$coerce_to!'](level, $$$('Integer'), "to_int"); 15334 15335 var result = []; 15336 15337 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15338 key = keys[i]; 15339 15340 if (key.$$is_string) { 15341 value = self.$$smap[key]; 15342 } else { 15343 value = key.value; 15344 key = key.key; 15345 } 15346 15347 result.push(key); 15348 15349 if (value.$$is_array) { 15350 if (level === 1) { 15351 result.push(value); 15352 continue; 15353 } 15354 15355 result = result.concat((value).$flatten(level - 2)); 15356 continue; 15357 } 15358 15359 result.push(value); 15360 } 15361 15362 return result; 15363 }, -1); 15364 15365 $def(self, '$freeze', function $$freeze() { 15366 var self = this; 15367 15368 15369 if ($truthy(self['$frozen?']())) { 15370 return self 15371 } return $freeze(self); }); 15372 15373 $def(self, '$has_key?', function $Hash_has_key$ques$24(key) { 15374 var self = this; 15375 15376 return $hash_get(self, key) !== undefined; 15377 }); 15378 15379 $def(self, '$has_value?', function $Hash_has_value$ques$25(value) { 15380 var self = this; 15381 15382 15383 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 15384 key = keys[i]; 15385 15386 if (((key.$$is_string ? self.$$smap[key] : key.value))['$=='](value)) { 15387 return true; 15388 } 15389 } 15390 15391 return false; 15392 15393 }); 15394 15395 $def(self, '$hash', function $$hash() { 15396 var self = this; 15397 15398 15399 var top = (Opal.hash_ids === undefined), 15400 hash_id = self.$object_id(), 15401 result = ['Hash'], 15402 key, item; 15403 15404 try { 15405 if (top) { 15406 Opal.hash_ids = Object.create(null); 15407 } 15408 15409 if (Opal[hash_id]) { 15410 return 'self'; 15411 } 15412 15413 for (key in Opal.hash_ids) { 15414 item = Opal.hash_ids[key]; 15415 if (self['$eql?'](item)) { 15416 return 'self'; 15417 } 15418 } 15419 15420 Opal.hash_ids[hash_id] = self; 15421 15422 for (var i = 0, keys = self.$$keys, length = keys.length; i < length; i++) { 15423 key = keys[i]; 15424 15425 if (key.$$is_string) { 15426 result.push([key, self.$$smap[key].$hash()]); 15427 } else { 15428 result.push([key.key_hash, key.value.$hash()]); 15429 } 15430 } 15431 15432 return result.sort().join(); 15433 15434 } finally { 15435 if (top) { 15436 Opal.hash_ids = undefined; 15437 } 15438 } 15439 15440 }); 15441 15442 $def(self, '$index', function $$index(object) { 15443 var self = this; 15444 15445 15446 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15447 key = keys[i]; 15448 15449 if (key.$$is_string) { 15450 value = self.$$smap[key]; 15451 } else { 15452 value = key.value; 15453 key = key.key; 15454 } 15455 15456 if ((value)['$=='](object)) { 15457 return key; 15458 } 15459 } 15460 15461 return nil; 15462 15463 }); 15464 15465 $def(self, '$indexes', function $$indexes($a) { 15466 var $post_args, args, self = this; 15467 15468 15469 $post_args = $slice(arguments); 15470 args = $post_args; 15471 15472 var result = []; 15473 15474 for (var i = 0, length = args.length, key, value; i < length; i++) { 15475 key = args[i]; 15476 value = $hash_get(self, key); 15477 15478 if (value === undefined) { 15479 result.push(self.$default()); 15480 continue; 15481 } 15482 15483 result.push(value); 15484 } 15485 15486 return result; 15487 }, -1); 15488 var inspect_ids; 15489 15490 $def(self, '$inspect', function $$inspect() { 15491 var self = this; 15492 15493 15494 15495 var top = (inspect_ids === undefined), 15496 hash_id = self.$object_id(), 15497 result = []; 15498 15499 return (function() { try { 15500 15501 15502 if (top) { 15503 inspect_ids = {}; 15504 } 15505 15506 if (inspect_ids.hasOwnProperty(hash_id)) { 15507 return '{...}'; 15508 } 15509 15510 inspect_ids[hash_id] = true; 15511 15512 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15513 key = keys[i]; 15514 15515 if (key.$$is_string) { 15516 value = self.$$smap[key]; 15517 } else { 15518 value = key.value; 15519 key = key.key; 15520 } 15521 15522 key = $$('Opal').$inspect(key); 15523 value = $$('Opal').$inspect(value); 15524 15525 result.push(key + '=>' + value); 15526 } 15527 15528 return '{' + result.join(', ') + '}'; 15529 ; 15530 return nil; 15531 } finally { 15532 if (top) inspect_ids = undefined; 15533 } })(); }); 15534 15535 $def(self, '$invert', function $$invert() { 15536 var self = this; 15537 15538 15539 var hash = $hash(); 15540 15541 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15542 key = keys[i]; 15543 15544 if (key.$$is_string) { 15545 value = self.$$smap[key]; 15546 } else { 15547 value = key.value; 15548 key = key.key; 15549 } 15550 15551 $hash_put(hash, value, key); 15552 } 15553 15554 return hash; 15555 15556 }); 15557 15558 $def(self, '$keep_if', function $$keep_if() { 15559 var block = $$keep_if.$$p || nil, self = this; 15560 15561 $$keep_if.$$p = null; 15562 if (!$truthy(block)) { 15563 return $send(self, 'enum_for', ["keep_if"], function $$26(){var self = $$26.$$s == null ? this : $$26.$$s; 15564 15565 return self.$size()}, {$$s: self}) 15566 } 15567 $deny_frozen_access(self); 15568 15569 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15570 key = keys[i]; 15571 15572 if (key.$$is_string) { 15573 value = self.$$smap[key]; 15574 } else { 15575 value = key.value; 15576 key = key.key; 15577 } 15578 15579 obj = block(key, value); 15580 15581 if (obj === false || obj === nil) { 15582 if ($hash_delete(self, key) !== undefined) { 15583 length--; 15584 i--; 15585 } 15586 } 15587 } 15588 15589 return self; 15590 }); 15591 15592 $def(self, '$keys', function $$keys() { 15593 var self = this; 15594 15595 15596 var result = []; 15597 15598 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 15599 key = keys[i]; 15600 15601 if (key.$$is_string) { 15602 result.push(key); 15603 } else { 15604 result.push(key.key); 15605 } 15606 } 15607 15608 return result; 15609 15610 }); 15611 15612 $def(self, '$length', function $$length() { 15613 var self = this; 15614 15615 return self.$$keys.length; 15616 }); 15617 15618 $def(self, '$merge', function $$merge($a) { 15619 var block = $$merge.$$p || nil, $post_args, others, self = this; 15620 15621 $$merge.$$p = null; 15622 $post_args = $slice(arguments); 15623 others = $post_args; 15624 return $send(self.$dup(), 'merge!', $to_a(others), block.$to_proc()); 15625 }, -1); 15626 15627 $def(self, '$merge!', function $Hash_merge$excl$27($a) { 15628 var block = $Hash_merge$excl$27.$$p || nil, $post_args, others, self = this; 15629 15630 $Hash_merge$excl$27.$$p = null; 15631 $post_args = $slice(arguments); 15632 others = $post_args; 15633 15634 $deny_frozen_access(self); 15635 var i, j, other, other_keys, length, key, value, other_value; 15636 for (i = 0; i < others.length; ++i) { 15637 other = $Opal['$coerce_to!'](others[i], $$$('Hash'), "to_hash"); 15638 other_keys = other.$$keys, length = other_keys.length; 15639 15640 if (block === nil) { 15641 for (j = 0; j < length; j++) { 15642 key = other_keys[j]; 15643 15644 if (key.$$is_string) { 15645 other_value = other.$$smap[key]; 15646 } else { 15647 other_value = key.value; 15648 key = key.key; 15649 } 15650 15651 $hash_put(self, key, other_value); 15652 } 15653 } else { 15654 for (j = 0; j < length; j++) { 15655 key = other_keys[j]; 15656 15657 if (key.$$is_string) { 15658 other_value = other.$$smap[key]; 15659 } else { 15660 other_value = key.value; 15661 key = key.key; 15662 } 15663 15664 value = $hash_get(self, key); 15665 15666 if (value === undefined) { 15667 $hash_put(self, key, other_value); 15668 continue; 15669 } 15670 15671 $hash_put(self, key, block(key, value, other_value)); 15672 } 15673 } 15674 } 15675 15676 return self; 15677 }, -1); 15678 15679 $def(self, '$rassoc', function $$rassoc(object) { 15680 var self = this; 15681 15682 15683 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15684 key = keys[i]; 15685 15686 if (key.$$is_string) { 15687 value = self.$$smap[key]; 15688 } else { 15689 value = key.value; 15690 key = key.key; 15691 } 15692 15693 if ((value)['$=='](object)) { 15694 return [key, value]; 15695 } 15696 } 15697 15698 return nil; 15699 15700 }); 15701 15702 $def(self, '$rehash', function $$rehash() { 15703 var self = this; 15704 15705 15706 $deny_frozen_access(self); 15707 Opal.hash_rehash(self); 15708 return self; 15709 15710 }); 15711 15712 $def(self, '$reject', function $$reject() { 15713 var block = $$reject.$$p || nil, self = this; 15714 15715 $$reject.$$p = null; 15716 if (!$truthy(block)) { 15717 return $send(self, 'enum_for', ["reject"], function $$28(){var self = $$28.$$s == null ? this : $$28.$$s; 15718 15719 return self.$size()}, {$$s: self}) 15720 } 15721 var hash = $hash(); 15722 15723 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15724 key = keys[i]; 15725 15726 if (key.$$is_string) { 15727 value = self.$$smap[key]; 15728 } else { 15729 value = key.value; 15730 key = key.key; 15731 } 15732 15733 obj = block(key, value); 15734 15735 if (obj === false || obj === nil) { 15736 $hash_put(hash, key, value); 15737 } 15738 } 15739 15740 return hash; 15741 }); 15742 15743 $def(self, '$reject!', function $Hash_reject$excl$29() { 15744 var block = $Hash_reject$excl$29.$$p || nil, self = this; 15745 15746 $Hash_reject$excl$29.$$p = null; 15747 if (!$truthy(block)) { 15748 return $send(self, 'enum_for', ["reject!"], function $$30(){var self = $$30.$$s == null ? this : $$30.$$s; 15749 15750 return self.$size()}, {$$s: self}) 15751 } 15752 $deny_frozen_access(self); 15753 15754 var changes_were_made = false; 15755 15756 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15757 key = keys[i]; 15758 15759 if (key.$$is_string) { 15760 value = self.$$smap[key]; 15761 } else { 15762 value = key.value; 15763 key = key.key; 15764 } 15765 15766 obj = block(key, value); 15767 15768 if (obj !== false && obj !== nil) { 15769 if ($hash_delete(self, key) !== undefined) { 15770 changes_were_made = true; 15771 length--; 15772 i--; 15773 } 15774 } 15775 } 15776 15777 return changes_were_made ? self : nil; 15778 }); 15779 15780 $def(self, '$replace', function $$replace(other) { 15781 var self = this; 15782 15783 15784 $deny_frozen_access(self); other = $Opal['$coerce_to!'](other, $$$('Hash'), "to_hash"); 15785 15786 $hash_init(self); 15787 15788 for (var i = 0, other_keys = other.$$keys, length = other_keys.length, key, value, other_value; i < length; i++) { 15789 key = other_keys[i]; 15790 15791 if (key.$$is_string) { 15792 other_value = other.$$smap[key]; 15793 } else { 15794 other_value = key.value; 15795 key = key.key; 15796 } 15797 15798 $hash_put(self, key, other_value); 15799 } 15800 if ($truthy(other.$default_proc())) { 15801 self['$default_proc='](other.$default_proc()); 15802 } else { 15803 self['$default='](other.$default()); 15804 } return self; 15805 }); 15806 15807 $def(self, '$select', function $$select() { 15808 var block = $$select.$$p || nil, self = this; 15809 15810 $$select.$$p = null; 15811 if (!$truthy(block)) { 15812 return $send(self, 'enum_for', ["select"], function $$31(){var self = $$31.$$s == null ? this : $$31.$$s; 15813 15814 return self.$size()}, {$$s: self}) 15815 } 15816 var hash = $hash(); 15817 15818 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15819 key = keys[i]; 15820 15821 if (key.$$is_string) { 15822 value = self.$$smap[key]; 15823 } else { 15824 value = key.value; 15825 key = key.key; 15826 } 15827 15828 obj = block(key, value); 15829 15830 if (obj !== false && obj !== nil) { 15831 $hash_put(hash, key, value); 15832 } 15833 } 15834 15835 return hash; 15836 }); 15837 15838 $def(self, '$select!', function $Hash_select$excl$32() { 15839 var block = $Hash_select$excl$32.$$p || nil, self = this; 15840 15841 $Hash_select$excl$32.$$p = null; 15842 if (!$truthy(block)) { 15843 return $send(self, 'enum_for', ["select!"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 15844 15845 return self.$size()}, {$$s: self}) 15846 } 15847 $deny_frozen_access(self); 15848 15849 var result = nil; 15850 15851 for (var i = 0, keys = self.$$keys, length = keys.length, key, value, obj; i < length; i++) { 15852 key = keys[i]; 15853 15854 if (key.$$is_string) { 15855 value = self.$$smap[key]; 15856 } else { 15857 value = key.value; 15858 key = key.key; 15859 } 15860 15861 obj = block(key, value); 15862 15863 if (obj === false || obj === nil) { 15864 if ($hash_delete(self, key) !== undefined) { 15865 length--; 15866 i--; 15867 } 15868 result = self; 15869 } 15870 } 15871 15872 return result; 15873 }); 15874 15875 $def(self, '$shift', function $$shift() { 15876 var self = this; 15877 15878 15879 $deny_frozen_access(self); 15880 var keys = self.$$keys, 15881 key; 15882 15883 if (keys.length > 0) { 15884 key = keys[0]; 15885 15886 key = key.$$is_string ? key : key.key; 15887 15888 return [key, $hash_delete(self, key)]; 15889 } 15890 15891 return nil; 15892 15893 }); 15894 15895 $def(self, '$slice', function $$slice($a) { 15896 var $post_args, keys, self = this; 15897 15898 15899 $post_args = $slice(arguments); 15900 keys = $post_args; 15901 15902 var result = $hash(); 15903 15904 for (var i = 0, length = keys.length; i < length; i++) { 15905 var key = keys[i], value = $hash_get(self, key); 15906 15907 if (value !== undefined) { 15908 $hash_put(result, key, value); 15909 } 15910 } 15911 15912 return result; 15913 }, -1); 15914 15915 $def(self, '$to_a', function $$to_a() { 15916 var self = this; 15917 15918 15919 var result = []; 15920 15921 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15922 key = keys[i]; 15923 15924 if (key.$$is_string) { 15925 value = self.$$smap[key]; 15926 } else { 15927 value = key.value; 15928 key = key.key; 15929 } 15930 15931 result.push([key, value]); 15932 } 15933 15934 return result; 15935 15936 }); 15937 15938 $def(self, '$to_h', function $$to_h() { 15939 var block = $$to_h.$$p || nil, self = this; 15940 15941 $$to_h.$$p = null; 15942 if ((block !== nil)) { 15943 return $send(self, 'map', [], block.$to_proc()).$to_h() 15944 } 15945 if (self.$$class === Opal.Hash) { 15946 return self; 15947 } 15948 15949 var hash = new Opal.Hash(); 15950 15951 $hash_init(hash); 15952 Opal.hash_clone(self, hash); 15953 15954 return hash; 15955 }); 15956 15957 $def(self, '$to_hash', $return_self); 15958 15959 $def(self, '$to_proc', function $$to_proc() { 15960 var self = this; 15961 15962 return $send(self, 'proc', [], function $$34(key){var self = $$34.$$s == null ? this : $$34.$$s; 15963 15964 if (key == null) { 15965 $Kernel.$raise($$$('ArgumentError'), "no key given"); 15966 } 15967 return self['$[]'](key);}, {$$arity: -1, $$s: self}) 15968 }); 15969 15970 $def(self, '$transform_keys', function $$transform_keys() { 15971 var block = $$transform_keys.$$p || nil, self = this; 15972 15973 $$transform_keys.$$p = null; 15974 if (!$truthy(block)) { 15975 return $send(self, 'enum_for', ["transform_keys"], function $$35(){var self = $$35.$$s == null ? this : $$35.$$s; 15976 15977 return self.$size()}, {$$s: self}) 15978 } 15979 var result = $hash(); 15980 15981 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 15982 key = keys[i]; 15983 15984 if (key.$$is_string) { 15985 value = self.$$smap[key]; 15986 } else { 15987 value = key.value; 15988 key = key.key; 15989 } 15990 15991 key = $yield1(block, key); 15992 15993 $hash_put(result, key, value); 15994 } 15995 15996 return result; 15997 }); 15998 15999 $def(self, '$transform_keys!', function $Hash_transform_keys$excl$36() { 16000 var block = $Hash_transform_keys$excl$36.$$p || nil, self = this; 16001 16002 $Hash_transform_keys$excl$36.$$p = null; 16003 if (!$truthy(block)) { 16004 return $send(self, 'enum_for', ["transform_keys!"], function $$37(){var self = $$37.$$s == null ? this : $$37.$$s; 16005 16006 return self.$size()}, {$$s: self}) 16007 } 16008 $deny_frozen_access(self); 16009 16010 var keys = Opal.slice(self.$$keys), 16011 i, length = keys.length, key, value, new_key; 16012 16013 for (i = 0; i < length; i++) { 16014 key = keys[i]; 16015 16016 if (key.$$is_string) { 16017 value = self.$$smap[key]; 16018 } else { 16019 value = key.value; 16020 key = key.key; 16021 } 16022 16023 new_key = $yield1(block, key); 16024 16025 $hash_delete(self, key); 16026 $hash_put(self, new_key, value); 16027 } 16028 16029 return self; 16030 }); 16031 16032 $def(self, '$transform_values', function $$transform_values() { 16033 var block = $$transform_values.$$p || nil, self = this; 16034 16035 $$transform_values.$$p = null; 16036 if (!$truthy(block)) { 16037 return $send(self, 'enum_for', ["transform_values"], function $$38(){var self = $$38.$$s == null ? this : $$38.$$s; 16038 16039 return self.$size()}, {$$s: self}) 16040 } 16041 var result = $hash(); 16042 16043 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16044 key = keys[i]; 16045 16046 if (key.$$is_string) { 16047 value = self.$$smap[key]; 16048 } else { 16049 value = key.value; 16050 key = key.key; 16051 } 16052 16053 value = $yield1(block, value); 16054 16055 $hash_put(result, key, value); 16056 } 16057 16058 return result; 16059 }); 16060 16061 $def(self, '$transform_values!', function $Hash_transform_values$excl$39() { 16062 var block = $Hash_transform_values$excl$39.$$p || nil, self = this; 16063 16064 $Hash_transform_values$excl$39.$$p = null; 16065 if (!$truthy(block)) { 16066 return $send(self, 'enum_for', ["transform_values!"], function $$40(){var self = $$40.$$s == null ? this : $$40.$$s; 16067 16068 return self.$size()}, {$$s: self}) 16069 } 16070 $deny_frozen_access(self); 16071 16072 for (var i = 0, keys = self.$$keys, length = keys.length, key, value; i < length; i++) { 16073 key = keys[i]; 16074 16075 if (key.$$is_string) { 16076 value = self.$$smap[key]; 16077 } else { 16078 value = key.value; 16079 key = key.key; 16080 } 16081 16082 value = $yield1(block, value); 16083 16084 $hash_put(self, key, value); 16085 } 16086 16087 return self; 16088 }); 16089 16090 $def(self, '$values', function $$values() { 16091 var self = this; 16092 16093 16094 var result = []; 16095 16096 for (var i = 0, keys = self.$$keys, length = keys.length, key; i < length; i++) { 16097 key = keys[i]; 16098 16099 if (key.$$is_string) { 16100 result.push(self.$$smap[key]); 16101 } else { 16102 result.push(key.value); 16103 } 16104 } 16105 16106 return result; 16107 16108 }); 16109 $alias(self, "dup", "clone"); 16110 $alias(self, "each_pair", "each"); 16111 $alias(self, "eql?", "=="); 16112 $alias(self, "filter", "select"); 16113 $alias(self, "filter!", "select!"); 16114 $alias(self, "include?", "has_key?"); 16115 $alias(self, "indices", "indexes"); 16116 $alias(self, "key", "index"); 16117 $alias(self, "key?", "has_key?"); 16118 $alias(self, "member?", "has_key?"); 16119 $alias(self, "size", "length"); 16120 $alias(self, "store", "[]="); 16121 $alias(self, "to_s", "inspect"); 16122 $alias(self, "update", "merge!"); 16123 $alias(self, "value?", "has_value?"); 16124 return $alias(self, "values_at", "indexes"); 16125 })('::', null, $nesting); 16126}; 16127 16128Opal.modules["corelib/number"] = function(Opal) {/* Generated by Opal 1.7.3 */ 16129 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.$$$; 16130 16131 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?'); 16132 16133 self.$require("corelib/numeric"); 16134 (function($base, $super, $parent_nesting) { 16135 var self = $klass($base, $super, 'Number'); 16136 16137 16138 $Opal.$bridge(Number, self); 16139 Opal.prop(self.$$prototype, '$$is_number', true); 16140 self.$$is_number_class = true; 16141 (function(self, $parent_nesting) { 16142 16143 16144 16145 $def(self, '$allocate', function $$allocate() { 16146 var self = this; 16147 16148 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 16149 }); 16150 16151 16152 Opal.udef(self, '$' + "new"); return nil; })(Opal.get_singleton_class(self)); 16153 16154 $def(self, '$coerce', function $$coerce(other) { 16155 var self = this; 16156 16157 16158 if (other === nil) { 16159 $Kernel.$raise($$$('TypeError'), "can't convert " + (other.$class()) + " into Float"); 16160 } 16161 else if (other.$$is_string) { 16162 return [$Kernel.$Float(other), self]; 16163 } 16164 else if (other['$respond_to?']("to_f")) { 16165 return [$Opal['$coerce_to!'](other, $$$('Float'), "to_f"), self]; 16166 } 16167 else if (other.$$is_number) { 16168 return [other, self]; 16169 } 16170 else { 16171 $Kernel.$raise($$$('TypeError'), "can't convert " + (other.$class()) + " into Float"); 16172 } 16173 16174 }); 16175 16176 $def(self, '$__id__', function $$__id__() { 16177 var self = this; 16178 16179 return (self * 2) + 1; 16180 }); 16181 16182 $def(self, '$+', function $Number_$plus$1(other) { 16183 var self = this; 16184 16185 16186 if (other.$$is_number) { 16187 return self + other; 16188 } 16189 else { 16190 return self.$__coerced__("+", other); 16191 } 16192 16193 }); 16194 16195 $def(self, '$-', function $Number_$minus$2(other) { 16196 var self = this; 16197 16198 16199 if (other.$$is_number) { 16200 return self - other; 16201 } 16202 else { 16203 return self.$__coerced__("-", other); 16204 } 16205 16206 }); 16207 16208 $def(self, '$*', function $Number_$$3(other) { 16209 var self = this; 16210 16211 16212 if (other.$$is_number) { 16213 return self * other; 16214 } 16215 else { 16216 return self.$__coerced__("*", other); 16217 } 16218 16219 }); 16220 16221 $def(self, '$/', function $Number_$slash$4(other) { 16222 var self = this; 16223 16224 16225 if (other.$$is_number) { 16226 return self / other; 16227 } 16228 else { 16229 return self.$__coerced__("/", other); 16230 } 16231 16232 }); 16233 16234 $def(self, '$%', function $Number_$percent$5(other) { 16235 var self = this; 16236 16237 16238 if (other.$$is_number) { 16239 if (other == -Infinity) { 16240 return other; 16241 } 16242 else if (other == 0) { 16243 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0"); 16244 } 16245 else if (other < 0 || self < 0) { 16246 return (self % other + other) % other; 16247 } 16248 else { 16249 return self % other; 16250 } 16251 } 16252 else { 16253 return self.$__coerced__("%", other); 16254 } 16255 16256 }); 16257 16258 $def(self, '$&', function $Number_$$6(other) { 16259 var self = this; 16260 16261 16262 if (other.$$is_number) { 16263 return self & other; 16264 } 16265 else { 16266 return self.$__coerced__("&", other); 16267 } 16268 16269 }); 16270 16271 $def(self, '$|', function $Number_$$7(other) { 16272 var self = this; 16273 16274 16275 if (other.$$is_number) { 16276 return self | other; 16277 } 16278 else { 16279 return self.$__coerced__("|", other); 16280 } 16281 16282 }); 16283 16284 $def(self, '$^', function $Number_$$8(other) { 16285 var self = this; 16286 16287 16288 if (other.$$is_number) { 16289 return self ^ other; 16290 } 16291 else { 16292 return self.$__coerced__("^", other); 16293 } 16294 16295 }); 16296 16297 $def(self, '$<', function $Number_$lt$9(other) { 16298 var self = this; 16299 16300 16301 if (other.$$is_number) { 16302 return self < other; 16303 } 16304 else { 16305 return self.$__coerced__("<", other); 16306 } 16307 16308 }); 16309 16310 $def(self, '$<=', function $Number_$lt_eq$10(other) { 16311 var self = this; 16312 16313 16314 if (other.$$is_number) { 16315 return self <= other; 16316 } 16317 else { 16318 return self.$__coerced__("<=", other); 16319 } 16320 16321 }); 16322 16323 $def(self, '$>', function $Number_$gt$11(other) { 16324 var self = this; 16325 16326 16327 if (other.$$is_number) { 16328 return self > other; 16329 } 16330 else { 16331 return self.$__coerced__(">", other); 16332 } 16333 16334 }); 16335 16336 $def(self, '$>=', function $Number_$gt_eq$12(other) { 16337 var self = this; 16338 16339 16340 if (other.$$is_number) { 16341 return self >= other; 16342 } 16343 else { 16344 return self.$__coerced__(">=", other); 16345 } 16346 16347 }); 16348 16349 var spaceship_operator = function(self, other) { 16350 if (other.$$is_number) { 16351 if (isNaN(self) || isNaN(other)) { 16352 return nil; 16353 } 16354 16355 if (self > other) { 16356 return 1; 16357 } else if (self < other) { 16358 return -1; 16359 } else { 16360 return 0; 16361 } 16362 } 16363 else { 16364 return self.$__coerced__("<=>", other); 16365 } 16366 } 16367 ; 16368 16369 $def(self, '$<=>', function $Number_$lt_eq_gt$13(other) { 16370 var self = this; 16371 16372 try { 16373 return spaceship_operator(self, other); 16374 } catch ($err) { 16375 if (Opal.rescue($err, [$$$('ArgumentError')])) { 16376 try { 16377 return nil 16378 } finally { Opal.pop_exception(); } 16379 } else { throw $err; } 16380 } 16381 }); 16382 16383 $def(self, '$<<', function $Number_$lt$lt$14(count) { 16384 var self = this; 16385 16386 16387 count = $Opal['$coerce_to!'](count, $$$('Integer'), "to_int"); 16388 return count > 0 ? self << count : self >> -count; 16389 }); 16390 16391 $def(self, '$>>', function $Number_$gt$gt$15(count) { 16392 var self = this; 16393 16394 16395 count = $Opal['$coerce_to!'](count, $$$('Integer'), "to_int"); 16396 return count > 0 ? self >> count : self << -count; 16397 }); 16398 16399 $def(self, '$[]', function $Number_$$$16(bit) { 16400 var self = this; 16401 16402 16403 bit = $Opal['$coerce_to!'](bit, $$$('Integer'), "to_int"); 16404 16405 if (bit < 0) { 16406 return 0; 16407 } 16408 if (bit >= 32) { 16409 return self < 0 ? 1 : 0; 16410 } 16411 return (self >> bit) & 1; 16412 }); 16413 16414 $def(self, '$+@', function $Number_$plus$$17() { 16415 var self = this; 16416 16417 return +self; 16418 }); 16419 16420 $def(self, '$-@', function $Number_$minus$$18() { 16421 var self = this; 16422 16423 return -self; 16424 }); 16425 16426 $def(self, '$~', function $Number_$$19() { 16427 var self = this; 16428 16429 return ~self; 16430 }); 16431 16432 $def(self, '$**', function $Number_$$$20(other) { 16433 var self = this; 16434 16435 if ($eqeqeq($$$('Integer'), other)) { 16436 if (($not($$$('Integer')['$==='](self)) || ($truthy($rb_gt(other, 0))))) { 16437 return Math.pow(self, other); 16438 } else { 16439 return $$$('Rational').$new(self, 1)['$**'](other) 16440 } 16441 } else if (($rb_lt(self, 0) && (($eqeqeq($$$('Float'), other) || ($eqeqeq($$$('Rational'), other)))))) { 16442 return $$$('Complex').$new(self, 0)['$**'](other.$to_f()) 16443 } else if ($truthy(other.$$is_number != null)) { 16444 return Math.pow(self, other); 16445 } else { 16446 return self.$__coerced__("**", other) 16447 } 16448 }); 16449 16450 $def(self, '$==', function $Number_$eq_eq$21(other) { 16451 var self = this; 16452 16453 16454 if (other.$$is_number) { 16455 return self.valueOf() === other.valueOf(); 16456 } 16457 else if (other['$respond_to?']("==")) { 16458 return other['$=='](self); 16459 } 16460 else { 16461 return false; 16462 } 16463 16464 }); 16465 $alias(self, "===", "=="); 16466 16467 $def(self, '$abs', function $$abs() { 16468 var self = this; 16469 16470 return Math.abs(self); 16471 }); 16472 16473 $def(self, '$abs2', function $$abs2() { 16474 var self = this; 16475 16476 return Math.abs(self * self); 16477 }); 16478 16479 $def(self, '$allbits?', function $Number_allbits$ques$22(mask) { 16480 var self = this; 16481 16482 16483 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 16484 return (self & mask) == mask; }); 16485 16486 $def(self, '$anybits?', function $Number_anybits$ques$23(mask) { 16487 var self = this; 16488 16489 16490 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 16491 return (self & mask) !== 0; }); 16492 16493 $def(self, '$angle', function $$angle() { 16494 var self = this; 16495 16496 16497 if ($truthy(self['$nan?']())) { 16498 return self 16499 } 16500 if (self == 0) { 16501 if (1 / self > 0) { 16502 return 0; 16503 } 16504 else { 16505 return Math.PI; 16506 } 16507 } 16508 else if (self < 0) { 16509 return Math.PI; 16510 } 16511 else { 16512 return 0; 16513 } 16514 }); 16515 16516 $def(self, '$bit_length', function $$bit_length() { 16517 var self = this; 16518 16519 16520 if (!$eqeqeq($$$('Integer'), self)) { 16521 $Kernel.$raise($$$('NoMethodError').$new("undefined method `bit_length` for " + (self) + ":Float", "bit_length")); 16522 } 16523 if (self === 0 || self === -1) { 16524 return 0; 16525 } 16526 16527 var result = 0, 16528 value = self < 0 ? ~self : self; 16529 16530 while (value != 0) { 16531 result += 1; 16532 value >>>= 1; 16533 } 16534 16535 return result; 16536 }); 16537 16538 $def(self, '$ceil', function $$ceil(ndigits) { 16539 var self = this; 16540 16541 16542 if (ndigits == null) ndigits = 0; 16543 16544 var f = self.$to_f(); 16545 16546 if (f % 1 === 0 && ndigits >= 0) { 16547 return f; 16548 } 16549 16550 var factor = Math.pow(10, ndigits), 16551 result = Math.ceil(f * factor) / factor; 16552 16553 if (f % 1 === 0) { 16554 result = Math.round(result); 16555 } 16556 16557 return result; 16558 }, -1); 16559 16560 $def(self, '$chr', function $$chr(encoding) { 16561 var self = this; 16562 return Opal.enc(String.fromCharCode(self), encoding || "BINARY"); }, -1); 16563 16564 $def(self, '$denominator', function $$denominator() { 16565 var $yield = $$denominator.$$p || nil, self = this; 16566 16567 $$denominator.$$p = null; 16568 if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 16569 return 1 16570 } else { 16571 return $send2(self, $find_super(self, 'denominator', $$denominator, false, true), 'denominator', [], $yield) 16572 } 16573 }); 16574 16575 $def(self, '$downto', function $$downto(stop) { 16576 var block = $$downto.$$p || nil, self = this; 16577 16578 $$downto.$$p = null; 16579 if (!(block !== nil)) { 16580 return $send(self, 'enum_for', ["downto", stop], function $$24(){var self = $$24.$$s == null ? this : $$24.$$s; 16581 16582 16583 if (!$eqeqeq($$$('Numeric'), stop)) { 16584 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed"); 16585 } if ($truthy($rb_gt(stop, self))) { 16586 return 0 16587 } else { 16588 return $rb_plus($rb_minus(self, stop), 1) 16589 }}, {$$s: self}) 16590 } 16591 if (!stop.$$is_number) { 16592 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed"); 16593 } 16594 for (var i = self; i >= stop; i--) { 16595 block(i); 16596 } 16597 return self; 16598 }); 16599 16600 $def(self, '$equal?', function $Number_equal$ques$25(other) { 16601 var self = this, $ret_or_1 = nil; 16602 16603 if ($truthy(($ret_or_1 = self['$=='](other)))) { 16604 return $ret_or_1 16605 } else { 16606 return isNaN(self) && isNaN(other); 16607 } 16608 }); 16609 16610 $def(self, '$even?', function $Number_even$ques$26() { 16611 var self = this; 16612 16613 return self % 2 === 0; 16614 }); 16615 16616 $def(self, '$floor', function $$floor(ndigits) { 16617 var self = this; 16618 16619 16620 if (ndigits == null) ndigits = 0; 16621 16622 var f = self.$to_f(); 16623 16624 if (f % 1 === 0 && ndigits >= 0) { 16625 return f; 16626 } 16627 16628 var factor = Math.pow(10, ndigits), 16629 result = Math.floor(f * factor) / factor; 16630 16631 if (f % 1 === 0) { 16632 result = Math.round(result); 16633 } 16634 16635 return result; 16636 }, -1); 16637 16638 $def(self, '$gcd', function $$gcd(other) { 16639 var self = this; 16640 16641 16642 if (!$eqeqeq($$$('Integer'), other)) { 16643 $Kernel.$raise($$$('TypeError'), "not an integer"); 16644 } 16645 var min = Math.abs(self), 16646 max = Math.abs(other); 16647 16648 while (min > 0) { 16649 var tmp = min; 16650 16651 min = max % min; 16652 max = tmp; 16653 } 16654 16655 return max; 16656 }); 16657 16658 $def(self, '$gcdlcm', function $$gcdlcm(other) { 16659 var self = this; 16660 16661 return [self.$gcd(other), self.$lcm(other)] 16662 }); 16663 16664 $def(self, '$integer?', function $Number_integer$ques$27() { 16665 var self = this; 16666 16667 return self % 1 === 0; 16668 }); 16669 16670 $def(self, '$is_a?', function $Number_is_a$ques$28(klass) { 16671 var $yield = $Number_is_a$ques$28.$$p || nil, self = this; 16672 16673 $Number_is_a$ques$28.$$p = null; 16674 16675 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 16676 return true 16677 } if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 16678 return true 16679 } if (($eqeq(klass, $$$('Float')) && ($eqeqeq($$$('Float'), self)))) { 16680 return true 16681 } return $send2(self, $find_super(self, 'is_a?', $Number_is_a$ques$28, false, true), 'is_a?', [klass], $yield); 16682 }); 16683 16684 $def(self, '$instance_of?', function $Number_instance_of$ques$29(klass) { 16685 var $yield = $Number_instance_of$ques$29.$$p || nil, self = this; 16686 16687 $Number_instance_of$ques$29.$$p = null; 16688 16689 if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 16690 return true 16691 } if (($eqeq(klass, $$$('Integer')) && ($eqeqeq($$$('Integer'), self)))) { 16692 return true 16693 } if (($eqeq(klass, $$$('Float')) && ($eqeqeq($$$('Float'), self)))) { 16694 return true 16695 } return $send2(self, $find_super(self, 'instance_of?', $Number_instance_of$ques$29, false, true), 'instance_of?', [klass], $yield); 16696 }); 16697 16698 $def(self, '$lcm', function $$lcm(other) { 16699 var self = this; 16700 16701 16702 if (!$eqeqeq($$$('Integer'), other)) { 16703 $Kernel.$raise($$$('TypeError'), "not an integer"); 16704 } 16705 if (self == 0 || other == 0) { 16706 return 0; 16707 } 16708 else { 16709 return Math.abs(self * other / self.$gcd(other)); 16710 } 16711 }); 16712 16713 $def(self, '$next', function $$next() { 16714 var self = this; 16715 16716 return self + 1; 16717 }); 16718 16719 $def(self, '$nobits?', function $Number_nobits$ques$30(mask) { 16720 var self = this; 16721 16722 16723 mask = $Opal['$coerce_to!'](mask, $$$('Integer'), "to_int"); 16724 return (self & mask) == 0; }); 16725 16726 $def(self, '$nonzero?', function $Number_nonzero$ques$31() { 16727 var self = this; 16728 16729 return self == 0 ? nil : self; 16730 }); 16731 16732 $def(self, '$numerator', function $$numerator() { 16733 var $yield = $$numerator.$$p || nil, self = this; 16734 16735 $$numerator.$$p = null; 16736 if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 16737 return self 16738 } else { 16739 return $send2(self, $find_super(self, 'numerator', $$numerator, false, true), 'numerator', [], $yield) 16740 } 16741 }); 16742 16743 $def(self, '$odd?', function $Number_odd$ques$32() { 16744 var self = this; 16745 16746 return self % 2 !== 0; 16747 }); 16748 16749 $def(self, '$ord', $return_self); 16750 16751 $def(self, '$pow', function $$pow(b, m) { 16752 var self = this; 16753 16754 if (self == 0) { 16755 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0"); 16756 } 16757 16758 if (m === undefined) { 16759 return self['$**'](b); 16760 } else { 16761 if (!($$$('Integer')['$==='](b))) { 16762 $Kernel.$raise($$$('TypeError'), "Integer#pow() 2nd argument not allowed unless a 1st argument is integer"); 16763 } 16764 16765 if (b < 0) { 16766 $Kernel.$raise($$$('TypeError'), "Integer#pow() 1st argument cannot be negative when 2nd argument specified"); 16767 } 16768 16769 if (!($$$('Integer')['$==='](m))) { 16770 $Kernel.$raise($$$('TypeError'), "Integer#pow() 2nd argument not allowed unless all arguments are integers"); 16771 } 16772 16773 if (m === 0) { 16774 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0"); 16775 } 16776 16777 return self['$**'](b)['$%'](m) 16778 } 16779 }, -2); 16780 16781 $def(self, '$pred', function $$pred() { 16782 var self = this; 16783 16784 return self - 1; 16785 }); 16786 16787 $def(self, '$quo', function $$quo(other) { 16788 var $yield = $$quo.$$p || nil, self = this; 16789 16790 $$quo.$$p = null; 16791 if ($eqeqeq($$$('Integer'), self)) { 16792 return $send2(self, $find_super(self, 'quo', $$quo, false, true), 'quo', [other], $yield) 16793 } else { 16794 return $rb_divide(self, other) 16795 } 16796 }); 16797 16798 $def(self, '$rationalize', function $$rationalize(eps) { 16799 var $a, $b, self = this, f = nil, n = nil; 16800 16801 if (arguments.length > 1) { 16802 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 16803 } 16804 if ($eqeqeq($$$('Integer'), self)) { 16805 return $$$('Rational').$new(self, 1) 16806 } else if ($truthy(self['$infinite?']())) { 16807 return $Kernel.$raise($$$('FloatDomainError'), "Infinity") 16808 } else if ($truthy(self['$nan?']())) { 16809 return $Kernel.$raise($$$('FloatDomainError'), "NaN") 16810 } else if ($truthy(eps == null)) { 16811 16812 $b = $$$('Math').$frexp(self), $a = $to_ary($b), (f = ($a[0] == null ? nil : $a[0])), (n = ($a[1] == null ? nil : $a[1])); 16813 f = $$$('Math').$ldexp(f, $$$($$$('Float'), 'MANT_DIG')).$to_i(); 16814 n = $rb_minus(n, $$$($$$('Float'), 'MANT_DIG')); 16815 return $$$('Rational').$new($rb_times(2, f), (1)['$<<']($rb_minus(1, n))).$rationalize($$$('Rational').$new(1, (1)['$<<']($rb_minus(1, n)))); 16816 } else { 16817 return self.$to_r().$rationalize(eps) 16818 } }, -1); 16819 16820 $def(self, '$remainder', function $$remainder(y) { 16821 var self = this; 16822 16823 return $rb_minus(self, $rb_times(y, $rb_divide(self, y).$truncate())) 16824 }); 16825 16826 $def(self, '$round', function $$round(ndigits) { 16827 var $a, $b, self = this, exp = nil; 16828 if ($eqeqeq($$$('Integer'), self)) { 16829 16830 if ($truthy(ndigits == null)) { 16831 return self 16832 } if (($eqeqeq($$$('Float'), ndigits) && ($truthy(ndigits['$infinite?']())))) { 16833 $Kernel.$raise($$$('RangeError'), "Infinity"); 16834 } ndigits = $Opal['$coerce_to!'](ndigits, $$$('Integer'), "to_int"); 16835 if ($truthy($rb_lt(ndigits, $$$($$$('Integer'), 'MIN')))) { 16836 $Kernel.$raise($$$('RangeError'), "out of bounds"); 16837 } if ($truthy(ndigits >= 0)) { 16838 return self 16839 } ndigits = ndigits['$-@'](); 16840 16841 if (0.415241 * ndigits - 0.125 > self.$size()) { 16842 return 0; 16843 } 16844 16845 var f = Math.pow(10, ndigits), 16846 x = Math.floor((Math.abs(self) + f / 2) / f) * f; 16847 16848 return self < 0 ? -x : x; 16849 } else { 16850 16851 if (($truthy(self['$nan?']()) && ($truthy(ndigits == null)))) { 16852 $Kernel.$raise($$$('FloatDomainError'), "NaN"); 16853 } ndigits = $Opal['$coerce_to!'](ndigits || 0, $$$('Integer'), "to_int"); 16854 if ($truthy($rb_le(ndigits, 0))) { 16855 if ($truthy(self['$nan?']())) { 16856 $Kernel.$raise($$$('RangeError'), "NaN"); 16857 } else if ($truthy(self['$infinite?']())) { 16858 $Kernel.$raise($$$('FloatDomainError'), "Infinity"); 16859 } 16860 } else if ($eqeq(ndigits, 0)) { 16861 return Math.round(self) 16862 } else if (($truthy(self['$nan?']()) || ($truthy(self['$infinite?']())))) { 16863 return self 16864 } $b = $$$('Math').$frexp(self), $a = $to_ary($b), (($a[0] == null ? nil : $a[0])), (exp = ($a[1] == null ? nil : $a[1])); 16865 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))))))) { 16866 return self 16867 } if ($truthy($rb_lt(ndigits, ($truthy($rb_gt(exp, 0)) ? ($rb_plus($rb_divide(exp, 3), 1)) : ($rb_divide(exp, 4)))['$-@']()))) { 16868 return 0 16869 } return Math.round(self * Math.pow(10, ndigits)) / Math.pow(10, ndigits); } }, -1); 16870 16871 $def(self, '$times', function $$times() { 16872 var block = $$times.$$p || nil, self = this; 16873 16874 $$times.$$p = null; 16875 if (!$truthy(block)) { 16876 return $send(self, 'enum_for', ["times"], function $$33(){var self = $$33.$$s == null ? this : $$33.$$s; 16877 16878 return self}, {$$s: self}) 16879 } 16880 for (var i = 0; i < self; i++) { 16881 block(i); 16882 } 16883 return self; 16884 }); 16885 16886 $def(self, '$to_f', $return_self); 16887 16888 $def(self, '$to_i', function $$to_i() { 16889 var self = this; 16890 16891 return self < 0 ? Math.ceil(self) : Math.floor(self); 16892 }); 16893 16894 $def(self, '$to_r', function $$to_r() { 16895 var $a, $b, self = this, f = nil, e = nil; 16896 16897 if ($eqeqeq($$$('Integer'), self)) { 16898 return $$$('Rational').$new(self, 1) 16899 } else { 16900 16901 $b = $$$('Math').$frexp(self), $a = $to_ary($b), (f = ($a[0] == null ? nil : $a[0])), (e = ($a[1] == null ? nil : $a[1])); 16902 f = $$$('Math').$ldexp(f, $$$($$$('Float'), 'MANT_DIG')).$to_i(); 16903 e = $rb_minus(e, $$$($$$('Float'), 'MANT_DIG')); 16904 return $rb_times(f, $$$($$$('Float'), 'RADIX')['$**'](e)).$to_r(); 16905 } 16906 }); 16907 16908 $def(self, '$to_s', function $$to_s(base) { 16909 var self = this; 16910 16911 16912 if (base == null) base = 10; 16913 base = $Opal['$coerce_to!'](base, $$$('Integer'), "to_int"); 16914 if (($truthy($rb_lt(base, 2)) || ($truthy($rb_gt(base, 36))))) { 16915 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)); 16916 } if (($eqeq(self, 0) && ($truthy(1/self === -Infinity)))) { 16917 return "-0.0" 16918 } return self.toString(base); }, -1); 16919 16920 $def(self, '$truncate', function $$truncate(ndigits) { 16921 var self = this; 16922 16923 16924 if (ndigits == null) ndigits = 0; 16925 16926 var f = self.$to_f(); 16927 16928 if (f % 1 === 0 && ndigits >= 0) { 16929 return f; 16930 } 16931 16932 var factor = Math.pow(10, ndigits), 16933 result = parseInt(f * factor, 10) / factor; 16934 16935 if (f % 1 === 0) { 16936 result = Math.round(result); 16937 } 16938 16939 return result; 16940 }, -1); 16941 16942 $def(self, '$digits', function $$digits(base) { 16943 var self = this; 16944 16945 16946 if (base == null) base = 10; 16947 if ($rb_lt(self, 0)) { 16948 $Kernel.$raise($$$($$$('Math'), 'DomainError'), "out of domain"); 16949 } base = $Opal['$coerce_to!'](base, $$$('Integer'), "to_int"); 16950 if ($truthy($rb_lt(base, 2))) { 16951 $Kernel.$raise($$$('ArgumentError'), "invalid radix " + (base)); 16952 } 16953 if (self != parseInt(self)) $Kernel.$raise($$$('NoMethodError'), "undefined method `digits' for " + (self.$inspect())); 16954 16955 var value = self, result = []; 16956 16957 if (self == 0) { 16958 return [0]; 16959 } 16960 16961 while (value != 0) { 16962 result.push(value % base); 16963 value = parseInt(value / base, 10); 16964 } 16965 16966 return result; 16967 }, -1); 16968 16969 $def(self, '$divmod', function $$divmod(other) { 16970 var $yield = $$divmod.$$p || nil, self = this; 16971 16972 $$divmod.$$p = null; 16973 if (($truthy(self['$nan?']()) || ($truthy(other['$nan?']())))) { 16974 return $Kernel.$raise($$$('FloatDomainError'), "NaN") 16975 } else if ($truthy(self['$infinite?']())) { 16976 return $Kernel.$raise($$$('FloatDomainError'), "Infinity") 16977 } else { 16978 return $send2(self, $find_super(self, 'divmod', $$divmod, false, true), 'divmod', [other], $yield) 16979 } 16980 }); 16981 16982 $def(self, '$upto', function $$upto(stop) { 16983 var block = $$upto.$$p || nil, self = this; 16984 16985 $$upto.$$p = null; 16986 if (!(block !== nil)) { 16987 return $send(self, 'enum_for', ["upto", stop], function $$34(){var self = $$34.$$s == null ? this : $$34.$$s; 16988 16989 16990 if (!$eqeqeq($$$('Numeric'), stop)) { 16991 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed"); 16992 } if ($truthy($rb_lt(stop, self))) { 16993 return 0 16994 } else { 16995 return $rb_plus($rb_minus(stop, self), 1) 16996 }}, {$$s: self}) 16997 } 16998 if (!stop.$$is_number) { 16999 $Kernel.$raise($$$('ArgumentError'), "comparison of " + (self.$class()) + " with " + (stop.$class()) + " failed"); 17000 } 17001 for (var i = self; i <= stop; i++) { 17002 block(i); 17003 } 17004 return self; 17005 }); 17006 17007 $def(self, '$zero?', function $Number_zero$ques$35() { 17008 var self = this; 17009 17010 return self == 0; 17011 }); 17012 17013 $def(self, '$size', $return_val(4)); 17014 17015 $def(self, '$nan?', function $Number_nan$ques$36() { 17016 var self = this; 17017 17018 return isNaN(self); 17019 }); 17020 17021 $def(self, '$finite?', function $Number_finite$ques$37() { 17022 var self = this; 17023 17024 return self != Infinity && self != -Infinity && !isNaN(self); 17025 }); 17026 17027 $def(self, '$infinite?', function $Number_infinite$ques$38() { 17028 var self = this; 17029 17030 17031 if (self == Infinity) { 17032 return +1; 17033 } 17034 else if (self == -Infinity) { 17035 return -1; 17036 } 17037 else { 17038 return nil; 17039 } 17040 17041 }); 17042 17043 $def(self, '$positive?', function $Number_positive$ques$39() { 17044 var self = this; 17045 17046 return self != 0 && (self == Infinity || 1 / self > 0); 17047 }); 17048 17049 $def(self, '$negative?', function $Number_negative$ques$40() { 17050 var self = this; 17051 17052 return self == -Infinity || 1 / self < 0; 17053 }); 17054 17055 function numberToUint8Array(num) { 17056 var uint8array = new Uint8Array(8); 17057 new DataView(uint8array.buffer).setFloat64(0, num, true); 17058 return uint8array; 17059 } 17060 17061 function uint8ArrayToNumber(arr) { 17062 return new DataView(arr.buffer).getFloat64(0, true); 17063 } 17064 17065 function incrementNumberBit(num) { 17066 var arr = numberToUint8Array(num); 17067 for (var i = 0; i < arr.length; i++) { 17068 if (arr[i] === 0xff) { 17069 arr[i] = 0; 17070 } else { 17071 arr[i]++; 17072 break; 17073 } 17074 } 17075 return uint8ArrayToNumber(arr); 17076 } 17077 17078 function decrementNumberBit(num) { 17079 var arr = numberToUint8Array(num); 17080 for (var i = 0; i < arr.length; i++) { 17081 if (arr[i] === 0) { 17082 arr[i] = 0xff; 17083 } else { 17084 arr[i]--; 17085 break; 17086 } 17087 } 17088 return uint8ArrayToNumber(arr); 17089 } 17090 17091 $def(self, '$next_float', function $$next_float() { 17092 var self = this; 17093 17094 17095 if ($eqeq(self, $$$($$$('Float'), 'INFINITY'))) { 17096 return $$$($$$('Float'), 'INFINITY') 17097 } if ($truthy(self['$nan?']())) { 17098 return $$$($$$('Float'), 'NAN') 17099 } if ($rb_ge(self, 0)) { 17100 return incrementNumberBit(Math.abs(self)); 17101 } else { 17102 return decrementNumberBit(self); 17103 } }); 17104 17105 $def(self, '$prev_float', function $$prev_float() { 17106 var self = this; 17107 17108 17109 if ($eqeq(self, $$$($$$('Float'), 'INFINITY')['$-@']())) { 17110 return $$$($$$('Float'), 'INFINITY')['$-@']() 17111 } if ($truthy(self['$nan?']())) { 17112 return $$$($$$('Float'), 'NAN') 17113 } if ($rb_gt(self, 0)) { 17114 return decrementNumberBit(self); 17115 } else { 17116 return -incrementNumberBit(Math.abs(self)); 17117 } }); 17118 $alias(self, "arg", "angle"); 17119 $alias(self, "eql?", "=="); 17120 $alias(self, "fdiv", "/"); 17121 $alias(self, "inspect", "to_s"); 17122 $alias(self, "kind_of?", "is_a?"); 17123 $alias(self, "magnitude", "abs"); 17124 $alias(self, "modulo", "%"); 17125 $alias(self, "object_id", "__id__"); 17126 $alias(self, "phase", "angle"); 17127 $alias(self, "succ", "next"); 17128 return $alias(self, "to_int", "to_i"); 17129 })('::', $$$('Numeric')); 17130 $const_set('::', 'Fixnum', $$$('Number')); 17131 (function($base, $super, $parent_nesting) { 17132 var self = $klass($base, $super, 'Integer'); 17133 17134 var $nesting = [self].concat($parent_nesting); 17135 17136 17137 self.$$is_number_class = true; 17138 self.$$is_integer_class = true; 17139 (function(self, $parent_nesting) { 17140 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 17141 17142 17143 17144 $def(self, '$allocate', function $$allocate() { 17145 var self = this; 17146 17147 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 17148 }); 17149 17150 Opal.udef(self, '$' + "new"); 17151 $def(self, '$sqrt', function $$sqrt(n) { 17152 17153 17154 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 17155 17156 if (n < 0) { 17157 $Kernel.$raise($$$($$$('Math'), 'DomainError'), "Numerical argument is out of domain - \"isqrt\""); 17158 } 17159 17160 return parseInt(Math.sqrt(n), 10); 17161 }); 17162 return $def(self, '$try_convert', function $$try_convert(object) { 17163 var self = this; 17164 17165 return $$('Opal')['$coerce_to?'](object, self, "to_int") 17166 }); 17167 })(Opal.get_singleton_class(self), $nesting); 17168 $const_set(self, 'MAX', Math.pow(2, 30) - 1); 17169 return $const_set(self, 'MIN', -Math.pow(2, 30)); 17170 })('::', $$$('Numeric'), $nesting); 17171 return (function($base, $super, $parent_nesting) { 17172 var self = $klass($base, $super, 'Float'); 17173 17174 17175 self.$$is_number_class = true; 17176 (function(self, $parent_nesting) { 17177 17178 17179 17180 $def(self, '$allocate', function $$allocate() { 17181 var self = this; 17182 17183 return $Kernel.$raise($$$('TypeError'), "allocator undefined for " + (self.$name())) 17184 }); 17185 17186 Opal.udef(self, '$' + "new"); return $def(self, '$===', function $eq_eq_eq$41(other) { 17187 17188 return !!other.$$is_number; 17189 }); 17190 })(Opal.get_singleton_class(self)); 17191 $const_set(self, 'INFINITY', Infinity); 17192 $const_set(self, 'MAX', Number.MAX_VALUE); 17193 $const_set(self, 'MIN', Number.MIN_VALUE); 17194 $const_set(self, 'NAN', NaN); 17195 $const_set(self, 'DIG', 15); 17196 $const_set(self, 'MANT_DIG', 53); 17197 $const_set(self, 'RADIX', 2); 17198 return $const_set(self, 'EPSILON', Number.EPSILON || 2.2204460492503130808472633361816E-16); 17199 })('::', $$$('Numeric')); 17200}; 17201 17202Opal.modules["corelib/range"] = function(Opal) {/* Generated by Opal 1.7.3 */ 17203 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.$$$; 17204 17205 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?'); 17206 17207 self.$require("corelib/enumerable"); 17208 return (function($base, $super, $parent_nesting) { 17209 var self = $klass($base, $super, 'Range'); 17210 17211 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 17212 17213 $proto.begin = $proto.end = $proto.excl = nil; 17214 17215 self.$include($$$('Enumerable')); 17216 self.$$prototype.$$is_range = true; 17217 self.$attr_reader("begin", "end"); 17218 17219 $def(self, '$initialize', function $$initialize(first, last, exclude) { 17220 var self = this; 17221 17222 17223 if (exclude == null) exclude = false; 17224 if ($truthy(self.begin)) { 17225 $Kernel.$raise($$$('NameError'), "'initialize' called twice"); 17226 } if (!(($truthy(first['$<=>'](last)) || ($truthy(first['$nil?']()))) || ($truthy(last['$nil?']())))) { 17227 $Kernel.$raise($$$('ArgumentError'), "bad value for range"); 17228 } self.begin = first; 17229 self.end = last; 17230 return (self.excl = exclude); 17231 }, -3); 17232 17233 $def(self, '$===', function $Range_$eq_eq_eq$1(value) { 17234 var self = this; 17235 17236 return self['$include?'](value) 17237 }); 17238 17239 function is_infinite(self) { 17240 if (self.begin === nil || self.end === nil || 17241 self.begin === -Infinity || self.end === Infinity || 17242 self.begin === Infinity || self.end === -Infinity) return true; 17243 return false; 17244 } 17245 17246 $def(self, '$count', function $$count() { 17247 var block = $$count.$$p || nil, self = this; 17248 17249 $$count.$$p = null; 17250 if (($not((block !== nil)) && ($truthy(is_infinite(self))))) { 17251 return $$$($$$('Float'), 'INFINITY') 17252 } return $send2(self, $find_super(self, 'count', $$count, false, true), 'count', [], block); 17253 }); 17254 17255 $def(self, '$to_a', function $$to_a() { 17256 var $yield = $$to_a.$$p || nil, self = this; 17257 17258 $$to_a.$$p = null; 17259 17260 if ($truthy(is_infinite(self))) { 17261 $Kernel.$raise($$$('TypeError'), "cannot convert endless range to an array"); 17262 } return $send2(self, $find_super(self, 'to_a', $$to_a, false, true), 'to_a', [], $yield); 17263 }); 17264 17265 $def(self, '$cover?', function $Range_cover$ques$2(value) { 17266 var self = this, beg_cmp = nil, $ret_or_1 = nil, $ret_or_2 = nil, $ret_or_3 = nil, end_cmp = nil; 17267 17268 17269 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)); 17270 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)); 17271 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))))) { 17272 return $rb_le(beg_cmp, 0) 17273 } else { 17274 return $ret_or_1 17275 } }); 17276 17277 $def(self, '$each', function $$each() { 17278 var block = $$each.$$p || nil, self = this, current = nil, last = nil, $ret_or_1 = nil; 17279 17280 $$each.$$p = null; 17281 if (!(block !== nil)) { 17282 return $send(self, 'enum_for', ["each"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 17283 17284 return self.$size()}, {$$s: self}) 17285 } 17286 var i, limit; 17287 17288 if (self.begin.$$is_number && self.end.$$is_number) { 17289 if (self.begin % 1 !== 0 || self.end % 1 !== 0) { 17290 $Kernel.$raise($$$('TypeError'), "can't iterate from Float"); 17291 } 17292 17293 for (i = self.begin, limit = self.end + ($truthy(self.excl) ? (0) : (1)); i < limit; i++) { 17294 block(i); 17295 } 17296 17297 return self; 17298 } 17299 17300 if (self.begin.$$is_string && self.end.$$is_string) { 17301 $send(self.begin, 'upto', [self.end, self.excl], block.$to_proc()); 17302 return self; 17303 } 17304 current = self.begin; 17305 last = self.end; 17306 if (!$truthy(current['$respond_to?']("succ"))) { 17307 $Kernel.$raise($$$('TypeError'), "can't iterate from " + (current.$class())); 17308 } while ($truthy(($truthy(($ret_or_1 = self.end['$nil?']())) ? ($ret_or_1) : ($rb_lt(current['$<=>'](last), 0))))) { 17309 17310 Opal.yield1(block, current); 17311 current = current.$succ(); 17312 } if (($not(self.excl) && ($eqeq(current, last)))) { 17313 Opal.yield1(block, current); 17314 } return self; 17315 }); 17316 17317 $def(self, '$eql?', function $Range_eql$ques$4(other) { 17318 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 17319 17320 17321 if (!$eqeqeq($$$('Range'), other)) { 17322 return false 17323 } if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = self.excl['$==='](other['$exclude_end?']()))) ? (self.begin['$eql?'](other.$begin())) : ($ret_or_2))))) { 17324 return self.end['$eql?'](other.$end()) 17325 } else { 17326 return $ret_or_1 17327 } }); 17328 17329 $def(self, '$exclude_end?', $return_ivar("excl")); 17330 17331 $def(self, '$first', function $$first(n) { 17332 var $yield = $$first.$$p || nil, self = this; 17333 17334 $$first.$$p = null; 17335 if ($truthy(self.begin['$nil?']())) { 17336 $Kernel.$raise($$$('RangeError'), "cannot get the minimum of beginless range"); 17337 } if ($truthy(n == null)) { 17338 return self.begin 17339 } return $send2(self, $find_super(self, 'first', $$first, false, true), 'first', [n], $yield); 17340 }, -1); 17341 17342 $def(self, '$last', function $$last(n) { 17343 var self = this; 17344 if ($truthy(self.end['$nil?']())) { 17345 $Kernel.$raise($$$('RangeError'), "cannot get the maximum of endless range"); 17346 } if ($truthy(n == null)) { 17347 return self.end 17348 } return self.$to_a().$last(n); 17349 }, -1); 17350 17351 $def(self, '$max', function $$max() { 17352 var $yield = $$max.$$p || nil, self = this; 17353 17354 $$max.$$p = null; 17355 if ($truthy(self.end['$nil?']())) { 17356 return $Kernel.$raise($$$('RangeError'), "cannot get the maximum of endless range") 17357 } else if (($yield !== nil)) { 17358 return $send2(self, $find_super(self, 'max', $$max, false, true), 'max', [], $yield) 17359 } else if (($not(self.begin['$nil?']()) && (($truthy($rb_gt(self.begin, self.end)) || (($truthy(self.excl) && ($eqeq(self.begin, self.end)))))))) { 17360 return nil 17361 } else { 17362 return self.excl ? self.end - 1 : self.end 17363 } 17364 }); 17365 17366 $def(self, '$min', function $$min() { 17367 var $yield = $$min.$$p || nil, self = this; 17368 17369 $$min.$$p = null; 17370 if ($truthy(self.begin['$nil?']())) { 17371 return $Kernel.$raise($$$('RangeError'), "cannot get the minimum of beginless range") 17372 } else if (($yield !== nil)) { 17373 return $send2(self, $find_super(self, 'min', $$min, false, true), 'min', [], $yield) 17374 } else if (($not(self.end['$nil?']()) && (($truthy($rb_gt(self.begin, self.end)) || (($truthy(self.excl) && ($eqeq(self.begin, self.end)))))))) { 17375 return nil 17376 } else { 17377 return self.begin 17378 } 17379 }); 17380 17381 $def(self, '$size', function $$size() { 17382 var self = this, infinity = nil, range_begin = nil, range_end = nil; 17383 17384 17385 infinity = $$$($$$('Float'), 'INFINITY'); 17386 if ((($eqeq(self.begin, infinity) && ($not(self.end['$nil?']()))) || (($eqeq(self.end, infinity['$-@']()) && ($not(self.begin['$nil?']())))))) { 17387 return 0 17388 } if ($truthy(is_infinite(self))) { 17389 return infinity 17390 } if (!($eqeqeq($$$('Numeric'), self.begin) && ($eqeqeq($$$('Numeric'), self.end)))) { 17391 return nil 17392 } range_begin = self.begin; 17393 range_end = self.end; 17394 if ($truthy(self.excl)) { 17395 range_end = $rb_minus(range_end, 1); 17396 } if ($truthy($rb_lt(range_end, range_begin))) { 17397 return 0 17398 } return (Math.abs(range_end - range_begin) + 1).$to_i(); 17399 }); 17400 17401 $def(self, '$step', function $$step(n) { 17402 var $yield = $$step.$$p || nil, self = this, $ret_or_1 = nil, i = nil; 17403 17404 $$step.$$p = null; 17405 17406 function coerceStepSize() { 17407 if (n == null) { 17408 n = 1; 17409 } 17410 else if (!n.$$is_number) { 17411 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 17412 } 17413 17414 if (n < 0) { 17415 $Kernel.$raise($$$('ArgumentError'), "step can't be negative"); 17416 } else if (n === 0) { 17417 $Kernel.$raise($$$('ArgumentError'), "step can't be 0"); 17418 } 17419 } 17420 17421 function enumeratorSize() { 17422 if (!self.begin['$respond_to?']("succ")) { 17423 return nil; 17424 } 17425 17426 if (self.begin.$$is_string && self.end.$$is_string) { 17427 return nil; 17428 } 17429 17430 if (n % 1 === 0) { 17431 return $rb_divide(self.$size(), n).$ceil(); 17432 } else { 17433 // n is a float 17434 var begin = self.begin, end = self.end, 17435 abs = Math.abs, floor = Math.floor, 17436 err = (abs(begin) + abs(end) + abs(end - begin)) / abs(n) * $$$($$$('Float'), 'EPSILON'), 17437 size; 17438 17439 if (err > 0.5) { 17440 err = 0.5; 17441 } 17442 17443 if (self.excl) { 17444 size = floor((end - begin) / n - err); 17445 if (size * n + begin < end) { 17446 size++; 17447 } 17448 } else { 17449 size = floor((end - begin) / n + err) + 1; 17450 } 17451 17452 return size; 17453 } 17454 } 17455 if (!($yield !== nil)) { 17456 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)))))) { 17457 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new(self, n, "step") 17458 } else { 17459 return $send(self, 'enum_for', ["step", n], function $$5(){ 17460 17461 coerceStepSize(); 17462 return enumeratorSize(); 17463 }) 17464 } 17465 } coerceStepSize(); 17466 if ($truthy(self.begin.$$is_number && self.end.$$is_number)) { 17467 17468 i = 0; 17469 (function(){try { var $t_break = $thrower('break'); return $send(self, 'loop', [], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s, current = nil; 17470 if (self.begin == null) self.begin = nil; 17471 if (self.excl == null) self.excl = nil; 17472 if (self.end == null) self.end = nil; 17473 17474 17475 current = $rb_plus(self.begin, $rb_times(i, n)); 17476 if ($truthy(self.excl)) { 17477 if ($truthy($rb_ge(current, self.end))) { 17478 $t_break.$throw(); 17479 } 17480 } else if ($truthy($rb_gt(current, self.end))) { 17481 $t_break.$throw(); 17482 }; 17483 Opal.yield1($yield, current); 17484 return (i = $rb_plus(i, 1));}, {$$s: self})} catch($e) { 17485 if ($e === $t_break) return $e.$v; 17486 throw $e; 17487 }})(); 17488 } else { 17489 17490 17491 if (self.begin.$$is_string && self.end.$$is_string && n % 1 !== 0) { 17492 $Kernel.$raise($$$('TypeError'), "no implicit conversion to float from string"); 17493 } 17494 $send(self, 'each_with_index', [], function $$7(value, idx){ 17495 17496 if (value == null) value = nil; 17497 if (idx == null) idx = nil; 17498 if ($eqeq(idx['$%'](n), 0)) { 17499 return Opal.yield1($yield, value); 17500 } else { 17501 return nil 17502 }}); 17503 } return self; 17504 }, -1); 17505 17506 $def(self, '$%', function $Range_$percent$8(n) { 17507 var self = this; 17508 17509 if (($truthy(self.begin['$is_a?']($$('Numeric'))) && ($truthy(self.end['$is_a?']($$('Numeric')))))) { 17510 return $$$($$$('Enumerator'), 'ArithmeticSequence').$new(self, n, "%") 17511 } else { 17512 return self.$step(n) 17513 } 17514 }); 17515 17516 $def(self, '$bsearch', function $$bsearch() { 17517 var block = $$bsearch.$$p || nil, self = this; 17518 17519 $$bsearch.$$p = null; 17520 if (!(block !== nil)) { 17521 return self.$enum_for("bsearch") 17522 } if ($truthy(is_infinite(self) && (self.begin.$$is_number || self.end.$$is_number))) { 17523 $Kernel.$raise($$$('NotImplementedError'), "Can't #bsearch an infinite range"); 17524 } if (!$truthy(self.begin.$$is_number && self.end.$$is_number)) { 17525 $Kernel.$raise($$$('TypeError'), "can't do binary search for " + (self.begin.$class())); 17526 } return $send(self.$to_a(), 'bsearch', [], block.$to_proc()); 17527 }); 17528 17529 $def(self, '$to_s', function $$to_s() { 17530 var self = this, $ret_or_1 = nil; 17531 17532 return "" + (($truthy(($ret_or_1 = self.begin)) ? ($ret_or_1) : (""))) + (($truthy(self.excl) ? ("...") : (".."))) + (($truthy(($ret_or_1 = self.end)) ? ($ret_or_1) : (""))) 17533 }); 17534 17535 $def(self, '$inspect', function $$inspect() { 17536 var self = this, $ret_or_1 = nil; 17537 17538 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))) 17539 }); 17540 17541 $def(self, '$marshal_load', function $$marshal_load(args) { 17542 var self = this; 17543 17544 17545 self.begin = args['$[]']("begin"); 17546 self.end = args['$[]']("end"); 17547 return (self.excl = args['$[]']("excl")); 17548 }); 17549 17550 $def(self, '$hash', function $$hash() { 17551 var self = this; 17552 17553 return [self.begin, self.end, self.excl].$hash() 17554 }); 17555 $alias(self, "==", "eql?"); 17556 $alias(self, "include?", "cover?"); 17557 return $alias(self, "member?", "cover?"); 17558 })('::', null, $nesting); 17559}; 17560 17561Opal.modules["corelib/proc"] = function(Opal) {/* Generated by Opal 1.7.3 */ 17562 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.$$$; 17563 17564 Opal.add_stubs('raise,proc,call,to_proc,new,source_location,coerce_to!,dup'); 17565 return (function($base, $super) { 17566 var self = $klass($base, $super, 'Proc'); 17567 17568 17569 17570 Opal.prop(self.$$prototype, '$$is_proc', true); 17571 Opal.prop(self.$$prototype, '$$is_lambda', false); 17572 $defs(self, '$new', function $Proc_new$1() { 17573 var block = $Proc_new$1.$$p || nil; 17574 17575 $Proc_new$1.$$p = null; 17576 if (!$truthy(block)) { 17577 $Kernel.$raise($$$('ArgumentError'), "tried to create a Proc object without a block"); 17578 } return block; 17579 }); 17580 17581 $def(self, '$call', function $$call($a) { 17582 var block = $$call.$$p || nil, $post_args, args, self = this; 17583 17584 $$call.$$p = null; 17585 $post_args = $slice(arguments); 17586 args = $post_args; 17587 17588 if (block !== nil) { 17589 self.$$p = block; 17590 } 17591 17592 var result, $brk = self.$$brk, $ret = self.$$ret; 17593 17594 if ($brk || ($ret && self.$$is_lambda)) { 17595 try { 17596 if (self.$$is_lambda) { 17597 result = self.apply(null, args); 17598 } 17599 else { 17600 result = Opal.yieldX(self, args); 17601 } 17602 } catch (err) { 17603 if (err === $brk) { 17604 return err.$v; 17605 } 17606 else if (self.$$is_lambda && err === $ret) { 17607 return err.$v; 17608 } 17609 else { 17610 throw err; 17611 } 17612 } 17613 } 17614 else { 17615 if (self.$$is_lambda) { 17616 result = self.apply(null, args); 17617 } 17618 else { 17619 result = Opal.yieldX(self, args); 17620 } 17621 } 17622 17623 return result; 17624 }, -1); 17625 17626 $def(self, '$>>', function $Proc_$gt$gt$2(other) { 17627 var self = this; 17628 17629 $Proc_$gt$gt$2.$$p = null; 17630 return $send($Kernel, 'proc', [], function $$3($a){var block = $$3.$$p || nil, $post_args, args, self = $$3.$$s == null ? this : $$3.$$s, out = nil; 17631 17632 $$3.$$p = null; 17633 $post_args = $slice(arguments); 17634 args = $post_args; 17635 out = $send(self, 'call', $to_a(args), block.$to_proc()); 17636 return other.$call(out);}, {$$arity: -1, $$s: self}) 17637 }); 17638 17639 $def(self, '$<<', function $Proc_$lt$lt$4(other) { 17640 var self = this; 17641 17642 $Proc_$lt$lt$4.$$p = null; 17643 return $send($Kernel, 'proc', [], function $$5($a){var block = $$5.$$p || nil, $post_args, args, self = $$5.$$s == null ? this : $$5.$$s, out = nil; 17644 17645 $$5.$$p = null; 17646 $post_args = $slice(arguments); 17647 args = $post_args; 17648 out = $send(other, 'call', $to_a(args), block.$to_proc()); 17649 return self.$call(out);}, {$$arity: -1, $$s: self}) 17650 }); 17651 17652 $def(self, '$to_proc', $return_self); 17653 17654 $def(self, '$lambda?', function $Proc_lambda$ques$6() { 17655 var self = this; 17656 17657 return !!self.$$is_lambda; 17658 }); 17659 17660 $def(self, '$arity', function $$arity() { 17661 var self = this; 17662 17663 17664 if (self.$$is_curried) { 17665 return -1; 17666 } else if (self.$$arity != null) { 17667 return self.$$arity; 17668 } else { 17669 return self.length; 17670 } 17671 17672 }); 17673 17674 $def(self, '$source_location', function $$source_location() { 17675 var self = this, $ret_or_1 = nil; 17676 17677 17678 if (self.$$is_curried) { return nil; } if ($truthy(($ret_or_1 = self.$$source_location))) { 17679 return $ret_or_1 17680 } else { 17681 return nil 17682 } }); 17683 17684 $def(self, '$binding', function $$binding() { 17685 var self = this; 17686 17687 17688 if (self.$$is_curried) { $Kernel.$raise($$$('ArgumentError'), "Can't create Binding"); } if ($truthy((($$$('::', 'Binding', 'skip_raise')) ? 'constant' : nil))) { 17689 return $$$('Binding').$new(nil, [], self.$$s, self.$source_location()) 17690 } else { 17691 return nil 17692 } }); 17693 17694 $def(self, '$parameters', function $$parameters($kwargs) { 17695 var lambda, self = this; 17696 17697 17698 $kwargs = $ensure_kwargs($kwargs); 17699 17700 lambda = $kwargs.$$smap["lambda"]; 17701 if (self.$$is_curried) { 17702 return [["rest"]]; 17703 } else if (self.$$parameters) { 17704 if (lambda == null ? self.$$is_lambda : lambda) { 17705 return self.$$parameters; 17706 } else { 17707 var result = [], i, length; 17708 17709 for (i = 0, length = self.$$parameters.length; i < length; i++) { 17710 var parameter = self.$$parameters[i]; 17711 17712 if (parameter[0] === 'req') { 17713 // required arguments always have name 17714 parameter = ['opt', parameter[1]]; 17715 } 17716 17717 result.push(parameter); 17718 } 17719 17720 return result; 17721 } 17722 } else { 17723 return []; 17724 } 17725 }, -1); 17726 17727 $def(self, '$curry', function $$curry(arity) { 17728 var self = this; 17729 17730 if (arity === undefined) { 17731 arity = self.length; 17732 } 17733 else { 17734 arity = $Opal['$coerce_to!'](arity, $$$('Integer'), "to_int"); 17735 if (self.$$is_lambda && arity !== self.length) { 17736 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arity) + " for " + (self.length) + ")"); 17737 } 17738 } 17739 17740 function curried () { 17741 var args = $slice(arguments), 17742 length = args.length, 17743 result; 17744 17745 if (length > arity && self.$$is_lambda && !self.$$is_curried) { 17746 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (length) + " for " + (arity) + ")"); 17747 } 17748 17749 if (length >= arity) { 17750 return self.$call.apply(self, args); 17751 } 17752 17753 result = function () { 17754 return curried.apply(null, 17755 args.concat($slice(arguments))); 17756 }; 17757 result.$$is_lambda = self.$$is_lambda; 17758 result.$$is_curried = true; 17759 17760 return result; 17761 } 17762 curried.$$is_lambda = self.$$is_lambda; 17763 curried.$$is_curried = true; 17764 return curried; 17765 }, -1); 17766 17767 $def(self, '$dup', function $$dup() { 17768 var self = this; 17769 17770 17771 var original_proc = self.$$original_proc || self, 17772 proc = function () { 17773 return original_proc.apply(this, arguments); 17774 }; 17775 17776 for (var prop in self) { 17777 if (self.hasOwnProperty(prop)) { 17778 proc[prop] = self[prop]; 17779 } 17780 } 17781 17782 return proc; 17783 17784 }); 17785 $alias(self, "===", "call"); 17786 $alias(self, "clone", "dup"); 17787 $alias(self, "yield", "call"); 17788 return $alias(self, "[]", "call"); 17789 })('::', Function) 17790}; 17791 17792Opal.modules["corelib/method"] = function(Opal) {/* Generated by Opal 1.7.3 */ 17793 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.$$$; 17794 17795 Opal.add_stubs('attr_reader,arity,curry,>>,<<,new,class,join,source_location,call,raise,bind,to_proc'); 17796 17797 (function($base, $super) { 17798 var self = $klass($base, $super, 'Method'); 17799 17800 var $proto = self.$$prototype; 17801 17802 $proto.method = $proto.receiver = $proto.owner = $proto.name = nil; 17803 17804 self.$attr_reader("owner", "receiver", "name"); 17805 17806 $def(self, '$initialize', function $$initialize(receiver, owner, method, name) { 17807 var self = this; 17808 17809 17810 self.receiver = receiver; 17811 self.owner = owner; 17812 self.name = name; 17813 return (self.method = method); 17814 }); 17815 17816 $def(self, '$arity', function $$arity() { 17817 var self = this; 17818 17819 return self.method.$arity() 17820 }); 17821 17822 $def(self, '$parameters', function $$parameters() { 17823 var self = this; 17824 17825 return self.method.$$parameters 17826 }); 17827 17828 $def(self, '$source_location', function $$source_location() { 17829 var self = this, $ret_or_1 = nil; 17830 17831 if ($truthy(($ret_or_1 = self.method.$$source_location))) { 17832 return $ret_or_1 17833 } else { 17834 return ["(eval)", 0] 17835 } 17836 }); 17837 17838 $def(self, '$comments', function $$comments() { 17839 var self = this, $ret_or_1 = nil; 17840 17841 if ($truthy(($ret_or_1 = self.method.$$comments))) { 17842 return $ret_or_1 17843 } else { 17844 return [] 17845 } 17846 }); 17847 17848 $def(self, '$call', function $$call($a) { 17849 var block = $$call.$$p || nil, $post_args, args, self = this; 17850 17851 $$call.$$p = null; 17852 $post_args = $slice(arguments); 17853 args = $post_args; 17854 17855 self.method.$$p = block; 17856 17857 return self.method.apply(self.receiver, args); 17858 }, -1); 17859 17860 $def(self, '$curry', function $$curry(arity) { 17861 var self = this; 17862 return self.method.$curry(arity); 17863 }, -1); 17864 17865 $def(self, '$>>', function $Method_$gt$gt$1(other) { 17866 var self = this; 17867 17868 return self.method['$>>'](other) 17869 }); 17870 17871 $def(self, '$<<', function $Method_$lt$lt$2(other) { 17872 var self = this; 17873 17874 return self.method['$<<'](other) 17875 }); 17876 17877 $def(self, '$unbind', function $$unbind() { 17878 var self = this; 17879 17880 return $$$('UnboundMethod').$new(self.receiver.$class(), self.owner, self.method, self.name) 17881 }); 17882 17883 $def(self, '$to_proc', function $$to_proc() { 17884 var self = this; 17885 17886 17887 var proc = self.$call.bind(self); 17888 proc.$$unbound = self.method; 17889 proc.$$is_lambda = true; 17890 proc.$$arity = self.method.$$arity == null ? self.method.length : self.method.$$arity; 17891 proc.$$parameters = self.method.$$parameters; 17892 return proc; 17893 17894 }); 17895 17896 $def(self, '$inspect', function $$inspect() { 17897 var self = this; 17898 17899 return "#<" + (self.$class()) + ": " + (self.receiver.$class()) + "#" + (self.name) + " (defined in " + (self.owner) + " in " + (self.$source_location().$join(":")) + ")>" 17900 }); 17901 $alias(self, "[]", "call"); 17902 return $alias(self, "===", "call"); 17903 })('::', null); 17904 return (function($base, $super) { 17905 var self = $klass($base, $super, 'UnboundMethod'); 17906 17907 var $proto = self.$$prototype; 17908 17909 $proto.method = $proto.owner = $proto.name = $proto.source = nil; 17910 17911 self.$attr_reader("source", "owner", "name"); 17912 17913 $def(self, '$initialize', function $$initialize(source, owner, method, name) { 17914 var self = this; 17915 17916 17917 self.source = source; 17918 self.owner = owner; 17919 self.method = method; 17920 return (self.name = name); 17921 }); 17922 17923 $def(self, '$arity', function $$arity() { 17924 var self = this; 17925 17926 return self.method.$arity() 17927 }); 17928 17929 $def(self, '$parameters', function $$parameters() { 17930 var self = this; 17931 17932 return self.method.$$parameters 17933 }); 17934 17935 $def(self, '$source_location', function $$source_location() { 17936 var self = this, $ret_or_1 = nil; 17937 17938 if ($truthy(($ret_or_1 = self.method.$$source_location))) { 17939 return $ret_or_1 17940 } else { 17941 return ["(eval)", 0] 17942 } 17943 }); 17944 17945 $def(self, '$comments', function $$comments() { 17946 var self = this, $ret_or_1 = nil; 17947 17948 if ($truthy(($ret_or_1 = self.method.$$comments))) { 17949 return $ret_or_1 17950 } else { 17951 return [] 17952 } 17953 }); 17954 17955 $def(self, '$bind', function $$bind(object) { 17956 var self = this; 17957 17958 17959 if (self.owner.$$is_module || Opal.is_a(object, self.owner)) { 17960 return $$$('Method').$new(object, self.owner, self.method, self.name); 17961 } 17962 else { 17963 $Kernel.$raise($$$('TypeError'), "can't bind singleton method to a different class (expected " + (object) + ".kind_of?(" + (self.owner) + " to be true)"); 17964 } 17965 17966 }); 17967 17968 $def(self, '$bind_call', function $$bind_call(object, $a) { 17969 var block = $$bind_call.$$p || nil, $post_args, args, self = this; 17970 17971 $$bind_call.$$p = null; 17972 $post_args = $slice(arguments, 1); 17973 args = $post_args; 17974 return $send(self.$bind(object), 'call', $to_a(args), block.$to_proc()); 17975 }, -2); 17976 return $def(self, '$inspect', function $$inspect() { 17977 var self = this; 17978 17979 return "#<" + (self.$class()) + ": " + (self.source) + "#" + (self.name) + " (defined in " + (self.owner) + " in " + (self.$source_location().$join(":")) + ")>" 17980 }); 17981 })('::', null); 17982}; 17983 17984Opal.modules["corelib/variables"] = function(Opal) {/* Generated by Opal 1.7.3 */ 17985 var $gvars = Opal.gvars, $const_set = Opal.const_set, $Object = Opal.Object, $hash2 = Opal.hash2, nil = Opal.nil; 17986 17987 Opal.add_stubs('new'); 17988 17989 $gvars['&'] = $gvars['~'] = $gvars['`'] = $gvars["'"] = nil; 17990 $gvars.LOADED_FEATURES = ($gvars["\""] = Opal.loaded_features); 17991 $gvars.LOAD_PATH = ($gvars[":"] = []); 17992 $gvars["/"] = "\n"; 17993 $gvars[","] = nil; 17994 $const_set('::', 'ARGV', []); 17995 $const_set('::', 'ARGF', $Object.$new()); 17996 $const_set('::', 'ENV', $hash2([], {})); 17997 $gvars.VERBOSE = false; 17998 $gvars.DEBUG = false; 17999 return ($gvars.SAFE = 0); 18000}; 18001 18002Opal.modules["corelib/io"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18003 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.$$$; 18004 18005 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='); 18006 18007 (function($base, $super) { 18008 var self = $klass($base, $super, 'IO'); 18009 18010 var $proto = self.$$prototype; 18011 18012 $proto.read_buffer = $proto.closed = nil; 18013 18014 $const_set(self, 'SEEK_SET', 0); 18015 $const_set(self, 'SEEK_CUR', 1); 18016 $const_set(self, 'SEEK_END', 2); 18017 $const_set(self, 'SEEK_DATA', 3); 18018 $const_set(self, 'SEEK_HOLE', 4); 18019 $const_set(self, 'READABLE', 1); 18020 $const_set(self, 'WRITABLE', 4); 18021 self.$attr_reader("eof"); 18022 self.$attr_accessor("read_proc", "sync", "tty", "write_proc"); 18023 18024 $def(self, '$initialize', function $$initialize(fd, flags) { 18025 var self = this; 18026 18027 18028 if (flags == null) flags = "r"; 18029 self.fd = fd; 18030 self.flags = flags; 18031 self.eof = false; 18032 if (($truthy(flags['$include?']("r")) && ($not(flags['$match?'](/[wa+]/))))) { 18033 return (self.closed = "write") 18034 } else if (($truthy(flags['$match?'](/[wa]/)) && ($not(flags['$match?'](/[r+]/))))) { 18035 return (self.closed = "read") 18036 } else { 18037 return nil 18038 } }, -2); 18039 18040 $def(self, '$fileno', $return_ivar("fd")); 18041 18042 $def(self, '$tty?', function $IO_tty$ques$1() { 18043 var self = this; 18044 18045 return self.tty == true; 18046 }); 18047 18048 $def(self, '$write', function $$write(string) { 18049 var self = this; 18050 18051 18052 self.write_proc(string); 18053 return string.$size(); 18054 }); 18055 18056 $def(self, '$flush', $return_val(nil)); 18057 18058 $def(self, '$<<', function $IO_$lt$lt$2(string) { 18059 var self = this; 18060 18061 18062 self.$write(string); 18063 return self; 18064 }); 18065 18066 $def(self, '$print', function $$print($a) { 18067 var $post_args, args, self = this; 18068 if ($gvars[","] == null) $gvars[","] = nil; 18069 18070 18071 $post_args = $slice(arguments); 18072 args = $post_args; 18073 18074 for (var i = 0, ii = args.length; i < ii; i++) { 18075 args[i] = $Kernel.$String(args[i]); 18076 } 18077 self.$write(args.join($gvars[","])); 18078 return nil; 18079 }, -1); 18080 18081 $def(self, '$puts', function $$puts($a) { 18082 var $post_args, args, self = this; 18083 18084 18085 $post_args = $slice(arguments); 18086 args = $post_args; 18087 18088 var line; 18089 if (args.length === 0) { 18090 self.$write("\n"); 18091 return nil; 18092 } else { 18093 for (var i = 0, ii = args.length; i < ii; i++) { 18094 if (args[i].$$is_array){ 18095 var ary = (args[i]).$flatten(); 18096 if (ary.length > 0) $send(self, 'puts', $to_a((ary))); 18097 } else { 18098 if (args[i].$$is_string) { 18099 line = args[i].valueOf(); 18100 } else { 18101 line = $Kernel.$String(args[i]); 18102 } 18103 if (!line.endsWith("\n")) line += "\n"; 18104 self.$write(line); 18105 } 18106 } 18107 } 18108 return nil; 18109 }, -1); 18110 18111 $def(self, '$getc', function $$getc() { 18112 var self = this, $ret_or_1 = nil, parts = nil, ret = nil; 18113 18114 18115 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 18116 parts = ""; 18117 do { 18118 18119 self.read_buffer = $rb_plus(self.read_buffer, parts); 18120 if ($neqeq(self.read_buffer, "")) { 18121 18122 ret = self.read_buffer['$[]'](0); 18123 self.read_buffer = self.read_buffer['$[]']($range(1, -1, false)); 18124 return ret; 18125 } } while ($truthy((parts = self.$sysread_noraise(1)))); return nil; 18126 }); 18127 18128 $def(self, '$getbyte', function $$getbyte() { 18129 var $a, self = this; 18130 18131 return ($a = self.$getc(), ($a === nil || $a == null) ? nil : $a.$ord()) 18132 }); 18133 18134 $def(self, '$readbyte', function $$readbyte() { 18135 var self = this; 18136 18137 return self.$readchar().$ord() 18138 }); 18139 18140 $def(self, '$readchar', function $$readchar() { 18141 var self = this, $ret_or_1 = nil; 18142 18143 if ($truthy(($ret_or_1 = self.$getc()))) { 18144 return $ret_or_1 18145 } else { 18146 return $Kernel.$raise($$$('EOFError'), "end of file reached") 18147 } 18148 }); 18149 18150 $def(self, '$readline', function $$readline($a) { 18151 var $post_args, args, self = this, $ret_or_1 = nil; 18152 18153 18154 $post_args = $slice(arguments); 18155 args = $post_args; 18156 if ($truthy(($ret_or_1 = $send(self, 'gets', $to_a(args))))) { 18157 return $ret_or_1 18158 } else { 18159 return $Kernel.$raise($$$('EOFError'), "end of file reached") 18160 } }, -1); 18161 18162 $def(self, '$gets', function $$gets(sep, limit, opts) { 18163 var $a, $b, self = this, orig_sep = nil, $ret_or_1 = nil, seplen = nil, data = nil, ret = nil, orig_buffer = nil; 18164 if ($gvars["/"] == null) $gvars["/"] = nil; 18165 18166 18167 if (sep == null) sep = false; 18168 if (limit == null) limit = nil; 18169 if (opts == null) opts = $hash2([], {}); 18170 if (($truthy(sep.$$is_number) && ($not(limit)))) { 18171 $a = [false, sep, limit], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]); 18172 } if ((($truthy(sep.$$is_hash) && ($not(limit))) && ($eqeq(opts, $hash2([], {}))))) { 18173 $a = [false, nil, sep], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]); 18174 } else if (($truthy(limit.$$is_hash) && ($eqeq(opts, $hash2([], {}))))) { 18175 $a = [sep, nil, limit], (sep = $a[0]), (limit = $a[1]), (opts = $a[2]); 18176 } orig_sep = sep; 18177 if ($eqeq(sep, false)) { 18178 sep = $gvars["/"]; 18179 } if ($eqeq(sep, "")) { 18180 sep = /\r?\n\r?\n/; 18181 } sep = ($truthy(($ret_or_1 = sep)) ? ($ret_or_1) : ("")); 18182 if (!$eqeq(orig_sep, "")) { 18183 sep = sep.$to_str(); 18184 } seplen = ($eqeq(orig_sep, "") ? (2) : (sep.$length())); 18185 if ($eqeq(sep, " ")) { 18186 sep = / /; 18187 } self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 18188 data = ""; 18189 ret = nil; 18190 do { 18191 18192 self.read_buffer = $rb_plus(self.read_buffer, data); 18193 if (($neqeq(sep, "") && ($truthy(($truthy(sep.$$is_regexp) ? (self.read_buffer['$match?'](sep)) : (self.read_buffer['$include?'](sep))))))) { 18194 18195 orig_buffer = self.read_buffer; 18196 $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])); 18197 if ($neqeq(ret, orig_buffer)) { 18198 ret = $rb_plus(ret, orig_buffer['$[]'](ret.$length(), seplen)); 18199 } break; 18200 } } while ($truthy((data = self.$sysread_noraise(($eqeq(sep, "") ? (65536) : (1)))))); if (!$truthy(ret)) { 18201 18202 $a = [($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")), ""], (ret = $a[0]), (self.read_buffer = $a[1]); 18203 if ($eqeq(ret, "")) { 18204 ret = nil; 18205 } } if ($truthy(ret)) { 18206 18207 if ($truthy(limit)) { 18208 18209 ret = ret['$[]'](Opal.Range.$new(0,limit, true)); 18210 self.read_buffer = $rb_plus(ret['$[]'](Opal.Range.$new(limit, -1, false)), self.read_buffer); 18211 } if ($truthy(opts['$[]']("chomp"))) { 18212 ret = ret.$sub(/\r?\n$/, ""); 18213 } if ($eqeq(orig_sep, "")) { 18214 ret = ret.$sub(/^[\r\n]+/, ""); 18215 } } if ($eqeq(orig_sep, false)) { 18216 $gvars._ = ret; 18217 } return ret; 18218 }, -1); 18219 18220 $def(self, '$sysread', function $$sysread(integer) { 18221 var self = this, $ret_or_1 = nil; 18222 18223 if ($truthy(($ret_or_1 = self.read_proc(integer)))) { 18224 return $ret_or_1 18225 } else { 18226 18227 self.eof = true; 18228 return $Kernel.$raise($$$('EOFError'), "end of file reached"); 18229 } 18230 }); 18231 18232 $def(self, '$sysread_noraise', function $$sysread_noraise(integer) { 18233 var self = this; 18234 18235 try { 18236 return self.$sysread(integer) 18237 } catch ($err) { 18238 if (Opal.rescue($err, [$$$('EOFError')])) { 18239 try { 18240 return nil 18241 } finally { Opal.pop_exception(); } 18242 } else { throw $err; } 18243 } 18244 }); 18245 18246 $def(self, '$readpartial', function $$readpartial(integer) { 18247 var $a, self = this, $ret_or_1 = nil, part = nil, ret = nil; 18248 18249 18250 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 18251 part = self.$sysread(integer); 18252 $a = [$rb_plus(self.read_buffer, ($truthy(($ret_or_1 = part)) ? ($ret_or_1) : (""))), ""], (ret = $a[0]), (self.read_buffer = $a[1]); 18253 if ($eqeq(ret, "")) { 18254 ret = nil; 18255 } return ret; 18256 }); 18257 18258 $def(self, '$read', function $$read(integer) { 18259 var $a, self = this, $ret_or_1 = nil, parts = nil, ret = nil; 18260 18261 18262 if (integer == null) integer = nil; 18263 self.read_buffer = ($truthy(($ret_or_1 = self.read_buffer)) ? ($ret_or_1) : ("")); 18264 parts = ""; 18265 ret = nil; 18266 do { 18267 18268 self.read_buffer = $rb_plus(self.read_buffer, parts); 18269 if (($truthy(integer) && ($truthy($rb_gt(self.read_buffer.$length(), integer))))) { 18270 18271 $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]); 18272 return ret; 18273 } } while ($truthy((parts = self.$sysread_noraise(($truthy(($ret_or_1 = integer)) ? ($ret_or_1) : (65536)))))); $a = [self.read_buffer, ""], (ret = $a[0]), (self.read_buffer = $a[1]); 18274 return ret; 18275 }, -1); 18276 18277 $def(self, '$readlines', function $$readlines(separator) { 18278 var self = this; 18279 if ($gvars["/"] == null) $gvars["/"] = nil; 18280 18281 18282 if (separator == null) separator = $gvars["/"]; 18283 return self.$each_line(separator).$to_a(); 18284 }, -1); 18285 18286 $def(self, '$each', function $$each($a, $b) { 18287 var block = $$each.$$p || nil, $post_args, sep, args, self = this, s = nil; 18288 if ($gvars["/"] == null) $gvars["/"] = nil; 18289 18290 $$each.$$p = null; 18291 $post_args = $slice(arguments); 18292 18293 if ($post_args.length > 0) sep = $post_args.shift();if (sep == null) sep = $gvars["/"]; 18294 args = $post_args; 18295 if (!(block !== nil)) { 18296 return $send(self, 'enum_for', ["each", sep].concat($to_a(args))) 18297 } while ($truthy((s = $send(self, 'gets', [sep].concat($to_a(args)))))) { 18298 Opal.yield1(block, s); 18299 } return self; 18300 }, -1); 18301 18302 $def(self, '$each_byte', function $$each_byte() { 18303 var block = $$each_byte.$$p || nil, self = this, s = nil; 18304 18305 $$each_byte.$$p = null; 18306 if (!(block !== nil)) { 18307 return self.$enum_for("each_byte") 18308 } while ($truthy((s = self.$getbyte()))) { 18309 Opal.yield1(block, s); 18310 } return self; 18311 }); 18312 18313 $def(self, '$each_char', function $$each_char() { 18314 var block = $$each_char.$$p || nil, self = this, s = nil; 18315 18316 $$each_char.$$p = null; 18317 if (!(block !== nil)) { 18318 return self.$enum_for("each_char") 18319 } while ($truthy((s = self.$getc()))) { 18320 Opal.yield1(block, s); 18321 } return self; 18322 }); 18323 18324 $def(self, '$close', $assign_ivar_val("closed", "both")); 18325 18326 $def(self, '$close_read', function $$close_read() { 18327 var self = this; 18328 18329 if ($eqeq(self.closed, "write")) { 18330 return (self.closed = "both") 18331 } else { 18332 return (self.closed = "read") 18333 } 18334 }); 18335 18336 $def(self, '$close_write', function $$close_write() { 18337 var self = this; 18338 18339 if ($eqeq(self.closed, "read")) { 18340 return (self.closed = "both") 18341 } else { 18342 return (self.closed = "write") 18343 } 18344 }); 18345 18346 $def(self, '$closed?', function $IO_closed$ques$3() { 18347 var self = this; 18348 18349 return self.closed['$==']("both") 18350 }); 18351 18352 $def(self, '$closed_read?', function $IO_closed_read$ques$4() { 18353 var self = this, $ret_or_1 = nil; 18354 18355 if ($truthy(($ret_or_1 = self.closed['$==']("read")))) { 18356 return $ret_or_1 18357 } else { 18358 return self.closed['$==']("both") 18359 } 18360 }); 18361 18362 $def(self, '$closed_write?', function $IO_closed_write$ques$5() { 18363 var self = this, $ret_or_1 = nil; 18364 18365 if ($truthy(($ret_or_1 = self.closed['$==']("write")))) { 18366 return $ret_or_1 18367 } else { 18368 return self.closed['$==']("both") 18369 } 18370 }); 18371 18372 $def(self, '$check_writable', function $$check_writable() { 18373 var self = this; 18374 18375 if ($truthy(self['$closed_write?']())) { 18376 return $Kernel.$raise($$$('IOError'), "not opened for writing") 18377 } else { 18378 return nil 18379 } 18380 }); 18381 18382 $def(self, '$check_readable', function $$check_readable() { 18383 var self = this; 18384 18385 if ($truthy(self['$closed_read?']())) { 18386 return $Kernel.$raise($$$('IOError'), "not opened for reading") 18387 } else { 18388 return nil 18389 } 18390 }); 18391 $alias(self, "each_line", "each"); 18392 return $alias(self, "eof?", "eof"); 18393 })('::', null); 18394 $const_set('::', 'STDIN', ($gvars.stdin = $$$('IO').$new(0, "r"))); 18395 $const_set('::', 'STDOUT', ($gvars.stdout = $$$('IO').$new(1, "w"))); 18396 $const_set('::', 'STDERR', ($gvars.stderr = $$$('IO').$new(2, "w"))); 18397 var console = Opal.global.console; 18398 $$$('STDOUT')['$write_proc='](typeof(process) === 'object' && typeof(process.stdout) === 'object' ? function(s){process.stdout.write(s);} : function(s){console.log(s);}); 18399 $$$('STDERR')['$write_proc='](typeof(process) === 'object' && typeof(process.stderr) === 'object' ? function(s){process.stderr.write(s);} : function(s){console.warn(s);}); 18400 return ($a = [function(s) { var p = prompt(); if (p !== null) return p + "\n"; return nil; }], $send($$$('STDIN'), 'read_proc=', $a), $a[$a.length - 1]); 18401}; 18402 18403Opal.modules["opal/regexp_anchors"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18404 var $module = Opal.module, $const_set = Opal.const_set, $nesting = []; Opal.nil; var $$$ = Opal.$$$; 18405 18406 Opal.add_stubs('new'); 18407 return (function($base, $parent_nesting) { 18408 var self = $module($base, 'Opal'); 18409 18410 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 18411 18412 18413 $const_set(self, 'REGEXP_START', "^"); 18414 $const_set(self, 'REGEXP_END', "$"); 18415 $const_set(self, 'FORBIDDEN_STARTING_IDENTIFIER_CHARS', "\\u0001-\\u002F\\u003A-\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 18416 $const_set(self, 'FORBIDDEN_ENDING_IDENTIFIER_CHARS', "\\u0001-\\u0020\\u0022-\\u002F\\u003A-\\u003E\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 18417 $const_set(self, 'INLINE_IDENTIFIER_REGEXP', $$('Regexp').$new("[^" + ($$$(self, 'FORBIDDEN_STARTING_IDENTIFIER_CHARS')) + "]*[^" + ($$$(self, 'FORBIDDEN_ENDING_IDENTIFIER_CHARS')) + "]")); 18418 $const_set(self, 'FORBIDDEN_CONST_NAME_CHARS', "\\u0001-\\u0020\\u0021-\\u002F\\u003B-\\u003F\\u0040\\u005B-\\u005E\\u0060\\u007B-\\u007F"); 18419 return $const_set(self, 'CONST_NAME_REGEXP', $$('Regexp').$new("" + ($$$(self, 'REGEXP_START')) + "(::)?[A-Z][^" + ($$$(self, 'FORBIDDEN_CONST_NAME_CHARS')) + "]*" + ($$$(self, 'REGEXP_END')))); 18420 })($nesting[0], $nesting) 18421}; 18422 18423Opal.modules["opal/mini"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18424 var $Object = Opal.Object; Opal.nil; 18425 18426 Opal.add_stubs('require'); 18427 18428 $Object.$require("opal/base"); 18429 $Object.$require("corelib/nil"); 18430 $Object.$require("corelib/boolean"); 18431 $Object.$require("corelib/string"); 18432 $Object.$require("corelib/comparable"); 18433 $Object.$require("corelib/enumerable"); 18434 $Object.$require("corelib/enumerator"); 18435 $Object.$require("corelib/array"); 18436 $Object.$require("corelib/hash"); 18437 $Object.$require("corelib/number"); 18438 $Object.$require("corelib/range"); 18439 $Object.$require("corelib/proc"); 18440 $Object.$require("corelib/method"); 18441 $Object.$require("corelib/regexp"); 18442 $Object.$require("corelib/variables"); 18443 $Object.$require("corelib/io"); 18444 return $Object.$require("opal/regexp_anchors"); 18445}; 18446 18447Opal.modules["corelib/kernel/format"] = function(Opal) {/* Generated by Opal 1.7.3 */ 18448 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.$$$; 18449 18450 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'); 18451 return (function($base) { 18452 var self = $module($base, 'Kernel'); 18453 18454 18455 18456 18457 $def(self, '$format', function $$format(format_string, $a) { 18458 var $post_args, args, ary = nil; 18459 if ($gvars.DEBUG == null) $gvars.DEBUG = nil; 18460 18461 18462 $post_args = $slice(arguments, 1); 18463 args = $post_args; 18464 if (($eqeq(args.$length(), 1) && ($truthy(args['$[]'](0)['$respond_to?']("to_ary"))))) { 18465 18466 ary = $Opal['$coerce_to?'](args['$[]'](0), $$$('Array'), "to_ary"); 18467 if (!$truthy(ary['$nil?']())) { 18468 args = ary.$to_a(); 18469 } } 18470 var result = '', 18471 //used for slicing: 18472 begin_slice = 0, 18473 end_slice, 18474 //used for iterating over the format string: 18475 i, 18476 len = format_string.length, 18477 //used for processing field values: 18478 arg, 18479 str, 18480 //used for processing %g and %G fields: 18481 exponent, 18482 //used for keeping track of width and precision: 18483 width, 18484 precision, 18485 //used for holding temporary values: 18486 tmp_num, 18487 //used for processing %{} and %<> fileds: 18488 hash_parameter_key, 18489 closing_brace_char, 18490 //used for processing %b, %B, %o, %x, and %X fields: 18491 base_number, 18492 base_prefix, 18493 base_neg_zero_regex, 18494 base_neg_zero_digit, 18495 //used for processing arguments: 18496 next_arg, 18497 seq_arg_num = 1, 18498 pos_arg_num = 0, 18499 //used for keeping track of flags: 18500 flags, 18501 FNONE = 0, 18502 FSHARP = 1, 18503 FMINUS = 2, 18504 FPLUS = 4, 18505 FZERO = 8, 18506 FSPACE = 16, 18507 FWIDTH = 32, 18508 FPREC = 64, 18509 FPREC0 = 128; 18510 18511 function CHECK_FOR_FLAGS() { 18512 if (flags&FWIDTH) { $Kernel.$raise($$$('ArgumentError'), "flag after width"); } 18513 if (flags&FPREC0) { $Kernel.$raise($$$('ArgumentError'), "flag after precision"); } 18514 } 18515 18516 function CHECK_FOR_WIDTH() { 18517 if (flags&FWIDTH) { $Kernel.$raise($$$('ArgumentError'), "width given twice"); } 18518 if (flags&FPREC0) { $Kernel.$raise($$$('ArgumentError'), "width after precision"); } 18519 } 18520 18521 function GET_NTH_ARG(num) { 18522 if (num >= args.length) { $Kernel.$raise($$$('ArgumentError'), "too few arguments"); } 18523 return args[num]; 18524 } 18525 18526 function GET_NEXT_ARG() { 18527 switch (pos_arg_num) { 18528 case -1: $Kernel.$raise($$$('ArgumentError'), "unnumbered(" + (seq_arg_num) + ") mixed with numbered"); // raise 18529 case -2: $Kernel.$raise($$$('ArgumentError'), "unnumbered(" + (seq_arg_num) + ") mixed with named"); // raise 18530 } 18531 pos_arg_num = seq_arg_num++; 18532 return GET_NTH_ARG(pos_arg_num - 1); 18533 } 18534 18535 function GET_POS_ARG(num) { 18536 if (pos_arg_num > 0) { 18537 $Kernel.$raise($$$('ArgumentError'), "numbered(" + (num) + ") after unnumbered(" + (pos_arg_num) + ")"); 18538 } 18539 if (pos_arg_num === -2) { 18540 $Kernel.$raise($$$('ArgumentError'), "numbered(" + (num) + ") after named"); 18541 } 18542 if (num < 1) { 18543 $Kernel.$raise($$$('ArgumentError'), "invalid index - " + (num) + "$"); 18544 } 18545 pos_arg_num = -1; 18546 return GET_NTH_ARG(num - 1); 18547 } 18548 18549 function GET_ARG() { 18550 return (next_arg === undefined ? GET_NEXT_ARG() : next_arg); 18551 } 18552 18553 function READ_NUM(label) { 18554 var num, str = ''; 18555 for (;; i++) { 18556 if (i === len) { 18557 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %*[0-9]"); 18558 } 18559 if (format_string.charCodeAt(i) < 48 || format_string.charCodeAt(i) > 57) { 18560 i--; 18561 num = parseInt(str, 10) || 0; 18562 if (num > 2147483647) { 18563 $Kernel.$raise($$$('ArgumentError'), "" + (label) + " too big"); 18564 } 18565 return num; 18566 } 18567 str += format_string.charAt(i); 18568 } 18569 } 18570 18571 function READ_NUM_AFTER_ASTER(label) { 18572 var arg, num = READ_NUM(label); 18573 if (format_string.charAt(i + 1) === '$') { 18574 i++; 18575 arg = GET_POS_ARG(num); 18576 } else { 18577 arg = GET_NEXT_ARG(); 18578 } 18579 return (arg).$to_int(); 18580 } 18581 18582 for (i = format_string.indexOf('%'); i !== -1; i = format_string.indexOf('%', i)) { 18583 str = undefined; 18584 18585 flags = FNONE; 18586 width = -1; 18587 precision = -1; 18588 next_arg = undefined; 18589 18590 end_slice = i; 18591 18592 i++; 18593 18594 switch (format_string.charAt(i)) { 18595 case '%': 18596 begin_slice = i; 18597 // no-break 18598 case '': 18599 case '\n': 18600 case '\0': 18601 i++; 18602 continue; 18603 } 18604 18605 format_sequence: for (; i < len; i++) { 18606 switch (format_string.charAt(i)) { 18607 18608 case ' ': 18609 CHECK_FOR_FLAGS(); 18610 flags |= FSPACE; 18611 continue format_sequence; 18612 18613 case '#': 18614 CHECK_FOR_FLAGS(); 18615 flags |= FSHARP; 18616 continue format_sequence; 18617 18618 case '+': 18619 CHECK_FOR_FLAGS(); 18620 flags |= FPLUS; 18621 continue format_sequence; 18622 18623 case '-': 18624 CHECK_FOR_FLAGS(); 18625 flags |= FMINUS; 18626 continue format_sequence; 18627 18628 case '0': 18629 CHECK_FOR_FLAGS(); 18630 flags |= FZERO; 18631 continue format_sequence; 18632 18633 case '1': 18634 case '2': 18635 case '3': 18636 case '4': 18637 case '5': 18638 case '6': 18639 case '7': 18640 case '8': 18641 case '9': 18642 tmp_num = READ_NUM('width'); 18643 if (format_string.charAt(i + 1) === '$') { 18644 if (i + 2 === len) { 18645 str = '%'; 18646 i++; 18647 break format_sequence; 18648 } 18649 if (next_arg !== undefined) { 18650 $Kernel.$raise($$$('ArgumentError'), "value given twice - %" + (tmp_num) + "$"); 18651 } 18652 next_arg = GET_POS_ARG(tmp_num); 18653 i++; 18654 } else { 18655 CHECK_FOR_WIDTH(); 18656 flags |= FWIDTH; 18657 width = tmp_num; 18658 } 18659 continue format_sequence; 18660 18661 case '<': 18662 case '\{': 18663 closing_brace_char = (format_string.charAt(i) === '<' ? '>' : '\}'); 18664 hash_parameter_key = ''; 18665 18666 i++; 18667 18668 for (;; i++) { 18669 if (i === len) { 18670 $Kernel.$raise($$$('ArgumentError'), "malformed name - unmatched parenthesis"); 18671 } 18672 if (format_string.charAt(i) === closing_brace_char) { 18673 18674 if (pos_arg_num > 0) { 18675 $Kernel.$raise($$$('ArgumentError'), "named " + (hash_parameter_key) + " after unnumbered(" + (pos_arg_num) + ")"); 18676 } 18677 if (pos_arg_num === -1) { 18678 $Kernel.$raise($$$('ArgumentError'), "named " + (hash_parameter_key) + " after numbered"); 18679 } 18680 pos_arg_num = -2; 18681 18682 if (args[0] === undefined || !args[0].$$is_hash) { 18683 $Kernel.$raise($$$('ArgumentError'), "one hash required"); 18684 } 18685 18686 next_arg = (args[0]).$fetch(hash_parameter_key); 18687 18688 if (closing_brace_char === '>') { 18689 continue format_sequence; 18690 } else { 18691 str = next_arg.toString(); 18692 if (precision !== -1) { str = str.slice(0, precision); } 18693 if (flags&FMINUS) { 18694 while (str.length < width) { str = str + ' '; } 18695 } else { 18696 while (str.length < width) { str = ' ' + str; } 18697 } 18698 break format_sequence; 18699 } 18700 } 18701 hash_parameter_key += format_string.charAt(i); 18702 } 18703 // raise 18704 18705 case '*': 18706 i++; 18707 CHECK_FOR_WIDTH(); 18708 flags |= FWIDTH; 18709 width = READ_NUM_AFTER_ASTER('width'); 18710 if (width < 0) { 18711 flags |= FMINUS; 18712 width = -width; 18713 } 18714 continue format_sequence; 18715 18716 case '.': 18717 if (flags&FPREC0) { 18718 $Kernel.$raise($$$('ArgumentError'), "precision given twice"); 18719 } 18720 flags |= FPREC|FPREC0; 18721 precision = 0; 18722 i++; 18723 if (format_string.charAt(i) === '*') { 18724 i++; 18725 precision = READ_NUM_AFTER_ASTER('precision'); 18726 if (precision < 0) { 18727 flags &= ~FPREC; 18728 } 18729 continue format_sequence; 18730 } 18731 precision = READ_NUM('precision'); 18732 continue format_sequence; 18733 18734 case 'd': 18735 case 'i': 18736 case 'u': 18737 arg = $Kernel.$Integer(GET_ARG()); 18738 if (arg >= 0) { 18739 str = arg.toString(); 18740 while (str.length < precision) { str = '0' + str; } 18741 if (flags&FMINUS) { 18742 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18743 while (str.length < width) { str = str + ' '; } 18744 } else { 18745 if (flags&FZERO && precision === -1) { 18746 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; } 18747 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18748 } else { 18749 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18750 while (str.length < width) { str = ' ' + str; } 18751 } 18752 } 18753 } else { 18754 str = (-arg).toString(); 18755 while (str.length < precision) { str = '0' + str; } 18756 if (flags&FMINUS) { 18757 str = '-' + str; 18758 while (str.length < width) { str = str + ' '; } 18759 } else { 18760 if (flags&FZERO && precision === -1) { 18761 while (str.length < width - 1) { str = '0' + str; } 18762 str = '-' + str; 18763 } else { 18764 str = '-' + str; 18765 while (str.length < width) { str = ' ' + str; } 18766 } 18767 } 18768 } 18769 break format_sequence; 18770 18771 case 'b': 18772 case 'B': 18773 case 'o': 18774 case 'x': 18775 case 'X': 18776 switch (format_string.charAt(i)) { 18777 case 'b': 18778 case 'B': 18779 base_number = 2; 18780 base_prefix = '0b'; 18781 base_neg_zero_regex = /^1+/; 18782 base_neg_zero_digit = '1'; 18783 break; 18784 case 'o': 18785 base_number = 8; 18786 base_prefix = '0'; 18787 base_neg_zero_regex = /^3?7+/; 18788 base_neg_zero_digit = '7'; 18789 break; 18790 case 'x': 18791 case 'X': 18792 base_number = 16; 18793 base_prefix = '0x'; 18794 base_neg_zero_regex = /^f+/; 18795 base_neg_zero_digit = 'f'; 18796 break; 18797 } 18798 arg = $Kernel.$Integer(GET_ARG()); 18799 if (arg >= 0) { 18800 str = arg.toString(base_number); 18801 while (str.length < precision) { str = '0' + str; } 18802 if (flags&FMINUS) { 18803 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18804 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 18805 while (str.length < width) { str = str + ' '; } 18806 } else { 18807 if (flags&FZERO && precision === -1) { 18808 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0) - ((flags&FSHARP && arg !== 0) ? base_prefix.length : 0)) { str = '0' + str; } 18809 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 18810 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18811 } else { 18812 if (flags&FSHARP && arg !== 0) { str = base_prefix + str; } 18813 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18814 while (str.length < width) { str = ' ' + str; } 18815 } 18816 } 18817 } else { 18818 if (flags&FPLUS || flags&FSPACE) { 18819 str = (-arg).toString(base_number); 18820 while (str.length < precision) { str = '0' + str; } 18821 if (flags&FMINUS) { 18822 if (flags&FSHARP) { str = base_prefix + str; } 18823 str = '-' + str; 18824 while (str.length < width) { str = str + ' '; } 18825 } else { 18826 if (flags&FZERO && precision === -1) { 18827 while (str.length < width - 1 - (flags&FSHARP ? 2 : 0)) { str = '0' + str; } 18828 if (flags&FSHARP) { str = base_prefix + str; } 18829 str = '-' + str; 18830 } else { 18831 if (flags&FSHARP) { str = base_prefix + str; } 18832 str = '-' + str; 18833 while (str.length < width) { str = ' ' + str; } 18834 } 18835 } 18836 } else { 18837 str = (arg >>> 0).toString(base_number).replace(base_neg_zero_regex, base_neg_zero_digit); 18838 while (str.length < precision - 2) { str = base_neg_zero_digit + str; } 18839 if (flags&FMINUS) { 18840 str = '..' + str; 18841 if (flags&FSHARP) { str = base_prefix + str; } 18842 while (str.length < width) { str = str + ' '; } 18843 } else { 18844 if (flags&FZERO && precision === -1) { 18845 while (str.length < width - 2 - (flags&FSHARP ? base_prefix.length : 0)) { str = base_neg_zero_digit + str; } 18846 str = '..' + str; 18847 if (flags&FSHARP) { str = base_prefix + str; } 18848 } else { 18849 str = '..' + str; 18850 if (flags&FSHARP) { str = base_prefix + str; } 18851 while (str.length < width) { str = ' ' + str; } 18852 } 18853 } 18854 } 18855 } 18856 if (format_string.charAt(i) === format_string.charAt(i).toUpperCase()) { 18857 str = str.toUpperCase(); 18858 } 18859 break format_sequence; 18860 18861 case 'f': 18862 case 'e': 18863 case 'E': 18864 case 'g': 18865 case 'G': 18866 arg = $Kernel.$Float(GET_ARG()); 18867 if (arg >= 0 || isNaN(arg)) { 18868 if (arg === Infinity) { 18869 str = 'Inf'; 18870 } else { 18871 switch (format_string.charAt(i)) { 18872 case 'f': 18873 str = arg.toFixed(precision === -1 ? 6 : precision); 18874 break; 18875 case 'e': 18876 case 'E': 18877 str = arg.toExponential(precision === -1 ? 6 : precision); 18878 break; 18879 case 'g': 18880 case 'G': 18881 str = arg.toExponential(); 18882 exponent = parseInt(str.split('e')[1], 10); 18883 if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) { 18884 str = arg.toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision); 18885 } 18886 break; 18887 } 18888 } 18889 if (flags&FMINUS) { 18890 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18891 while (str.length < width) { str = str + ' '; } 18892 } else { 18893 if (flags&FZERO && arg !== Infinity && !isNaN(arg)) { 18894 while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; } 18895 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18896 } else { 18897 if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; } 18898 while (str.length < width) { str = ' ' + str; } 18899 } 18900 } 18901 } else { 18902 if (arg === -Infinity) { 18903 str = 'Inf'; 18904 } else { 18905 switch (format_string.charAt(i)) { 18906 case 'f': 18907 str = (-arg).toFixed(precision === -1 ? 6 : precision); 18908 break; 18909 case 'e': 18910 case 'E': 18911 str = (-arg).toExponential(precision === -1 ? 6 : precision); 18912 break; 18913 case 'g': 18914 case 'G': 18915 str = (-arg).toExponential(); 18916 exponent = parseInt(str.split('e')[1], 10); 18917 if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) { 18918 str = (-arg).toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision); 18919 } 18920 break; 18921 } 18922 } 18923 if (flags&FMINUS) { 18924 str = '-' + str; 18925 while (str.length < width) { str = str + ' '; } 18926 } else { 18927 if (flags&FZERO && arg !== -Infinity) { 18928 while (str.length < width - 1) { str = '0' + str; } 18929 str = '-' + str; 18930 } else { 18931 str = '-' + str; 18932 while (str.length < width) { str = ' ' + str; } 18933 } 18934 } 18935 } 18936 if (format_string.charAt(i) === format_string.charAt(i).toUpperCase() && arg !== Infinity && arg !== -Infinity && !isNaN(arg)) { 18937 str = str.toUpperCase(); 18938 } 18939 str = str.replace(/([eE][-+]?)([0-9])$/, '$10$2'); 18940 break format_sequence; 18941 18942 case 'a': 18943 case 'A': 18944 // Not implemented because there are no specs for this field type. 18945 $Kernel.$raise($$$('NotImplementedError'), "`A` and `a` format field types are not implemented in Opal yet"); 18946 // raise 18947 18948 case 'c': 18949 arg = GET_ARG(); 18950 if ((arg)['$respond_to?']("to_ary")) { arg = (arg).$to_ary()[0]; } 18951 if ((arg)['$respond_to?']("to_str")) { 18952 str = (arg).$to_str(); 18953 } else { 18954 str = String.fromCharCode($coerce_to(arg, $$$('Integer'), 'to_int')); 18955 } 18956 if (str.length !== 1) { 18957 $Kernel.$raise($$$('ArgumentError'), "%c requires a character"); 18958 } 18959 if (flags&FMINUS) { 18960 while (str.length < width) { str = str + ' '; } 18961 } else { 18962 while (str.length < width) { str = ' ' + str; } 18963 } 18964 break format_sequence; 18965 18966 case 'p': 18967 str = (GET_ARG()).$inspect(); 18968 if (precision !== -1) { str = str.slice(0, precision); } 18969 if (flags&FMINUS) { 18970 while (str.length < width) { str = str + ' '; } 18971 } else { 18972 while (str.length < width) { str = ' ' + str; } 18973 } 18974 break format_sequence; 18975 18976 case 's': 18977 str = (GET_ARG()).$to_s(); 18978 if (precision !== -1) { str = str.slice(0, precision); } 18979 if (flags&FMINUS) { 18980 while (str.length < width) { str = str + ' '; } 18981 } else { 18982 while (str.length < width) { str = ' ' + str; } 18983 } 18984 break format_sequence; 18985 18986 default: 18987 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %" + (format_string.charAt(i))); 18988 } 18989 } 18990 18991 if (str === undefined) { 18992 $Kernel.$raise($$$('ArgumentError'), "malformed format string - %"); 18993 } 18994 18995 result += format_string.slice(begin_slice, end_slice) + str; 18996 begin_slice = i + 1; 18997 } 18998 18999 if ($gvars.DEBUG && pos_arg_num >= 0 && seq_arg_num < args.length) { 19000 $Kernel.$raise($$$('ArgumentError'), "too many arguments for format string"); 19001 } 19002 19003 return result + format_string.slice(begin_slice); 19004 }, -2); 19005 return $alias(self, "sprintf", "format"); 19006 })('::') 19007}; 19008 19009Opal.modules["corelib/string/encoding"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19010 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.$$$; 19011 19012 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='); 19013 19014 self.$require("corelib/string"); 19015 (function($base, $super) { 19016 var self = $klass($base, $super, 'Encoding'); 19017 19018 var $proto = self.$$prototype; 19019 19020 $proto.name = $proto.dummy = nil; 19021 19022 $defs(self, '$register', function $$register(name, options) { 19023 var block = $$register.$$p || nil, self = this, names = nil, $ret_or_1 = nil, ascii = nil, dummy = nil, encoding = nil, register = nil; 19024 19025 $$register.$$p = null; 19026 if (options == null) options = $hash2([], {}); 19027 names = $rb_plus([name], ($truthy(($ret_or_1 = options['$[]']("aliases"))) ? ($ret_or_1) : ([]))); 19028 ascii = ($truthy(($ret_or_1 = options['$[]']("ascii"))) && ($ret_or_1)); 19029 dummy = ($truthy(($ret_or_1 = options['$[]']("dummy"))) && ($ret_or_1)); 19030 if ($truthy(options['$[]']("inherits"))) { 19031 19032 encoding = options['$[]']("inherits").$clone(); 19033 encoding.$initialize(name, names, ascii, dummy); 19034 } else { 19035 encoding = self.$new(name, names, ascii, dummy); 19036 } if ((block !== nil)) { 19037 $send(encoding, 'instance_eval', [], block.$to_proc()); 19038 } register = Opal.encodings; 19039 return $send(names, 'each', [], function $$1(encoding_name){var self = $$1.$$s == null ? this : $$1.$$s; 19040 19041 19042 if (encoding_name == null) encoding_name = nil; 19043 self.$const_set(encoding_name.$tr("-", "_"), encoding); 19044 return register[encoding_name] = encoding;}, {$$s: self}); 19045 }, -2); 19046 $defs(self, '$find', function $$find(name) { 19047 var self = this; 19048 19049 19050 if ($eqeq(name, "default_external")) { 19051 return self.$default_external() 19052 } return Opal.find_encoding(name); }); 19053 self.$singleton_class().$attr_accessor("default_external"); 19054 self.$attr_reader("name", "names"); 19055 19056 $def(self, '$initialize', function $$initialize(name, names, ascii, dummy) { 19057 var self = this; 19058 19059 19060 self.name = name; 19061 self.names = names; 19062 self.ascii = ascii; 19063 return (self.dummy = dummy); 19064 }); 19065 19066 $def(self, '$ascii_compatible?', $return_ivar("ascii")); 19067 19068 $def(self, '$dummy?', $return_ivar("dummy")); 19069 19070 $def(self, '$binary?', $return_val(false)); 19071 19072 $def(self, '$to_s', $return_ivar("name")); 19073 19074 $def(self, '$inspect', function $$inspect() { 19075 var self = this; 19076 19077 return "#<Encoding:" + (self.name) + (($truthy(self.dummy) ? (" (dummy)") : nil)) + ">" 19078 }); 19079 19080 $def(self, '$charsize', function $$charsize(string) { 19081 19082 19083 var len = 0; 19084 for (var i = 0, length = string.length; i < length; i++) { 19085 var charcode = string.charCodeAt(i); 19086 if (!(charcode >= 0xD800 && charcode <= 0xDBFF)) { 19087 len++; 19088 } 19089 } 19090 return len; 19091 19092 }); 19093 19094 $def(self, '$each_char', function $$each_char(string) { 19095 var block = $$each_char.$$p || nil; 19096 19097 $$each_char.$$p = null; 19098 19099 var low_surrogate = ""; 19100 for (var i = 0, length = string.length; i < length; i++) { 19101 var charcode = string.charCodeAt(i); 19102 var chr = string.charAt(i); 19103 if (charcode >= 0xDC00 && charcode <= 0xDFFF) { 19104 low_surrogate = chr; 19105 continue; 19106 } 19107 else if (charcode >= 0xD800 && charcode <= 0xDBFF) { 19108 chr = low_surrogate + chr; 19109 } 19110 if (string.encoding.name != "UTF-8") { 19111 chr = new String(chr); 19112 chr.encoding = string.encoding; 19113 } 19114 Opal.yield1(block, chr); 19115 } 19116 }); 19117 19118 $def(self, '$each_byte', function $$each_byte($a) { 19119 19120 19121 $slice(arguments); 19122 return $Kernel.$raise($$$('NotImplementedError')); 19123 }, -1); 19124 19125 $def(self, '$bytesize', function $$bytesize($a) { 19126 19127 19128 $slice(arguments); 19129 return $Kernel.$raise($$$('NotImplementedError')); 19130 }, -1); 19131 $klass('::', $$$('StandardError'), 'EncodingError'); 19132 return ($klass('::', $$$('EncodingError'), 'CompatibilityError'), nil); 19133 })('::', null); 19134 $send($$$('Encoding'), 'register', ["UTF-8", $hash2(["aliases", "ascii"], {"aliases": ["CP65001"], "ascii": true})], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 19135 19136 19137 19138 $def(self, '$each_byte', function $$each_byte(string) { 19139 var block = $$each_byte.$$p || nil; 19140 19141 $$each_byte.$$p = null; 19142 19143 // Taken from: https://github.com/feross/buffer/blob/f52dffd9df0445b93c0c9065c2f8f0f46b2c729a/index.js#L1954-L2032 19144 var units = Infinity; 19145 var codePoint; 19146 var length = string.length; 19147 var leadSurrogate = null; 19148 19149 for (var i = 0; i < length; ++i) { 19150 codePoint = string.charCodeAt(i); 19151 19152 // is surrogate component 19153 if (codePoint > 0xD7FF && codePoint < 0xE000) { 19154 // last char was a lead 19155 if (!leadSurrogate) { 19156 // no lead yet 19157 if (codePoint > 0xDBFF) { 19158 // unexpected trail 19159 if ((units -= 3) > -1) { 19160 Opal.yield1(block, 0xEF); 19161 Opal.yield1(block, 0xBF); 19162 Opal.yield1(block, 0xBD); 19163 } 19164 continue; 19165 } else if (i + 1 === length) { 19166 // unpaired lead 19167 if ((units -= 3) > -1) { 19168 Opal.yield1(block, 0xEF); 19169 Opal.yield1(block, 0xBF); 19170 Opal.yield1(block, 0xBD); 19171 } 19172 continue; 19173 } 19174 19175 // valid lead 19176 leadSurrogate = codePoint; 19177 19178 continue; 19179 } 19180 19181 // 2 leads in a row 19182 if (codePoint < 0xDC00) { 19183 if ((units -= 3) > -1) { 19184 Opal.yield1(block, 0xEF); 19185 Opal.yield1(block, 0xBF); 19186 Opal.yield1(block, 0xBD); 19187 } 19188 leadSurrogate = codePoint; 19189 continue; 19190 } 19191 19192 // valid surrogate pair 19193 codePoint = (leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00) + 0x10000; 19194 } else if (leadSurrogate) { 19195 // valid bmp char, but last char was a lead 19196 if ((units -= 3) > -1) { 19197 Opal.yield1(block, 0xEF); 19198 Opal.yield1(block, 0xBF); 19199 Opal.yield1(block, 0xBD); 19200 } 19201 } 19202 19203 leadSurrogate = null; 19204 19205 // encode utf8 19206 if (codePoint < 0x80) { 19207 if ((units -= 1) < 0) break; 19208 Opal.yield1(block, codePoint); 19209 } else if (codePoint < 0x800) { 19210 if ((units -= 2) < 0) break; 19211 Opal.yield1(block, codePoint >> 0x6 | 0xC0); 19212 Opal.yield1(block, codePoint & 0x3F | 0x80); 19213 } else if (codePoint < 0x10000) { 19214 if ((units -= 3) < 0) break; 19215 Opal.yield1(block, codePoint >> 0xC | 0xE0); 19216 Opal.yield1(block, codePoint >> 0x6 & 0x3F | 0x80); 19217 Opal.yield1(block, codePoint & 0x3F | 0x80); 19218 } else if (codePoint < 0x110000) { 19219 if ((units -= 4) < 0) break; 19220 Opal.yield1(block, codePoint >> 0x12 | 0xF0); 19221 Opal.yield1(block, codePoint >> 0xC & 0x3F | 0x80); 19222 Opal.yield1(block, codePoint >> 0x6 & 0x3F | 0x80); 19223 Opal.yield1(block, codePoint & 0x3F | 0x80); 19224 } else ; 19225 } 19226 }); 19227 return $def(self, '$bytesize', function $$bytesize(string) { 19228 19229 return string.$bytes().$length() 19230 });}, {$$s: self}); 19231 $send($$$('Encoding'), 'register', ["UTF-16LE"], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 19232 19233 19234 19235 $def(self, '$each_byte', function $$each_byte(string) { 19236 var block = $$each_byte.$$p || nil; 19237 19238 $$each_byte.$$p = null; 19239 19240 for (var i = 0, length = string.length; i < length; i++) { 19241 var code = string.charCodeAt(i); 19242 19243 Opal.yield1(block, code & 0xff); 19244 Opal.yield1(block, code >> 8); 19245 } 19246 }); 19247 return $def(self, '$bytesize', function $$bytesize(string) { 19248 19249 return string.length * 2; 19250 });}, {$$s: self}); 19251 $send($$$('Encoding'), 'register', ["UTF-16BE", $hash2(["inherits"], {"inherits": $$$($$$('Encoding'), 'UTF_16LE')})], function $$4(){var self = $$4.$$s == null ? this : $$4.$$s; 19252 19253 return $def(self, '$each_byte', function $$each_byte(string) { 19254 var block = $$each_byte.$$p || nil; 19255 19256 $$each_byte.$$p = null; 19257 19258 for (var i = 0, length = string.length; i < length; i++) { 19259 var code = string.charCodeAt(i); 19260 19261 Opal.yield1(block, code >> 8); 19262 Opal.yield1(block, code & 0xff); 19263 } 19264 })}, {$$s: self}); 19265 $send($$$('Encoding'), 'register', ["UTF-32LE"], function $$5(){var self = $$5.$$s == null ? this : $$5.$$s; 19266 19267 19268 19269 $def(self, '$each_byte', function $$each_byte(string) { 19270 var block = $$each_byte.$$p || nil; 19271 19272 $$each_byte.$$p = null; 19273 19274 for (var i = 0, length = string.length; i < length; i++) { 19275 var code = string.charCodeAt(i); 19276 19277 Opal.yield1(block, code & 0xff); 19278 Opal.yield1(block, code >> 8); 19279 Opal.yield1(block, 0); 19280 Opal.yield1(block, 0); 19281 } 19282 }); 19283 return $def(self, '$bytesize', function $$bytesize(string) { 19284 19285 return string.length * 4; 19286 });}, {$$s: self}); 19287 $send($$$('Encoding'), 'register', ["UTF-32BE", $hash2(["inherits"], {"inherits": $$$($$$('Encoding'), 'UTF_32LE')})], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s; 19288 19289 return $def(self, '$each_byte', function $$each_byte(string) { 19290 var block = $$each_byte.$$p || nil; 19291 19292 $$each_byte.$$p = null; 19293 19294 for (var i = 0, length = string.length; i < length; i++) { 19295 var code = string.charCodeAt(i); 19296 19297 Opal.yield1(block, 0); 19298 Opal.yield1(block, 0); 19299 Opal.yield1(block, code >> 8); 19300 Opal.yield1(block, code & 0xff); 19301 } 19302 })}, {$$s: self}); 19303 $send($$$('Encoding'), 'register', ["ASCII-8BIT", $hash2(["aliases", "ascii"], {"aliases": ["BINARY"], "ascii": true})], function $$7(){var self = $$7.$$s == null ? this : $$7.$$s; 19304 19305 19306 19307 $def(self, '$each_char', function $$each_char(string) { 19308 var block = $$each_char.$$p || nil; 19309 19310 $$each_char.$$p = null; 19311 19312 for (var i = 0, length = string.length; i < length; i++) { 19313 var chr = new String(string.charAt(i)); 19314 chr.encoding = string.encoding; 19315 Opal.yield1(block, chr); 19316 } 19317 }); 19318 19319 $def(self, '$charsize', function $$charsize(string) { 19320 19321 return string.length; 19322 }); 19323 19324 $def(self, '$each_byte', function $$each_byte(string) { 19325 var block = $$each_byte.$$p || nil; 19326 19327 $$each_byte.$$p = null; 19328 19329 for (var i = 0, length = string.length; i < length; i++) { 19330 var code = string.charCodeAt(i); 19331 Opal.yield1(block, code & 0xff); 19332 } 19333 }); 19334 19335 $def(self, '$bytesize', function $$bytesize(string) { 19336 19337 return string.length; 19338 }); 19339 return $def(self, '$binary?', $return_val(true));}, {$$s: self}); 19340 $$$('Encoding').$register("ISO-8859-1", $hash2(["aliases", "ascii", "inherits"], {"aliases": ["ISO8859-1"], "ascii": true, "inherits": $$$($$$('Encoding'), 'ASCII_8BIT')})); 19341 $$$('Encoding').$register("US-ASCII", $hash2(["aliases", "ascii", "inherits"], {"aliases": ["ASCII"], "ascii": true, "inherits": $$$($$$('Encoding'), 'ASCII_8BIT')})); 19342 (function($base, $super) { 19343 var self = $klass($base, $super, 'String'); 19344 19345 var $proto = self.$$prototype; 19346 19347 $proto.internal_encoding = $proto.bytes = $proto.encoding = nil; 19348 19349 self.$attr_reader("encoding"); 19350 self.$attr_reader("internal_encoding"); 19351 Opal.prop(String.prototype, 'bytes', nil); 19352 Opal.prop(String.prototype, 'encoding', $$$($$$('Encoding'), 'UTF_8')); 19353 Opal.prop(String.prototype, 'internal_encoding', $$$($$$('Encoding'), 'UTF_8')); 19354 19355 $def(self, '$b', function $$b() { 19356 var self = this; 19357 19358 return self.$dup().$force_encoding("binary") 19359 }); 19360 19361 $def(self, '$bytesize', function $$bytesize() { 19362 var self = this; 19363 19364 return self.internal_encoding.$bytesize(self) 19365 }); 19366 19367 $def(self, '$each_byte', function $$each_byte() { 19368 var block = $$each_byte.$$p || nil, self = this; 19369 19370 $$each_byte.$$p = null; 19371 if (!(block !== nil)) { 19372 return $send(self, 'enum_for', ["each_byte"], function $$8(){var self = $$8.$$s == null ? this : $$8.$$s; 19373 19374 return self.$bytesize()}, {$$s: self}) 19375 } $send(self.internal_encoding, 'each_byte', [self], block.$to_proc()); 19376 return self; 19377 }); 19378 19379 $def(self, '$bytes', function $$bytes() { 19380 var self = this, $ret_or_1 = nil; 19381 19382 19383 19384 if (typeof self === 'string') { 19385 return (new String(self)).$each_byte().$to_a(); 19386 } 19387 self.bytes = ($truthy(($ret_or_1 = self.bytes)) ? ($ret_or_1) : (self.$each_byte().$to_a())); 19388 return self.bytes.$dup(); 19389 }); 19390 19391 $def(self, '$each_char', function $$each_char() { 19392 var block = $$each_char.$$p || nil, self = this; 19393 19394 $$each_char.$$p = null; 19395 if (!(block !== nil)) { 19396 return $send(self, 'enum_for', ["each_char"], function $$9(){var self = $$9.$$s == null ? this : $$9.$$s; 19397 19398 return self.$length()}, {$$s: self}) 19399 } $send(self.encoding, 'each_char', [self], block.$to_proc()); 19400 return self; 19401 }); 19402 19403 $def(self, '$chars', function $$chars() { 19404 var block = $$chars.$$p || nil, self = this; 19405 19406 $$chars.$$p = null; 19407 if (!$truthy(block)) { 19408 return self.$each_char().$to_a() 19409 } return $send(self, 'each_char', [], block.$to_proc()); 19410 }); 19411 19412 $def(self, '$each_codepoint', function $$each_codepoint() { 19413 var block = $$each_codepoint.$$p || nil, self = this; 19414 19415 $$each_codepoint.$$p = null; 19416 if (!(block !== nil)) { 19417 return self.$enum_for("each_codepoint") 19418 } 19419 for (var i = 0, length = self.length; i < length; i++) { 19420 Opal.yield1(block, self.codePointAt(i)); 19421 } 19422 return self; 19423 }); 19424 19425 $def(self, '$codepoints', function $$codepoints() { 19426 var block = $$codepoints.$$p || nil, self = this; 19427 19428 $$codepoints.$$p = null; 19429 if ((block !== nil)) { 19430 return $send(self, 'each_codepoint', [], block.$to_proc()) 19431 } return self.$each_codepoint().$to_a(); 19432 }); 19433 19434 $def(self, '$encode', function $$encode(encoding) { 19435 var self = this; 19436 19437 return Opal.enc(self, encoding); 19438 }); 19439 19440 $def(self, '$force_encoding', function $$force_encoding(encoding) { 19441 var self = this; 19442 19443 19444 var str = self; 19445 19446 if (encoding === str.encoding) { return str; } 19447 19448 encoding = $Opal['$coerce_to!'](encoding, $$$('String'), "to_s"); 19449 encoding = $$$('Encoding').$find(encoding); 19450 19451 if (encoding === str.encoding) { return str; } 19452 19453 str = Opal.set_encoding(str, encoding); 19454 19455 return str; 19456 19457 }); 19458 19459 $def(self, '$getbyte', function $$getbyte(idx) { 19460 var self = this, string_bytes = nil; 19461 19462 19463 string_bytes = self.$bytes(); 19464 idx = $Opal['$coerce_to!'](idx, $$$('Integer'), "to_int"); 19465 if ($truthy($rb_lt(string_bytes.$length(), idx))) { 19466 return nil 19467 } return string_bytes['$[]'](idx); 19468 }); 19469 19470 $def(self, '$initialize_copy', function $$initialize_copy(other) { 19471 19472 return "\n" + " self.encoding = other.encoding;\n" + " self.internal_encoding = other.internal_encoding;\n" + " " 19473 }); 19474 return $def(self, '$valid_encoding?', $return_val(true)); 19475 })('::', null); 19476 return ($a = [$$$($$('Encoding'), 'UTF_8')], $send($$$('Encoding'), 'default_external=', $a), $a[$a.length - 1]); 19477}; 19478 19479Opal.modules["corelib/math"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19480 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.$$$; 19481 19482 Opal.add_stubs('new,raise,Float,Integer,module_function,each,define_method,checked,float!,===,gamma,-,integer!,/,infinite?'); 19483 return (function($base, $parent_nesting) { 19484 var self = $module($base, 'Math'); 19485 19486 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 19487 19488 19489 $const_set(self, 'E', Math.E); 19490 $const_set(self, 'PI', Math.PI); 19491 $const_set(self, 'DomainError', $Class.$new($$$('StandardError'))); 19492 $defs(self, '$checked', function $$checked(method, $a) { 19493 var $post_args, args; 19494 19495 19496 $post_args = $slice(arguments, 1); 19497 args = $post_args; 19498 19499 if (isNaN(args[0]) || (args.length == 2 && isNaN(args[1]))) { 19500 return NaN; 19501 } 19502 19503 var result = Math[method].apply(null, args); 19504 19505 if (isNaN(result)) { 19506 $Kernel.$raise($$('DomainError'), "Numerical argument is out of domain - \"" + (method) + "\""); 19507 } 19508 19509 return result; 19510 }, -2); 19511 $defs(self, '$float!', function $Math_float$excl$1(value) { 19512 19513 try { 19514 return $Kernel.$Float(value) 19515 } catch ($err) { 19516 if (Opal.rescue($err, [$$$('ArgumentError')])) { 19517 try { 19518 return $Kernel.$raise($type_error(value, $$$('Float'))) 19519 } finally { Opal.pop_exception(); } 19520 } else { throw $err; } 19521 } 19522 }); 19523 $defs(self, '$integer!', function $Math_integer$excl$2(value) { 19524 19525 try { 19526 return $Kernel.$Integer(value) 19527 } catch ($err) { 19528 if (Opal.rescue($err, [$$$('ArgumentError')])) { 19529 try { 19530 return $Kernel.$raise($type_error(value, $$$('Integer'))) 19531 } finally { Opal.pop_exception(); } 19532 } else { throw $err; } 19533 } 19534 }); 19535 self.$module_function(); 19536 if (!$truthy((typeof(Math.erf) !== "undefined"))) { 19537 19538 Opal.prop(Math, 'erf', function(x) { 19539 var A1 = 0.254829592, 19540 A2 = -0.284496736, 19541 A3 = 1.421413741, 19542 A4 = -1.453152027, 19543 A5 = 1.061405429, 19544 P = 0.3275911; 19545 19546 var sign = 1; 19547 19548 if (x < 0) { 19549 sign = -1; 19550 } 19551 19552 x = Math.abs(x); 19553 19554 var t = 1.0 / (1.0 + P * x); 19555 var y = 1.0 - (((((A5 * t + A4) * t) + A3) * t + A2) * t + A1) * t * Math.exp(-x * x); 19556 19557 return sign * y; 19558 }); 19559 19560 } if (!$truthy((typeof(Math.erfc) !== "undefined"))) { 19561 19562 Opal.prop(Math, 'erfc', function(x) { 19563 var z = Math.abs(x), 19564 t = 1.0 / (0.5 * z + 1.0); 19565 19566 var A1 = t * 0.17087277 + -0.82215223, 19567 A2 = t * A1 + 1.48851587, 19568 A3 = t * A2 + -1.13520398, 19569 A4 = t * A3 + 0.27886807, 19570 A5 = t * A4 + -0.18628806, 19571 A6 = t * A5 + 0.09678418, 19572 A7 = t * A6 + 0.37409196, 19573 A8 = t * A7 + 1.00002368, 19574 A9 = t * A8, 19575 A10 = -z * z - 1.26551223 + A9; 19576 19577 var a = t * Math.exp(A10); 19578 19579 if (x < 0.0) { 19580 return 2.0 - a; 19581 } 19582 else { 19583 return a; 19584 } 19585 }); 19586 19587 } $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; 19588 19589 19590 if (method == null) method = nil; 19591 return $send(self, 'define_method', [method], function $$4(x){ 19592 19593 if (x == null) x = nil; 19594 return $$$('Math').$checked(method, $$$('Math')['$float!'](x));});}, {$$s: self}); 19595 19596 $def(self, '$atan2', function $$atan2(y, x) { 19597 19598 return $$$('Math').$checked("atan2", $$$('Math')['$float!'](y), $$$('Math')['$float!'](x)) 19599 }); 19600 19601 $def(self, '$hypot', function $$hypot(x, y) { 19602 19603 return $$$('Math').$checked("hypot", $$$('Math')['$float!'](x), $$$('Math')['$float!'](y)) 19604 }); 19605 19606 $def(self, '$frexp', function $$frexp(x) { 19607 19608 19609 x = $$('Math')['$float!'](x); 19610 19611 if (isNaN(x)) { 19612 return [NaN, 0]; 19613 } 19614 19615 var ex = Math.floor(Math.log(Math.abs(x)) / Math.log(2)) + 1, 19616 frac = x / Math.pow(2, ex); 19617 19618 return [frac, ex]; 19619 }); 19620 19621 $def(self, '$gamma', function $$gamma(n) { 19622 19623 19624 n = $$('Math')['$float!'](n); 19625 19626 var i, t, x, value, result, twoN, threeN, fourN, fiveN; 19627 19628 var G = 4.7421875; 19629 19630 var P = [ 19631 0.99999999999999709182, 19632 57.156235665862923517, 19633 -59.597960355475491248, 19634 14.136097974741747174, 19635 -0.49191381609762019978, 19636 0.33994649984811888699e-4, 19637 0.46523628927048575665e-4, 19638 -0.98374475304879564677e-4, 19639 0.15808870322491248884e-3, 19640 -0.21026444172410488319e-3, 19641 0.21743961811521264320e-3, 19642 -0.16431810653676389022e-3, 19643 0.84418223983852743293e-4, 19644 -0.26190838401581408670e-4, 19645 0.36899182659531622704e-5 19646 ]; 19647 19648 19649 if (isNaN(n)) { 19650 return NaN; 19651 } 19652 19653 if (n === 0 && 1 / n < 0) { 19654 return -Infinity; 19655 } 19656 19657 if (n === -1 || n === -Infinity) { 19658 $Kernel.$raise($$('DomainError'), "Numerical argument is out of domain - \"gamma\""); 19659 } 19660 19661 if ($$('Integer')['$==='](n)) { 19662 if (n <= 0) { 19663 return isFinite(n) ? Infinity : NaN; 19664 } 19665 19666 if (n > 171) { 19667 return Infinity; 19668 } 19669 19670 value = n - 2; 19671 result = n - 1; 19672 19673 while (value > 1) { 19674 result *= value; 19675 value--; 19676 } 19677 19678 if (result == 0) { 19679 result = 1; 19680 } 19681 19682 return result; 19683 } 19684 19685 if (n < 0.5) { 19686 return Math.PI / (Math.sin(Math.PI * n) * $$$('Math').$gamma($rb_minus(1, n))); 19687 } 19688 19689 if (n >= 171.35) { 19690 return Infinity; 19691 } 19692 19693 if (n > 85.0) { 19694 twoN = n * n; 19695 threeN = twoN * n; 19696 fourN = threeN * n; 19697 fiveN = fourN * n; 19698 19699 return Math.sqrt(2 * Math.PI / n) * Math.pow((n / Math.E), n) * 19700 (1 + 1 / (12 * n) + 1 / (288 * twoN) - 139 / (51840 * threeN) - 19701 571 / (2488320 * fourN) + 163879 / (209018880 * fiveN) + 19702 5246819 / (75246796800 * fiveN * n)); 19703 } 19704 19705 n -= 1; 19706 x = P[0]; 19707 19708 for (i = 1; i < P.length; ++i) { 19709 x += P[i] / (n + i); 19710 } 19711 19712 t = n + G + 0.5; 19713 19714 return Math.sqrt(2 * Math.PI) * Math.pow(t, n + 0.5) * Math.exp(-t) * x; 19715 }); 19716 19717 $def(self, '$ldexp', function $$ldexp(mantissa, exponent) { 19718 19719 19720 mantissa = $$('Math')['$float!'](mantissa); 19721 exponent = $$('Math')['$integer!'](exponent); 19722 19723 if (isNaN(exponent)) { 19724 $Kernel.$raise($$$('RangeError'), "float NaN out of range of integer"); 19725 } 19726 19727 return mantissa * Math.pow(2, exponent); 19728 }); 19729 19730 $def(self, '$lgamma', function $$lgamma(n) { 19731 19732 19733 if (n == -1) { 19734 return [Infinity, 1]; 19735 } 19736 else { 19737 return [Math.log(Math.abs($$$('Math').$gamma(n))), $$$('Math').$gamma(n) < 0 ? -1 : 1]; 19738 } 19739 19740 }); 19741 19742 $def(self, '$log', function $$log(x, base) { 19743 if ($eqeqeq($$$('String'), x)) { 19744 $Kernel.$raise($type_error(x, $$$('Float'))); 19745 } if ($truthy(base == null)) { 19746 return $$$('Math').$checked("log", $$$('Math')['$float!'](x)) 19747 } else { 19748 19749 if ($eqeqeq($$$('String'), base)) { 19750 $Kernel.$raise($type_error(base, $$$('Float'))); 19751 } return $rb_divide($$$('Math').$checked("log", $$$('Math')['$float!'](x)), $$$('Math').$checked("log", $$$('Math')['$float!'](base))); 19752 } }, -2); 19753 19754 $def(self, '$log10', function $$log10(x) { 19755 19756 19757 if ($eqeqeq($$$('String'), x)) { 19758 $Kernel.$raise($type_error(x, $$$('Float'))); 19759 } return $$$('Math').$checked("log10", $$$('Math')['$float!'](x)); 19760 }); 19761 19762 $def(self, '$log2', function $$log2(x) { 19763 19764 19765 if ($eqeqeq($$$('String'), x)) { 19766 $Kernel.$raise($type_error(x, $$$('Float'))); 19767 } return $$$('Math').$checked("log2", $$$('Math')['$float!'](x)); 19768 }); 19769 return $def(self, '$tan', function $$tan(x) { 19770 19771 19772 x = $$$('Math')['$float!'](x); 19773 if ($truthy(x['$infinite?']())) { 19774 return $$$($$$('Float'), 'NAN') 19775 } return $$$('Math').$checked("tan", $$$('Math')['$float!'](x)); 19776 }); 19777 })('::', $nesting) 19778}; 19779 19780Opal.modules["corelib/complex/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19781 var $module = Opal.module, $truthy = Opal.truthy, $def = Opal.def, $klass = Opal.klass, $nesting = [], nil = Opal.nil; 19782 19783 Opal.add_stubs('new,from_string'); 19784 19785 (function($base, $parent_nesting) { 19786 var self = $module($base, 'Kernel'); 19787 19788 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 19789 19790 return $def(self, '$Complex', function $$Complex(real, imag) { 19791 19792 19793 if (imag == null) imag = nil; 19794 if ($truthy(imag)) { 19795 return $$('Complex').$new(real, imag) 19796 } else { 19797 return $$('Complex').$new(real, 0) 19798 } }, -2) 19799 })('::', $nesting); 19800 return (function($base, $super, $parent_nesting) { 19801 var self = $klass($base, $super, 'String'); 19802 19803 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 19804 19805 return $def(self, '$to_c', function $$to_c() { 19806 var self = this; 19807 19808 return $$('Complex').$from_string(self) 19809 }) 19810 })('::', null, $nesting); 19811}; 19812 19813Opal.modules["corelib/complex"] = function(Opal) {/* Generated by Opal 1.7.3 */ 19814 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.$$$; 19815 19816 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'); 19817 19818 self.$require("corelib/numeric"); 19819 self.$require("corelib/complex/base"); 19820 return (function($base, $super, $parent_nesting) { 19821 var self = $klass($base, $super, 'Complex'); 19822 19823 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 19824 19825 $proto.real = $proto.imag = nil; 19826 19827 $defs(self, '$rect', function $$rect(real, imag) { 19828 var self = this; 19829 19830 19831 if (imag == null) imag = 0; 19832 if (!((($eqeqeq($$$('Numeric'), real) && ($truthy(real['$real?']()))) && ($eqeqeq($$$('Numeric'), imag))) && ($truthy(imag['$real?']())))) { 19833 $Kernel.$raise($$$('TypeError'), "not a real"); 19834 } return self.$new(real, imag); 19835 }, -2); 19836 $defs(self, '$polar', function $$polar(r, theta) { 19837 var self = this; 19838 19839 19840 if (theta == null) theta = 0; 19841 if (!((($eqeqeq($$$('Numeric'), r) && ($truthy(r['$real?']()))) && ($eqeqeq($$$('Numeric'), theta))) && ($truthy(theta['$real?']())))) { 19842 $Kernel.$raise($$$('TypeError'), "not a real"); 19843 } return self.$new($rb_times(r, $$$('Math').$cos(theta)), $rb_times(r, $$$('Math').$sin(theta))); 19844 }, -2); 19845 self.$attr_reader("real", "imag"); 19846 19847 $def(self, '$initialize', function $$initialize(real, imag) { 19848 var self = this; 19849 19850 19851 if (imag == null) imag = 0; 19852 self.real = real; 19853 self.imag = imag; 19854 return self.$freeze(); 19855 }, -2); 19856 19857 $def(self, '$coerce', function $$coerce(other) { 19858 var self = this; 19859 19860 if ($eqeqeq($$$('Complex'), other)) { 19861 return [other, self] 19862 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19863 return [$$$('Complex').$new(other, 0), self] 19864 } else { 19865 return $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Complex") 19866 } 19867 }); 19868 19869 $def(self, '$==', function $Complex_$eq_eq$1(other) { 19870 var self = this, $ret_or_1 = nil; 19871 19872 if ($eqeqeq($$$('Complex'), other)) { 19873 if ($truthy(($ret_or_1 = self.real['$=='](other.$real())))) { 19874 return self.imag['$=='](other.$imag()) 19875 } else { 19876 return $ret_or_1 19877 } 19878 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19879 if ($truthy(($ret_or_1 = self.real['$=='](other)))) { 19880 return self.imag['$=='](0) 19881 } else { 19882 return $ret_or_1 19883 } 19884 } else { 19885 return other['$=='](self) 19886 } 19887 }); 19888 19889 $def(self, '$-@', function $Complex_$minus$$2() { 19890 var self = this; 19891 19892 return $Kernel.$Complex(self.real['$-@'](), self.imag['$-@']()) 19893 }); 19894 19895 $def(self, '$+', function $Complex_$plus$3(other) { 19896 var self = this; 19897 19898 if ($eqeqeq($$$('Complex'), other)) { 19899 return $Kernel.$Complex($rb_plus(self.real, other.$real()), $rb_plus(self.imag, other.$imag())) 19900 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19901 return $Kernel.$Complex($rb_plus(self.real, other), self.imag) 19902 } else { 19903 return self.$__coerced__("+", other) 19904 } 19905 }); 19906 19907 $def(self, '$-', function $Complex_$minus$4(other) { 19908 var self = this; 19909 19910 if ($eqeqeq($$$('Complex'), other)) { 19911 return $Kernel.$Complex($rb_minus(self.real, other.$real()), $rb_minus(self.imag, other.$imag())) 19912 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19913 return $Kernel.$Complex($rb_minus(self.real, other), self.imag) 19914 } else { 19915 return self.$__coerced__("-", other) 19916 } 19917 }); 19918 19919 $def(self, '$*', function $Complex_$$5(other) { 19920 var self = this; 19921 19922 if ($eqeqeq($$$('Complex'), other)) { 19923 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()))) 19924 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19925 return $Kernel.$Complex($rb_times(self.real, other), $rb_times(self.imag, other)) 19926 } else { 19927 return self.$__coerced__("*", other) 19928 } 19929 }); 19930 19931 $def(self, '$/', function $Complex_$slash$6(other) { 19932 var self = this; 19933 19934 if ($eqeqeq($$$('Complex'), other)) { 19935 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?']())))))) { 19936 return $$$('Complex').$new($$$($$$('Float'), 'NAN'), $$$($$$('Float'), 'NAN')) 19937 } else { 19938 return $rb_divide($rb_times(self, other.$conj()), other.$abs2()) 19939 } 19940 } else if (($eqeqeq($$$('Numeric'), other) && ($truthy(other['$real?']())))) { 19941 return $Kernel.$Complex(self.real.$quo(other), self.imag.$quo(other)) 19942 } else { 19943 return self.$__coerced__("/", other) 19944 } 19945 }); 19946 19947 $def(self, '$**', function $Complex_$$$7(other) { 19948 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; 19949 19950 19951 if ($eqeq(other, 0)) { 19952 return $$$('Complex').$new(1, 0) 19953 } if ($eqeqeq($$$('Complex'), other)) { 19954 19955 $b = self.$polar(), $a = $to_ary($b), (r = ($a[0] == null ? nil : $a[0])), (theta = ($a[1] == null ? nil : $a[1])); 19956 ore = other.$real(); 19957 oim = other.$imag(); 19958 nr = $$$('Math').$exp($rb_minus($rb_times(ore, $$$('Math').$log(r)), $rb_times(oim, theta))); 19959 ntheta = $rb_plus($rb_times(theta, ore), $rb_times(oim, $$$('Math').$log(r))); 19960 return $$$('Complex').$polar(nr, ntheta); 19961 } else if ($eqeqeq($$$('Integer'), other)) { 19962 if ($truthy($rb_gt(other, 0))) { 19963 19964 x = self; 19965 z = x; 19966 n = $rb_minus(other, 1); 19967 while ($neqeq(n, 0)) { 19968 19969 $b = n.$divmod(2), $a = $to_ary($b), (div = ($a[0] == null ? nil : $a[0])), (mod = ($a[1] == null ? nil : $a[1])); 19970 while ($eqeq(mod, 0)) { 19971 19972 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())); 19973 n = div; 19974 $b = n.$divmod(2), $a = $to_ary($b), (div = ($a[0] == null ? nil : $a[0])), (mod = ($a[1] == null ? nil : $a[1])); 19975 } z = $rb_times(z, x); 19976 n = $rb_minus(n, 1); 19977 } return z; 19978 } else { 19979 return $rb_divide($$$('Rational').$new(1, 1), self)['$**'](other['$-@']()) 19980 } 19981 } else if (($eqeqeq($$$('Float'), other) || ($eqeqeq($$$('Rational'), other)))) { 19982 19983 $b = self.$polar(), $a = $to_ary($b), (r = ($a[0] == null ? nil : $a[0])), (theta = ($a[1] == null ? nil : $a[1])); 19984 return $$$('Complex').$polar(r['$**'](other), $rb_times(theta, other)); 19985 } else { 19986 return self.$__coerced__("**", other) 19987 } }); 19988 19989 $def(self, '$abs', function $$abs() { 19990 var self = this; 19991 19992 return $$$('Math').$hypot(self.real, self.imag) 19993 }); 19994 19995 $def(self, '$abs2', function $$abs2() { 19996 var self = this; 19997 19998 return $rb_plus($rb_times(self.real, self.real), $rb_times(self.imag, self.imag)) 19999 }); 20000 20001 $def(self, '$angle', function $$angle() { 20002 var self = this; 20003 20004 return $$$('Math').$atan2(self.imag, self.real) 20005 }); 20006 20007 $def(self, '$conj', function $$conj() { 20008 var self = this; 20009 20010 return $Kernel.$Complex(self.real, self.imag['$-@']()) 20011 }); 20012 20013 $def(self, '$denominator', function $$denominator() { 20014 var self = this; 20015 20016 return self.real.$denominator().$lcm(self.imag.$denominator()) 20017 }); 20018 20019 $def(self, '$eql?', function $Complex_eql$ques$8(other) { 20020 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 20021 20022 if ($truthy(($ret_or_1 = ($truthy(($ret_or_2 = $$('Complex')['$==='](other))) ? (self.real.$class()['$=='](self.imag.$class())) : ($ret_or_2))))) { 20023 return self['$=='](other) 20024 } else { 20025 return $ret_or_1 20026 } 20027 }); 20028 20029 $def(self, '$fdiv', function $$fdiv(other) { 20030 var self = this; 20031 20032 20033 if (!$eqeqeq($$$('Numeric'), other)) { 20034 $Kernel.$raise($$$('TypeError'), "" + (other.$class()) + " can't be coerced into Complex"); 20035 } return $rb_divide(self, other); 20036 }); 20037 20038 $def(self, '$finite?', function $Complex_finite$ques$9() { 20039 var self = this, $ret_or_1 = nil; 20040 20041 if ($truthy(($ret_or_1 = self.real['$finite?']()))) { 20042 return self.imag['$finite?']() 20043 } else { 20044 return $ret_or_1 20045 } 20046 }); 20047 20048 $def(self, '$hash', function $$hash() { 20049 var self = this; 20050 20051 return "Complex:" + (self.real) + ":" + (self.imag) 20052 }); 20053 20054 $def(self, '$infinite?', function $Complex_infinite$ques$10() { 20055 var self = this, $ret_or_1 = nil; 20056 20057 if ($truthy(($ret_or_1 = self.real['$infinite?']()))) { 20058 return $ret_or_1 20059 } else { 20060 return self.imag['$infinite?']() 20061 } 20062 }); 20063 20064 $def(self, '$inspect', function $$inspect() { 20065 var self = this; 20066 20067 return "(" + (self) + ")" 20068 }); 20069 20070 $def(self, '$numerator', function $$numerator() { 20071 var self = this, d = nil; 20072 20073 20074 d = self.$denominator(); 20075 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()))); 20076 }); 20077 20078 $def(self, '$polar', function $$polar() { 20079 var self = this; 20080 20081 return [self.$abs(), self.$arg()] 20082 }); 20083 20084 $def(self, '$rationalize', function $$rationalize(eps) { 20085 var self = this; 20086 20087 if (arguments.length > 1) { 20088 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 20089 } 20090 if ($neqeq(self.imag, 0)) { 20091 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Rational"); 20092 } return self.$real().$rationalize(eps); 20093 }, -1); 20094 20095 $def(self, '$real?', $return_val(false)); 20096 20097 $def(self, '$rect', function $$rect() { 20098 var self = this; 20099 20100 return [self.real, self.imag] 20101 }); 20102 20103 $def(self, '$to_f', function $$to_f() { 20104 var self = this; 20105 20106 20107 if (!$eqeq(self.imag, 0)) { 20108 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Float"); 20109 } return self.real.$to_f(); 20110 }); 20111 20112 $def(self, '$to_i', function $$to_i() { 20113 var self = this; 20114 20115 20116 if (!$eqeq(self.imag, 0)) { 20117 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Integer"); 20118 } return self.real.$to_i(); 20119 }); 20120 20121 $def(self, '$to_r', function $$to_r() { 20122 var self = this; 20123 20124 20125 if (!$eqeq(self.imag, 0)) { 20126 $Kernel.$raise($$$('RangeError'), "can't convert " + (self) + " into Rational"); 20127 } return self.real.$to_r(); 20128 }); 20129 20130 $def(self, '$to_s', function $$to_s() { 20131 var self = this, result = nil; 20132 20133 20134 result = self.real.$inspect(); 20135 result = $rb_plus(result, (((($eqeqeq($$$('Number'), self.imag) && ($truthy(self.imag['$nan?']()))) || ($truthy(self.imag['$positive?']()))) || ($truthy(self.imag['$zero?']()))) ? ("+") : ("-"))); 20136 result = $rb_plus(result, self.imag.$abs().$inspect()); 20137 if (($eqeqeq($$$('Number'), self.imag) && (($truthy(self.imag['$nan?']()) || ($truthy(self.imag['$infinite?']())))))) { 20138 result = $rb_plus(result, "*"); 20139 } return $rb_plus(result, "i"); 20140 }); 20141 $const_set($nesting[0], 'I', self.$new(0, 1)); 20142 $defs(self, '$from_string', function $$from_string(str) { 20143 20144 20145 var re = /[+-]?[\d_]+(\.[\d_]+)?(e\d+)?/; 20146 str.match(re); 20147 var real, imag; 20148 20149 function isFloat() { 20150 return re.test(str); 20151 } 20152 20153 function cutFloat() { 20154 var match = str.match(re); 20155 var number = match[0]; 20156 str = str.slice(number.length); 20157 return number.replace(/_/g, ''); 20158 } 20159 20160 // handles both floats and rationals 20161 function cutNumber() { 20162 if (isFloat()) { 20163 var numerator = parseFloat(cutFloat()); 20164 20165 if (str[0] === '/') { 20166 // rational real part 20167 str = str.slice(1); 20168 20169 if (isFloat()) { 20170 var denominator = parseFloat(cutFloat()); 20171 return $Kernel.$Rational(numerator, denominator); 20172 } else { 20173 // reverting '/' 20174 str = '/' + str; 20175 return numerator; 20176 } 20177 } else { 20178 // float real part, no denominator 20179 return numerator; 20180 } 20181 } else { 20182 return null; 20183 } 20184 } 20185 20186 real = cutNumber(); 20187 20188 if (!real) { 20189 if (str[0] === 'i') { 20190 // i => Complex(0, 1) 20191 return $Kernel.$Complex(0, 1); 20192 } 20193 if (str[0] === '-' && str[1] === 'i') { 20194 // -i => Complex(0, -1) 20195 return $Kernel.$Complex(0, -1); 20196 } 20197 if (str[0] === '+' && str[1] === 'i') { 20198 // +i => Complex(0, 1) 20199 return $Kernel.$Complex(0, 1); 20200 } 20201 // anything => Complex(0, 0) 20202 return $Kernel.$Complex(0, 0); 20203 } 20204 20205 imag = cutNumber(); 20206 if (!imag) { 20207 if (str[0] === 'i') { 20208 // 3i => Complex(0, 3) 20209 return $Kernel.$Complex(0, real); 20210 } else { 20211 // 3 => Complex(3, 0) 20212 return $Kernel.$Complex(real, 0); 20213 } 20214 } else { 20215 // 3+2i => Complex(3, 2) 20216 return $Kernel.$Complex(real, imag); 20217 } 20218 20219 }); 20220 (function(self, $parent_nesting) { 20221 20222 return $alias(self, "rectangular", "rect") 20223 })(Opal.get_singleton_class(self)); 20224 $alias(self, "arg", "angle"); 20225 $alias(self, "conjugate", "conj"); 20226 $alias(self, "divide", "/"); 20227 $alias(self, "imaginary", "imag"); 20228 $alias(self, "magnitude", "abs"); 20229 $alias(self, "phase", "arg"); 20230 $alias(self, "quo", "/"); 20231 $alias(self, "rectangular", "rect"); 20232 20233 Opal.udef(self, '$' + "negative?"); 20234 Opal.udef(self, '$' + "positive?"); 20235 20236 Opal.udef(self, '$' + "step"); return nil; })('::', $$$('Numeric'), $nesting); 20237}; 20238 20239Opal.modules["corelib/rational/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 20240 var $module = Opal.module, $def = Opal.def, $klass = Opal.klass; Opal.nil; var $$$ = Opal.$$$; 20241 20242 Opal.add_stubs('convert,from_string'); 20243 20244 (function($base) { 20245 var self = $module($base, 'Kernel'); 20246 20247 20248 return $def(self, '$Rational', function $$Rational(numerator, denominator) { 20249 20250 20251 if (denominator == null) denominator = 1; 20252 return $$$('Rational').$convert(numerator, denominator); 20253 }, -2) 20254 })('::'); 20255 return (function($base, $super) { 20256 var self = $klass($base, $super, 'String'); 20257 20258 20259 return $def(self, '$to_r', function $$to_r() { 20260 var self = this; 20261 20262 return $$$('Rational').$from_string(self) 20263 }) 20264 })('::', null); 20265}; 20266 20267Opal.modules["corelib/rational"] = function(Opal) {/* Generated by Opal 1.7.3 */ 20268 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.$$$; 20269 20270 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'); 20271 20272 self.$require("corelib/numeric"); 20273 self.$require("corelib/rational/base"); 20274 return (function($base, $super) { 20275 var self = $klass($base, $super, 'Rational'); 20276 20277 var $proto = self.$$prototype; 20278 20279 $proto.num = $proto.den = nil; 20280 20281 $defs(self, '$reduce', function $$reduce(num, den) { 20282 var self = this, gcd = nil; 20283 20284 20285 num = num.$to_i(); 20286 den = den.$to_i(); 20287 if ($eqeq(den, 0)) { 20288 $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0"); 20289 } else if ($truthy($rb_lt(den, 0))) { 20290 20291 num = num['$-@'](); 20292 den = den['$-@'](); 20293 } else if ($eqeq(den, 1)) { 20294 return self.$new(num, den) 20295 } gcd = num.$gcd(den); 20296 return self.$new($rb_divide(num, gcd), $rb_divide(den, gcd)); 20297 }); 20298 $defs(self, '$convert', function $$convert(num, den) { 20299 var self = this; 20300 20301 20302 if (($truthy(num['$nil?']()) || ($truthy(den['$nil?']())))) { 20303 $Kernel.$raise($$$('TypeError'), "cannot convert nil into Rational"); 20304 } if (($eqeqeq($$$('Integer'), num) && ($eqeqeq($$$('Integer'), den)))) { 20305 return self.$reduce(num, den) 20306 } if ((($eqeqeq($$$('Float'), num) || ($eqeqeq($$$('String'), num))) || ($eqeqeq($$$('Complex'), num)))) { 20307 num = num.$to_r(); 20308 } if ((($eqeqeq($$$('Float'), den) || ($eqeqeq($$$('String'), den))) || ($eqeqeq($$$('Complex'), den)))) { 20309 den = den.$to_r(); 20310 } if (($truthy(den['$equal?'](1)) && ($not($$$('Integer')['$==='](num))))) { 20311 return $Opal['$coerce_to!'](num, $$$('Rational'), "to_r") 20312 } else if (($eqeqeq($$$('Numeric'), num) && ($eqeqeq($$$('Numeric'), den)))) { 20313 return $rb_divide(num, den) 20314 } else { 20315 return self.$reduce(num, den) 20316 } }); 20317 20318 $def(self, '$initialize', function $$initialize(num, den) { 20319 var self = this; 20320 20321 20322 self.num = num; 20323 self.den = den; 20324 return self.$freeze(); 20325 }); 20326 20327 $def(self, '$numerator', $return_ivar("num")); 20328 20329 $def(self, '$denominator', $return_ivar("den")); 20330 20331 $def(self, '$coerce', function $$coerce(other) { 20332 var self = this, $ret_or_1 = nil; 20333 20334 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20335 return [other, self] 20336 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20337 return [other.$to_r(), self] 20338 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20339 return [other, self.$to_f()] 20340 } else { 20341 return nil 20342 } 20343 }); 20344 20345 $def(self, '$==', function $Rational_$eq_eq$1(other) { 20346 var self = this, $ret_or_1 = nil, $ret_or_2 = nil; 20347 20348 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20349 if ($truthy(($ret_or_2 = self.num['$=='](other.$numerator())))) { 20350 return self.den['$=='](other.$denominator()) 20351 } else { 20352 return $ret_or_2 20353 } 20354 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20355 if ($truthy(($ret_or_2 = self.num['$=='](other)))) { 20356 return self.den['$=='](1) 20357 } else { 20358 return $ret_or_2 20359 } 20360 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20361 return self.$to_f()['$=='](other) 20362 } else { 20363 return other['$=='](self) 20364 } 20365 }); 20366 20367 $def(self, '$<=>', function $Rational_$lt_eq_gt$2(other) { 20368 var self = this, $ret_or_1 = nil; 20369 20370 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20371 return $rb_minus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator()))['$<=>'](0) 20372 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20373 return $rb_minus(self.num, $rb_times(self.den, other))['$<=>'](0) 20374 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20375 return self.$to_f()['$<=>'](other) 20376 } else { 20377 return self.$__coerced__("<=>", other) 20378 } 20379 }); 20380 20381 $def(self, '$+', function $Rational_$plus$3(other) { 20382 var self = this, $ret_or_1 = nil, num = nil, den = nil; 20383 20384 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20385 20386 num = $rb_plus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator())); 20387 den = $rb_times(self.den, other.$denominator()); 20388 return $Kernel.$Rational(num, den); 20389 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20390 return $Kernel.$Rational($rb_plus(self.num, $rb_times(other, self.den)), self.den) 20391 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20392 return $rb_plus(self.$to_f(), other) 20393 } else { 20394 return self.$__coerced__("+", other) 20395 } 20396 }); 20397 20398 $def(self, '$-', function $Rational_$minus$4(other) { 20399 var self = this, $ret_or_1 = nil, num = nil, den = nil; 20400 20401 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20402 20403 num = $rb_minus($rb_times(self.num, other.$denominator()), $rb_times(self.den, other.$numerator())); 20404 den = $rb_times(self.den, other.$denominator()); 20405 return $Kernel.$Rational(num, den); 20406 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20407 return $Kernel.$Rational($rb_minus(self.num, $rb_times(other, self.den)), self.den) 20408 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20409 return $rb_minus(self.$to_f(), other) 20410 } else { 20411 return self.$__coerced__("-", other) 20412 } 20413 }); 20414 20415 $def(self, '$*', function $Rational_$$5(other) { 20416 var self = this, $ret_or_1 = nil, num = nil, den = nil; 20417 20418 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20419 20420 num = $rb_times(self.num, other.$numerator()); 20421 den = $rb_times(self.den, other.$denominator()); 20422 return $Kernel.$Rational(num, den); 20423 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20424 return $Kernel.$Rational($rb_times(self.num, other), self.den) 20425 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20426 return $rb_times(self.$to_f(), other) 20427 } else { 20428 return self.$__coerced__("*", other) 20429 } 20430 }); 20431 20432 $def(self, '$/', function $Rational_$slash$6(other) { 20433 var self = this, $ret_or_1 = nil, num = nil, den = nil; 20434 20435 if ($eqeqeq($$$('Rational'), ($ret_or_1 = other))) { 20436 20437 num = $rb_times(self.num, other.$denominator()); 20438 den = $rb_times(self.den, other.$numerator()); 20439 return $Kernel.$Rational(num, den); 20440 } else if ($eqeqeq($$$('Integer'), $ret_or_1)) { 20441 if ($eqeq(other, 0)) { 20442 return $rb_divide(self.$to_f(), 0.0) 20443 } else { 20444 return $Kernel.$Rational(self.num, $rb_times(self.den, other)) 20445 } 20446 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20447 return $rb_divide(self.$to_f(), other) 20448 } else { 20449 return self.$__coerced__("/", other) 20450 } 20451 }); 20452 20453 $def(self, '$**', function $Rational_$$$7(other) { 20454 var self = this, $ret_or_1 = nil; 20455 20456 if ($eqeqeq($$$('Integer'), ($ret_or_1 = other))) { 20457 if (($eqeq(self, 0) && ($truthy($rb_lt(other, 0))))) { 20458 return $$$($$$('Float'), 'INFINITY') 20459 } else if ($truthy($rb_gt(other, 0))) { 20460 return $Kernel.$Rational(self.num['$**'](other), self.den['$**'](other)) 20461 } else if ($truthy($rb_lt(other, 0))) { 20462 return $Kernel.$Rational(self.den['$**'](other['$-@']()), self.num['$**'](other['$-@']())) 20463 } else { 20464 return $Kernel.$Rational(1, 1) 20465 } 20466 } else if ($eqeqeq($$$('Float'), $ret_or_1)) { 20467 return self.$to_f()['$**'](other) 20468 } else if ($eqeqeq($$$('Rational'), $ret_or_1)) { 20469 if ($eqeq(other, 0)) { 20470 return $Kernel.$Rational(1, 1) 20471 } else if ($eqeq(other.$denominator(), 1)) { 20472 if ($truthy($rb_lt(other, 0))) { 20473 return $Kernel.$Rational(self.den['$**'](other.$numerator().$abs()), self.num['$**'](other.$numerator().$abs())) 20474 } else { 20475 return $Kernel.$Rational(self.num['$**'](other.$numerator()), self.den['$**'](other.$numerator())) 20476 } 20477 } else if (($eqeq(self, 0) && ($truthy($rb_lt(other, 0))))) { 20478 return $Kernel.$raise($$$('ZeroDivisionError'), "divided by 0") 20479 } else { 20480 return self.$to_f()['$**'](other) 20481 } 20482 } else { 20483 return self.$__coerced__("**", other) 20484 } 20485 }); 20486 20487 $def(self, '$abs', function $$abs() { 20488 var self = this; 20489 20490 return $Kernel.$Rational(self.num.$abs(), self.den.$abs()) 20491 }); 20492 20493 $def(self, '$ceil', function $$ceil(precision) { 20494 var self = this; 20495 20496 20497 if (precision == null) precision = 0; 20498 if ($eqeq(precision, 0)) { 20499 return $rb_divide(self.num['$-@'](), self.den)['$-@']().$ceil() 20500 } else { 20501 return self.$with_precision("ceil", precision) 20502 } }, -1); 20503 20504 $def(self, '$floor', function $$floor(precision) { 20505 var self = this; 20506 20507 20508 if (precision == null) precision = 0; 20509 if ($eqeq(precision, 0)) { 20510 return $rb_divide(self.num['$-@'](), self.den)['$-@']().$floor() 20511 } else { 20512 return self.$with_precision("floor", precision) 20513 } }, -1); 20514 20515 $def(self, '$hash', function $$hash() { 20516 var self = this; 20517 20518 return "Rational:" + (self.num) + ":" + (self.den) 20519 }); 20520 20521 $def(self, '$inspect', function $$inspect() { 20522 var self = this; 20523 20524 return "(" + (self) + ")" 20525 }); 20526 20527 $def(self, '$rationalize', function $$rationalize(eps) { 20528 var self = this; 20529 20530 if (arguments.length > 1) { 20531 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (" + (arguments.length) + " for 0..1)"); 20532 } 20533 20534 if (eps == null) { 20535 return self; 20536 } 20537 20538 var e = eps.$abs(), 20539 a = $rb_minus(self, e), 20540 b = $rb_plus(self, e); 20541 20542 var p0 = 0, 20543 p1 = 1, 20544 q0 = 1, 20545 q1 = 0, 20546 p2, q2; 20547 20548 var c, k, t; 20549 20550 while (true) { 20551 c = (a).$ceil(); 20552 20553 if ($rb_le(c, b)) { 20554 break; 20555 } 20556 20557 k = c - 1; 20558 p2 = k * p1 + p0; 20559 q2 = k * q1 + q0; 20560 t = $rb_divide(1, $rb_minus(b, k)); 20561 b = $rb_divide(1, $rb_minus(a, k)); 20562 a = t; 20563 20564 p0 = p1; 20565 q0 = q1; 20566 p1 = p2; 20567 q1 = q2; 20568 } 20569 20570 return $Kernel.$Rational(c * p1 + p0, c * q1 + q0); 20571 }, -1); 20572 20573 $def(self, '$round', function $$round(precision) { 20574 var self = this, num = nil, den = nil, approx = nil; 20575 20576 20577 if (precision == null) precision = 0; 20578 if (!$eqeq(precision, 0)) { 20579 return self.$with_precision("round", precision) 20580 } if ($eqeq(self.num, 0)) { 20581 return 0 20582 } if ($eqeq(self.den, 1)) { 20583 return self.num 20584 } num = $rb_plus($rb_times(self.num.$abs(), 2), self.den); 20585 den = $rb_times(self.den, 2); 20586 approx = $rb_divide(num, den).$truncate(); 20587 if ($truthy($rb_lt(self.num, 0))) { 20588 return approx['$-@']() 20589 } else { 20590 return approx 20591 } }, -1); 20592 20593 $def(self, '$to_f', function $$to_f() { 20594 var self = this; 20595 20596 return $rb_divide(self.num, self.den) 20597 }); 20598 20599 $def(self, '$to_i', function $$to_i() { 20600 var self = this; 20601 20602 return self.$truncate() 20603 }); 20604 20605 $def(self, '$to_r', $return_self); 20606 20607 $def(self, '$to_s', function $$to_s() { 20608 var self = this; 20609 20610 return "" + (self.num) + "/" + (self.den) 20611 }); 20612 20613 $def(self, '$truncate', function $$truncate(precision) { 20614 var self = this; 20615 20616 20617 if (precision == null) precision = 0; 20618 if ($eqeq(precision, 0)) { 20619 if ($truthy($rb_lt(self.num, 0))) { 20620 return self.$ceil() 20621 } else { 20622 return self.$floor() 20623 } 20624 } else { 20625 return self.$with_precision("truncate", precision) 20626 } }, -1); 20627 20628 $def(self, '$with_precision', function $$with_precision(method, precision) { 20629 var self = this, p = nil, s = nil; 20630 20631 20632 if (!$eqeqeq($$$('Integer'), precision)) { 20633 $Kernel.$raise($$$('TypeError'), "not an Integer"); 20634 } p = (10)['$**'](precision); 20635 s = $rb_times(self, p); 20636 if ($truthy($rb_lt(precision, 1))) { 20637 return $rb_divide(s.$send(method), p).$to_i() 20638 } else { 20639 return $Kernel.$Rational(s.$send(method), p) 20640 } }); 20641 $defs(self, '$from_string', function $$from_string(string) { 20642 20643 20644 var str = string.trimLeft(), 20645 re = /^[+-]?[\d_]+(\.[\d_]+)?/; 20646 str.match(re); 20647 var numerator, denominator; 20648 20649 function isFloat() { 20650 return re.test(str); 20651 } 20652 20653 function cutFloat() { 20654 var match = str.match(re); 20655 var number = match[0]; 20656 str = str.slice(number.length); 20657 return number.replace(/_/g, ''); 20658 } 20659 20660 if (isFloat()) { 20661 numerator = parseFloat(cutFloat()); 20662 20663 if (str[0] === '/') { 20664 // rational real part 20665 str = str.slice(1); 20666 20667 if (isFloat()) { 20668 denominator = parseFloat(cutFloat()); 20669 return $Kernel.$Rational(numerator, denominator); 20670 } else { 20671 return $Kernel.$Rational(numerator, 1); 20672 } 20673 } else { 20674 return $Kernel.$Rational(numerator, 1); 20675 } 20676 } else { 20677 return $Kernel.$Rational(0, 1); 20678 } 20679 20680 }); 20681 $alias(self, "divide", "/"); 20682 return $alias(self, "quo", "/"); 20683 })('::', $$$('Numeric')); 20684}; 20685 20686Opal.modules["corelib/time"] = function(Opal) {/* Generated by Opal 1.7.3 */ 20687 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.$$$; 20688 20689 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?'); 20690 20691 self.$require("corelib/comparable"); 20692 return (function($base, $super, $parent_nesting) { 20693 var self = $klass($base, $super, 'Time'); 20694 20695 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 20696 20697 20698 self.$include($$$('Comparable')); 20699 20700 var days_of_week = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"], 20701 short_days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"], 20702 short_months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"], 20703 long_months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; 20704 $defs(self, '$at', function $$at(seconds, frac) { 20705 20706 var result; 20707 20708 if ($$$('Time')['$==='](seconds)) { 20709 if (frac !== undefined) { 20710 $Kernel.$raise($$$('TypeError'), "can't convert Time into an exact number"); 20711 } 20712 result = new Date(seconds.getTime()); 20713 result.timezone = seconds.timezone; 20714 return result; 20715 } 20716 20717 if (!seconds.$$is_number) { 20718 seconds = $Opal['$coerce_to!'](seconds, $$$('Integer'), "to_int"); 20719 } 20720 20721 if (frac === undefined) { 20722 return new Date(seconds * 1000); 20723 } 20724 20725 if (!frac.$$is_number) { 20726 frac = $Opal['$coerce_to!'](frac, $$$('Integer'), "to_int"); 20727 } 20728 20729 return new Date(seconds * 1000 + (frac / 1000)); 20730 }, -2); 20731 20732 function time_params(year, month, day, hour, min, sec) { 20733 if (year.$$is_string) { 20734 year = parseInt(year, 10); 20735 } else { 20736 year = $Opal['$coerce_to!'](year, $$$('Integer'), "to_int"); 20737 } 20738 20739 if (month === nil) { 20740 month = 1; 20741 } else if (!month.$$is_number) { 20742 if ((month)['$respond_to?']("to_str")) { 20743 month = (month).$to_str(); 20744 switch (month.toLowerCase()) { 20745 case 'jan': month = 1; break; 20746 case 'feb': month = 2; break; 20747 case 'mar': month = 3; break; 20748 case 'apr': month = 4; break; 20749 case 'may': month = 5; break; 20750 case 'jun': month = 6; break; 20751 case 'jul': month = 7; break; 20752 case 'aug': month = 8; break; 20753 case 'sep': month = 9; break; 20754 case 'oct': month = 10; break; 20755 case 'nov': month = 11; break; 20756 case 'dec': month = 12; break; 20757 default: month = (month).$to_i(); 20758 } 20759 } else { 20760 month = $Opal['$coerce_to!'](month, $$$('Integer'), "to_int"); 20761 } 20762 } 20763 20764 if (month < 1 || month > 12) { 20765 $Kernel.$raise($$$('ArgumentError'), "month out of range: " + (month)); 20766 } 20767 month = month - 1; 20768 20769 if (day === nil) { 20770 day = 1; 20771 } else if (day.$$is_string) { 20772 day = parseInt(day, 10); 20773 } else { 20774 day = $Opal['$coerce_to!'](day, $$$('Integer'), "to_int"); 20775 } 20776 20777 if (day < 1 || day > 31) { 20778 $Kernel.$raise($$$('ArgumentError'), "day out of range: " + (day)); 20779 } 20780 20781 if (hour === nil) { 20782 hour = 0; 20783 } else if (hour.$$is_string) { 20784 hour = parseInt(hour, 10); 20785 } else { 20786 hour = $Opal['$coerce_to!'](hour, $$$('Integer'), "to_int"); 20787 } 20788 20789 if (hour < 0 || hour > 24) { 20790 $Kernel.$raise($$$('ArgumentError'), "hour out of range: " + (hour)); 20791 } 20792 20793 if (min === nil) { 20794 min = 0; 20795 } else if (min.$$is_string) { 20796 min = parseInt(min, 10); 20797 } else { 20798 min = $Opal['$coerce_to!'](min, $$$('Integer'), "to_int"); 20799 } 20800 20801 if (min < 0 || min > 59) { 20802 $Kernel.$raise($$$('ArgumentError'), "min out of range: " + (min)); 20803 } 20804 20805 if (sec === nil) { 20806 sec = 0; 20807 } else if (!sec.$$is_number) { 20808 if (sec.$$is_string) { 20809 sec = parseInt(sec, 10); 20810 } else { 20811 sec = $Opal['$coerce_to!'](sec, $$$('Integer'), "to_int"); 20812 } 20813 } 20814 20815 if (sec < 0 || sec > 60) { 20816 $Kernel.$raise($$$('ArgumentError'), "sec out of range: " + (sec)); 20817 } 20818 20819 return [year, month, day, hour, min, sec]; 20820 } 20821 $defs(self, '$new', function $Time_new$1(year, month, day, hour, min, sec, utc_offset) { 20822 var self = this; 20823 if (month == null) month = nil; 20824 if (day == null) day = nil; 20825 if (hour == null) hour = nil; 20826 if (min == null) min = nil; 20827 if (sec == null) sec = nil; 20828 if (utc_offset == null) utc_offset = nil; 20829 20830 var args, result, timezone, utc_date; 20831 20832 if (year === undefined) { 20833 return new Date(); 20834 } 20835 20836 args = time_params(year, month, day, hour, min, sec); 20837 year = args[0]; 20838 month = args[1]; 20839 day = args[2]; 20840 hour = args[3]; 20841 min = args[4]; 20842 sec = args[5]; 20843 20844 if (utc_offset === nil) { 20845 result = new Date(year, month, day, hour, min, 0, sec * 1000); 20846 if (year < 100) { 20847 result.setFullYear(year); 20848 } 20849 return result; 20850 } 20851 20852 timezone = self.$_parse_offset(utc_offset); 20853 utc_date = new Date(Date.UTC(year, month, day, hour, min, 0, sec * 1000)); 20854 if (year < 100) { 20855 utc_date.setUTCFullYear(year); 20856 } 20857 20858 result = new Date(utc_date.getTime() - timezone * 3600000); 20859 result.timezone = timezone; 20860 20861 return result; 20862 }, -1); 20863 $defs(self, '$_parse_offset', function $$_parse_offset(utc_offset) { 20864 20865 20866 var timezone; 20867 if (utc_offset.$$is_string) { 20868 if (utc_offset == 'UTC') { 20869 timezone = 0; 20870 } 20871 else if(/^[+-]\d\d:[0-5]\d$/.test(utc_offset)) { 20872 var sign, hours, minutes; 20873 sign = utc_offset[0]; 20874 hours = +(utc_offset[1] + utc_offset[2]); 20875 minutes = +(utc_offset[4] + utc_offset[5]); 20876 20877 timezone = (sign == '-' ? -1 : 1) * (hours + minutes / 60); 20878 } 20879 else { 20880 // Unsupported: "A".."I","K".."Z" 20881 $Kernel.$raise($$$('ArgumentError'), "\"+HH:MM\", \"-HH:MM\", \"UTC\" expected for utc_offset: " + (utc_offset)); 20882 } 20883 } 20884 else if (utc_offset.$$is_number) { 20885 timezone = utc_offset / 3600; 20886 } 20887 else { 20888 $Kernel.$raise($$$('ArgumentError'), "Opal doesn't support other types for a timezone argument than Integer and String"); 20889 } 20890 return timezone; 20891 20892 }); 20893 $defs(self, '$local', function $$local(year, month, day, hour, min, sec, millisecond, _dummy1, _dummy2, _dummy3) { 20894 20895 20896 if (month == null) month = nil; 20897 if (day == null) day = nil; 20898 if (hour == null) hour = nil; 20899 if (min == null) min = nil; 20900 if (sec == null) sec = nil; 20901 if (millisecond == null) millisecond = nil; 20902 if (_dummy1 == null) _dummy1 = nil; 20903 if (_dummy2 == null) _dummy2 = nil; 20904 if (_dummy3 == null) _dummy3 = nil; 20905 20906 var args, result; 20907 20908 if (arguments.length === 10) { 20909 args = $slice(arguments); 20910 year = args[5]; 20911 month = args[4]; 20912 day = args[3]; 20913 hour = args[2]; 20914 min = args[1]; 20915 sec = args[0]; 20916 } 20917 20918 args = time_params(year, month, day, hour, min, sec); 20919 year = args[0]; 20920 month = args[1]; 20921 day = args[2]; 20922 hour = args[3]; 20923 min = args[4]; 20924 sec = args[5]; 20925 20926 result = new Date(year, month, day, hour, min, 0, sec * 1000); 20927 if (year < 100) { 20928 result.setFullYear(year); 20929 } 20930 return result; 20931 }, -2); 20932 $defs(self, '$gm', function $$gm(year, month, day, hour, min, sec, millisecond, _dummy1, _dummy2, _dummy3) { 20933 20934 20935 if (month == null) month = nil; 20936 if (day == null) day = nil; 20937 if (hour == null) hour = nil; 20938 if (min == null) min = nil; 20939 if (sec == null) sec = nil; 20940 if (millisecond == null) millisecond = nil; 20941 if (_dummy1 == null) _dummy1 = nil; 20942 if (_dummy2 == null) _dummy2 = nil; 20943 if (_dummy3 == null) _dummy3 = nil; 20944 20945 var args, result; 20946 20947 if (arguments.length === 10) { 20948 args = $slice(arguments); 20949 year = args[5]; 20950 month = args[4]; 20951 day = args[3]; 20952 hour = args[2]; 20953 min = args[1]; 20954 sec = args[0]; 20955 } 20956 20957 args = time_params(year, month, day, hour, min, sec); 20958 year = args[0]; 20959 month = args[1]; 20960 day = args[2]; 20961 hour = args[3]; 20962 min = args[4]; 20963 sec = args[5]; 20964 20965 result = new Date(Date.UTC(year, month, day, hour, min, 0, sec * 1000)); 20966 if (year < 100) { 20967 result.setUTCFullYear(year); 20968 } 20969 result.timezone = 0; 20970 return result; 20971 }, -2); 20972 $defs(self, '$now', function $$now() { 20973 var self = this; 20974 20975 return self.$new() 20976 }); 20977 20978 $def(self, '$+', function $Time_$plus$2(other) { 20979 var self = this; 20980 20981 20982 if ($eqeqeq($$$('Time'), other)) { 20983 $Kernel.$raise($$$('TypeError'), "time + time?"); 20984 } 20985 if (!other.$$is_number) { 20986 other = $Opal['$coerce_to!'](other, $$$('Integer'), "to_int"); 20987 } 20988 var result = new Date(self.getTime() + (other * 1000)); 20989 result.timezone = self.timezone; 20990 return result; 20991 }); 20992 20993 $def(self, '$-', function $Time_$minus$3(other) { 20994 var self = this; 20995 20996 20997 if ($eqeqeq($$$('Time'), other)) { 20998 return (self.getTime() - other.getTime()) / 1000 20999 } 21000 if (!other.$$is_number) { 21001 other = $Opal['$coerce_to!'](other, $$$('Integer'), "to_int"); 21002 } 21003 var result = new Date(self.getTime() - (other * 1000)); 21004 result.timezone = self.timezone; 21005 return result; 21006 }); 21007 21008 $def(self, '$<=>', function $Time_$lt_eq_gt$4(other) { 21009 var self = this, r = nil; 21010 21011 if ($eqeqeq($$$('Time'), other)) { 21012 return self.$to_f()['$<=>'](other.$to_f()) 21013 } else { 21014 21015 r = other['$<=>'](self); 21016 if ($truthy(r['$nil?']())) { 21017 return nil 21018 } else if ($truthy($rb_gt(r, 0))) { 21019 return -1 21020 } else if ($truthy($rb_lt(r, 0))) { 21021 return 1 21022 } else { 21023 return 0 21024 } } 21025 }); 21026 21027 $def(self, '$==', function $Time_$eq_eq$5(other) { 21028 var self = this, $ret_or_1 = nil; 21029 21030 if ($truthy(($ret_or_1 = $$$('Time')['$==='](other)))) { 21031 return self.$to_f() === other.$to_f() 21032 } else { 21033 return $ret_or_1 21034 } 21035 }); 21036 21037 $def(self, '$asctime', function $$asctime() { 21038 var self = this; 21039 21040 return self.$strftime("%a %b %e %H:%M:%S %Y") 21041 }); 21042 $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; 21043 21044 21045 if (method == null) method = nil; 21046 if (getter == null) getter = nil; 21047 if (utcgetter == null) utcgetter = nil; 21048 if (difference == null) difference = 0; 21049 return $send(self, 'define_method', [method], function $$7(){var self = $$7.$$s == null ? this : $$7.$$s; 21050 21051 21052 return difference + ((self.timezone != null) ? 21053 (new Date(self.getTime() + self.timezone * 3600000))[utcgetter]() : 21054 self[getter]()) 21055 }, {$$s: self});}, {$$arity: -4, $$s: self}); 21056 21057 $def(self, '$yday', function $$yday() { 21058 var self = this, start_of_year = nil, start_of_day = nil, one_day = nil; 21059 21060 21061 start_of_year = $$('Time').$new(self.$year()).$to_i(); 21062 start_of_day = $$('Time').$new(self.$year(), self.$month(), self.$day()).$to_i(); 21063 one_day = 86400; 21064 return $rb_plus($rb_divide($rb_minus(start_of_day, start_of_year), one_day).$round(), 1); 21065 }); 21066 21067 $def(self, '$isdst', function $$isdst() { 21068 var self = this; 21069 21070 21071 var jan = new Date(self.getFullYear(), 0, 1), 21072 jul = new Date(self.getFullYear(), 6, 1); 21073 return self.getTimezoneOffset() < Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()); 21074 21075 }); 21076 21077 $def(self, '$dup', function $$dup() { 21078 var self = this, copy = nil; 21079 21080 21081 copy = new Date(self.getTime()); 21082 copy.$copy_instance_variables(self); 21083 copy.$initialize_dup(self); 21084 return copy; 21085 }); 21086 21087 $def(self, '$eql?', function $Time_eql$ques$8(other) { 21088 var self = this, $ret_or_1 = nil; 21089 21090 if ($truthy(($ret_or_1 = other['$is_a?']($$$('Time'))))) { 21091 return self['$<=>'](other)['$zero?']() 21092 } else { 21093 return $ret_or_1 21094 } 21095 }); 21096 $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; 21097 21098 21099 if (method == null) method = nil; 21100 if (weekday == null) weekday = nil; 21101 return $send(self, 'define_method', [method], function $$10(){var self = $$10.$$s == null ? this : $$10.$$s; 21102 21103 return self.$wday() === weekday}, {$$s: self});}, {$$s: self}); 21104 21105 $def(self, '$hash', function $$hash() { 21106 var self = this; 21107 21108 return 'Time:' + self.getTime(); 21109 }); 21110 21111 $def(self, '$inspect', function $$inspect() { 21112 var self = this; 21113 21114 if ($truthy(self['$utc?']())) { 21115 return self.$strftime("%Y-%m-%d %H:%M:%S UTC") 21116 } else { 21117 return self.$strftime("%Y-%m-%d %H:%M:%S %z") 21118 } 21119 }); 21120 21121 $def(self, '$succ', function $$succ() { 21122 var self = this; 21123 21124 21125 var result = new Date(self.getTime() + 1000); 21126 result.timezone = self.timezone; 21127 return result; 21128 21129 }); 21130 21131 $def(self, '$usec', function $$usec() { 21132 var self = this; 21133 21134 return self.getMilliseconds() * 1000; 21135 }); 21136 21137 $def(self, '$zone', function $$zone() { 21138 var self = this; 21139 21140 21141 if (self.timezone === 0) return "UTC"; 21142 else if (self.timezone != null) return nil; 21143 21144 var string = self.toString(), 21145 result; 21146 21147 if (string.indexOf('(') == -1) { 21148 result = string.match(/[A-Z]{3,4}/)[0]; 21149 } 21150 else { 21151 result = string.match(/\((.+)\)(?:\s|$)/)[1]; 21152 } 21153 21154 if (result == "GMT" && /(GMT\W*\d{4})/.test(string)) { 21155 return RegExp.$1; 21156 } 21157 else { 21158 return result; 21159 } 21160 21161 }); 21162 21163 $def(self, '$getgm', function $$getgm() { 21164 var self = this; 21165 21166 21167 var result = new Date(self.getTime()); 21168 result.timezone = 0; 21169 return result; 21170 21171 }); 21172 21173 $def(self, '$gmtime', function $$gmtime() { 21174 var self = this; 21175 21176 21177 if (self.timezone !== 0) { 21178 $deny_frozen_access(self); 21179 self.timezone = 0; 21180 } 21181 return self; 21182 21183 }); 21184 21185 $def(self, '$gmt?', function $Time_gmt$ques$11() { 21186 var self = this; 21187 21188 return self.timezone === 0; 21189 }); 21190 21191 $def(self, '$gmt_offset', function $$gmt_offset() { 21192 var self = this; 21193 21194 return (self.timezone != null) ? self.timezone * 60 : -self.getTimezoneOffset() * 60; 21195 }); 21196 21197 $def(self, '$strftime', function $$strftime(format) { 21198 var self = this; 21199 21200 21201 return format.replace(/%([\-_#^0]*:{0,2})(\d+)?([EO]*)(.)/g, function(full, flags, width, _, conv) { 21202 var result = "", jd, c, s, 21203 zero = flags.indexOf('0') !== -1, 21204 pad = flags.indexOf('-') === -1, 21205 blank = flags.indexOf('_') !== -1, 21206 upcase = flags.indexOf('^') !== -1, 21207 invert = flags.indexOf('#') !== -1, 21208 colons = (flags.match(':') || []).length; 21209 21210 width = parseInt(width, 10); 21211 21212 if (zero && blank) { 21213 if (flags.indexOf('0') < flags.indexOf('_')) { 21214 zero = false; 21215 } 21216 else { 21217 blank = false; 21218 } 21219 } 21220 21221 switch (conv) { 21222 case 'Y': 21223 result += self.$year(); 21224 break; 21225 21226 case 'C': 21227 zero = !blank; 21228 result += Math.round(self.$year() / 100); 21229 break; 21230 21231 case 'y': 21232 zero = !blank; 21233 result += (self.$year() % 100); 21234 break; 21235 21236 case 'm': 21237 zero = !blank; 21238 result += self.$mon(); 21239 break; 21240 21241 case 'B': 21242 result += long_months[self.$mon() - 1]; 21243 break; 21244 21245 case 'b': 21246 case 'h': 21247 blank = !zero; 21248 result += short_months[self.$mon() - 1]; 21249 break; 21250 21251 case 'd': 21252 zero = !blank; 21253 result += self.$day(); 21254 break; 21255 21256 case 'e': 21257 blank = !zero; 21258 result += self.$day(); 21259 break; 21260 21261 case 'j': 21262 zero = !blank; 21263 width = isNaN(width) ? 3 : width; 21264 result += self.$yday(); 21265 break; 21266 21267 case 'H': 21268 zero = !blank; 21269 result += self.$hour(); 21270 break; 21271 21272 case 'k': 21273 blank = !zero; 21274 result += self.$hour(); 21275 break; 21276 21277 case 'I': 21278 zero = !blank; 21279 result += (self.$hour() % 12 || 12); 21280 break; 21281 21282 case 'l': 21283 blank = !zero; 21284 result += (self.$hour() % 12 || 12); 21285 break; 21286 21287 case 'P': 21288 result += (self.$hour() >= 12 ? "pm" : "am"); 21289 break; 21290 21291 case 'p': 21292 result += (self.$hour() >= 12 ? "PM" : "AM"); 21293 break; 21294 21295 case 'M': 21296 zero = !blank; 21297 result += self.$min(); 21298 break; 21299 21300 case 'S': 21301 zero = !blank; 21302 result += self.$sec(); 21303 break; 21304 21305 case 'L': 21306 zero = !blank; 21307 width = isNaN(width) ? 3 : width; 21308 result += self.getMilliseconds(); 21309 break; 21310 21311 case 'N': 21312 width = isNaN(width) ? 9 : width; 21313 result += (self.getMilliseconds().toString()).$rjust(3, "0"); 21314 result = (result).$ljust(width, "0"); 21315 break; 21316 21317 case 'z': 21318 var offset = (self.timezone == null) ? self.getTimezoneOffset() : (-self.timezone * 60), 21319 hours = Math.floor(Math.abs(offset) / 60), 21320 minutes = Math.abs(offset) % 60; 21321 21322 result += offset < 0 ? "+" : "-"; 21323 result += hours < 10 ? "0" : ""; 21324 result += hours; 21325 21326 if (colons > 0) { 21327 result += ":"; 21328 } 21329 21330 result += minutes < 10 ? "0" : ""; 21331 result += minutes; 21332 21333 if (colons > 1) { 21334 result += ":00"; 21335 } 21336 21337 break; 21338 21339 case 'Z': 21340 result += self.$zone(); 21341 break; 21342 21343 case 'A': 21344 result += days_of_week[self.$wday()]; 21345 break; 21346 21347 case 'a': 21348 result += short_days[self.$wday()]; 21349 break; 21350 21351 case 'u': 21352 result += (self.$wday() + 1); 21353 break; 21354 21355 case 'w': 21356 result += self.$wday(); 21357 break; 21358 21359 case 'V': 21360 result += self.$cweek_cyear()['$[]'](0).$to_s().$rjust(2, "0"); 21361 break; 21362 21363 case 'G': 21364 result += self.$cweek_cyear()['$[]'](1); 21365 break; 21366 21367 case 'g': 21368 result += self.$cweek_cyear()['$[]'](1)['$[]']($range(-2, -1, false)); 21369 break; 21370 21371 case 's': 21372 result += self.$to_i(); 21373 break; 21374 21375 case 'n': 21376 result += "\n"; 21377 break; 21378 21379 case 't': 21380 result += "\t"; 21381 break; 21382 21383 case '%': 21384 result += "%"; 21385 break; 21386 21387 case 'c': 21388 result += self.$strftime("%a %b %e %T %Y"); 21389 break; 21390 21391 case 'D': 21392 case 'x': 21393 result += self.$strftime("%m/%d/%y"); 21394 break; 21395 21396 case 'F': 21397 result += self.$strftime("%Y-%m-%d"); 21398 break; 21399 21400 case 'v': 21401 result += self.$strftime("%e-%^b-%4Y"); 21402 break; 21403 21404 case 'r': 21405 result += self.$strftime("%I:%M:%S %p"); 21406 break; 21407 21408 case 'R': 21409 result += self.$strftime("%H:%M"); 21410 break; 21411 21412 case 'T': 21413 case 'X': 21414 result += self.$strftime("%H:%M:%S"); 21415 break; 21416 21417 // Non-standard: JIS X 0301 date format 21418 case 'J': 21419 jd = self.$to_date().$jd(); 21420 if (jd < 2405160) { 21421 result += self.$strftime("%Y-%m-%d"); 21422 break; 21423 } 21424 else if (jd < 2419614) 21425 c = 'M', s = 1867; 21426 else if (jd < 2424875) 21427 c = 'T', s = 1911; 21428 else if (jd < 2447535) 21429 c = 'S', s = 1925; 21430 else if (jd < 2458605) 21431 c = 'H', s = 1988; 21432 else 21433 c = 'R', s = 2018; 21434 21435 result += self.$format("%c%02d", c, $rb_minus(self.$year(), s)); 21436 result += self.$strftime("-%m-%d"); 21437 break; 21438 21439 default: 21440 return full; 21441 } 21442 21443 if (upcase) { 21444 result = result.toUpperCase(); 21445 } 21446 21447 if (invert) { 21448 result = result.replace(/[A-Z]/, function(c) { c.toLowerCase(); }). 21449 replace(/[a-z]/, function(c) { c.toUpperCase(); }); 21450 } 21451 21452 if (pad && (zero || blank)) { 21453 result = (result).$rjust(isNaN(width) ? 2 : width, blank ? " " : "0"); 21454 } 21455 21456 return result; 21457 }); 21458 21459 }); 21460 21461 $def(self, '$to_a', function $$to_a() { 21462 var self = this; 21463 21464 return [self.$sec(), self.$min(), self.$hour(), self.$day(), self.$month(), self.$year(), self.$wday(), self.$yday(), self.$isdst(), self.$zone()] 21465 }); 21466 21467 $def(self, '$to_f', function $$to_f() { 21468 var self = this; 21469 21470 return self.getTime() / 1000; 21471 }); 21472 21473 $def(self, '$to_i', function $$to_i() { 21474 var self = this; 21475 21476 return parseInt(self.getTime() / 1000, 10); 21477 }); 21478 21479 $def(self, '$cweek_cyear', function $$cweek_cyear() { 21480 var self = this, jan01 = nil, jan01_wday = nil, year = nil, offset = nil, week = nil, dec31 = nil, dec31_wday = nil; 21481 21482 21483 jan01 = $$$('Time').$new(self.$year(), 1, 1); 21484 jan01_wday = jan01.$wday(); 21485 year = self.$year(); 21486 if (($truthy($rb_le(jan01_wday, 4)) && ($neqeq(jan01_wday, 0)))) { 21487 offset = $rb_minus(jan01_wday, 1); 21488 } else { 21489 21490 offset = $rb_minus($rb_minus(jan01_wday, 7), 1); 21491 if ($eqeq(offset, -8)) { 21492 offset = -1; 21493 } } week = $rb_divide($rb_plus(self.$yday(), offset), 7.0).$ceil(); 21494 if ($truthy($rb_le(week, 0))) { 21495 return $$$('Time').$new($rb_minus(self.$year(), 1), 12, 31).$cweek_cyear() 21496 } else if ($eqeq(week, 53)) { 21497 21498 dec31 = $$$('Time').$new(self.$year(), 12, 31); 21499 dec31_wday = dec31.$wday(); 21500 if (($truthy($rb_le(dec31_wday, 3)) && ($neqeq(dec31_wday, 0)))) { 21501 21502 week = 1; 21503 year = $rb_plus(year, 1); 21504 } } return [week, year]; 21505 }); 21506 (function(self, $parent_nesting) { 21507 21508 21509 $alias(self, "mktime", "local"); 21510 return $alias(self, "utc", "gm"); 21511 })(Opal.get_singleton_class(self)); 21512 $alias(self, "ctime", "asctime"); 21513 $alias(self, "dst?", "isdst"); 21514 $alias(self, "getutc", "getgm"); 21515 $alias(self, "gmtoff", "gmt_offset"); 21516 $alias(self, "mday", "day"); 21517 $alias(self, "month", "mon"); 21518 $alias(self, "to_s", "inspect"); 21519 $alias(self, "tv_sec", "to_i"); 21520 $alias(self, "tv_usec", "usec"); 21521 $alias(self, "utc", "gmtime"); 21522 $alias(self, "utc?", "gmt?"); 21523 return $alias(self, "utc_offset", "gmt_offset"); 21524 })('::', Date, $nesting); 21525}; 21526 21527Opal.modules["corelib/struct"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21528 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.$$$; 21529 21530 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'); 21531 21532 self.$require("corelib/enumerable"); 21533 return (function($base, $super, $parent_nesting) { 21534 var self = $klass($base, $super, 'Struct'); 21535 21536 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 21537 21538 21539 self.$include($$$('Enumerable')); 21540 $defs(self, '$new', function $Struct_new$1(const_name, $a, $b) { 21541 var block = $Struct_new$1.$$p || nil, $post_args, $kwargs, args, keyword_init, self = this, klass = nil; 21542 21543 $Struct_new$1.$$p = null; 21544 $post_args = $slice(arguments, 1); 21545 $kwargs = $extract_kwargs($post_args); 21546 $kwargs = $ensure_kwargs($kwargs); 21547 args = $post_args; 21548 21549 keyword_init = $kwargs.$$smap["keyword_init"];if (keyword_init == null) keyword_init = false; 21550 if ($truthy(const_name)) { 21551 if (($eqeq(const_name.$class(), $$$('String')) && ($neqeq(const_name['$[]'](0).$upcase(), const_name['$[]'](0))))) { 21552 21553 args.$unshift(const_name); 21554 const_name = nil; 21555 } else { 21556 21557 try { 21558 const_name = $Opal['$const_name!'](const_name); 21559 } catch ($err) { 21560 if (Opal.rescue($err, [$$$('TypeError'), $$$('NameError')])) { 21561 try { 21562 21563 args.$unshift(const_name); 21564 const_name = nil; 21565 } finally { Opal.pop_exception(); } 21566 } else { throw $err; } 21567 } } 21568 } $send(args, 'map', [], function $$2(arg){ 21569 21570 if (arg == null) arg = nil; 21571 return $Opal['$coerce_to!'](arg, $$$('String'), "to_str");}); 21572 klass = $send($Class, 'new', [self], function $$3(){var self = $$3.$$s == null ? this : $$3.$$s; 21573 21574 21575 $send(args, 'each', [], function $$4(arg){var self = $$4.$$s == null ? this : $$4.$$s; 21576 21577 21578 if (arg == null) arg = nil; 21579 return self.$define_struct_attribute(arg);}, {$$s: self}); 21580 return (function(self, $parent_nesting) { 21581 21582 21583 21584 $def(self, '$new', function $new$5($a) { 21585 var $post_args, args, self = this, instance = nil; 21586 21587 21588 $post_args = $slice(arguments); 21589 args = $post_args; 21590 instance = self.$allocate(); 21591 instance.$$data = {}; 21592 $send(instance, 'initialize', $to_a(args)); 21593 return instance; 21594 }, -1); 21595 return self.$alias_method("[]", "new"); 21596 })(Opal.get_singleton_class(self));}, {$$s: self}); 21597 if ($truthy(block)) { 21598 $send(klass, 'module_eval', [], block.$to_proc()); 21599 } klass.$$keyword_init = keyword_init; 21600 if ($truthy(const_name)) { 21601 $$$('Struct').$const_set(const_name, klass); 21602 } return klass; 21603 }, -2); 21604 $defs(self, '$define_struct_attribute', function $$define_struct_attribute(name) { 21605 var self = this; 21606 21607 21608 if ($eqeq(self, $$$('Struct'))) { 21609 $Kernel.$raise($$$('ArgumentError'), "you cannot define attributes to the Struct class"); 21610 } self.$members()['$<<'](name); 21611 $send(self, 'define_method', [name], function $$6(){var self = $$6.$$s == null ? this : $$6.$$s; 21612 21613 return self.$$data[name];}, {$$s: self}); 21614 return $send(self, 'define_method', ["" + (name) + "="], function $$7(value){var self = $$7.$$s == null ? this : $$7.$$s; 21615 21616 21617 if (value == null) value = nil; 21618 return self.$$data[name] = value;}, {$$s: self}); 21619 }); 21620 $defs(self, '$members', function $$members() { 21621 var self = this, $ret_or_1 = nil; 21622 if (self.members == null) self.members = nil; 21623 21624 21625 if ($eqeq(self, $$$('Struct'))) { 21626 $Kernel.$raise($$$('ArgumentError'), "the Struct class has no members"); 21627 } return (self.members = ($truthy(($ret_or_1 = self.members)) ? ($ret_or_1) : ([]))); 21628 }); 21629 $defs(self, '$inherited', function $$inherited(klass) { 21630 var self = this, members = nil; 21631 if (self.members == null) self.members = nil; 21632 21633 21634 members = self.members; 21635 return $send(klass, 'instance_eval', [], function $$8(){var self = $$8.$$s == null ? this : $$8.$$s; 21636 21637 return (self.members = members)}, {$$s: self}); 21638 }); 21639 21640 $def(self, '$initialize', function $$initialize($a) { 21641 var $post_args, args, self = this, kwargs = nil, $ret_or_1 = nil, extra = nil; 21642 21643 21644 $post_args = $slice(arguments); 21645 args = $post_args; 21646 if ($truthy(self.$class().$$keyword_init)) { 21647 21648 kwargs = ($truthy(($ret_or_1 = args.$last())) ? ($ret_or_1) : ($hash2([], {}))); 21649 if (($truthy($rb_gt(args.$length(), 1)) || ($truthy((args.length === 1 && !kwargs.$$is_hash))))) { 21650 $Kernel.$raise($$$('ArgumentError'), "wrong number of arguments (given " + (args.$length()) + ", expected 0)"); 21651 } extra = $rb_minus(kwargs.$keys(), self.$class().$members()); 21652 if ($truthy(extra['$any?']())) { 21653 $Kernel.$raise($$$('ArgumentError'), "unknown keywords: " + (extra.$join(", "))); 21654 } return $send(self.$class().$members(), 'each', [], function $$9(name){var $b, self = $$9.$$s == null ? this : $$9.$$s; 21655 21656 21657 if (name == null) name = nil; 21658 return ($b = [name, kwargs['$[]'](name)], $send(self, '[]=', $b), $b[$b.length - 1]);}, {$$s: self}); 21659 } else { 21660 21661 if ($truthy($rb_gt(args.$length(), self.$class().$members().$length()))) { 21662 $Kernel.$raise($$$('ArgumentError'), "struct size differs"); 21663 } return $send(self.$class().$members(), 'each_with_index', [], function $$10(name, index){var $b, self = $$10.$$s == null ? this : $$10.$$s; 21664 21665 21666 if (name == null) name = nil; 21667 if (index == null) index = nil; 21668 return ($b = [name, args['$[]'](index)], $send(self, '[]=', $b), $b[$b.length - 1]);}, {$$s: self}); 21669 } }, -1); 21670 21671 $def(self, '$initialize_copy', function $$initialize_copy(from) { 21672 var self = this; 21673 21674 21675 self.$$data = {}; 21676 var keys = Object.keys(from.$$data), i, max, name; 21677 for (i = 0, max = keys.length; i < max; i++) { 21678 name = keys[i]; 21679 self.$$data[name] = from.$$data[name]; 21680 } 21681 21682 }); 21683 $defs(self, '$keyword_init?', function $Struct_keyword_init$ques$11() { 21684 var self = this; 21685 21686 return self.$$keyword_init; 21687 }); 21688 21689 $def(self, '$members', function $$members() { 21690 var self = this; 21691 21692 return self.$class().$members() 21693 }); 21694 21695 $def(self, '$hash', function $$hash() { 21696 var self = this; 21697 21698 return $$('Hash').$new(self.$$data).$hash() 21699 }); 21700 21701 $def(self, '$[]', function $Struct_$$$12(name) { 21702 var self = this; 21703 21704 21705 if ($eqeqeq($$$('Integer'), name)) { 21706 21707 if ($truthy($rb_lt(name, self.$class().$members().$size()['$-@']()))) { 21708 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too small for struct(size:" + (self.$class().$members().$size()) + ")"); 21709 } if ($truthy($rb_ge(name, self.$class().$members().$size()))) { 21710 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too large for struct(size:" + (self.$class().$members().$size()) + ")"); 21711 } name = self.$class().$members()['$[]'](name); 21712 } else if ($eqeqeq($$$('String'), name)) { 21713 21714 if(!self.$$data.hasOwnProperty(name)) { 21715 $Kernel.$raise($$$('NameError').$new("no member '" + (name) + "' in struct", name)); 21716 } 21717 21718 } else { 21719 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + (name.$class()) + " into Integer"); 21720 } name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 21721 return self.$$data[name]; }); 21722 21723 $def(self, '$[]=', function $Struct_$$$eq$13(name, value) { 21724 var self = this; 21725 21726 21727 if ($eqeqeq($$$('Integer'), name)) { 21728 21729 if ($truthy($rb_lt(name, self.$class().$members().$size()['$-@']()))) { 21730 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too small for struct(size:" + (self.$class().$members().$size()) + ")"); 21731 } if ($truthy($rb_ge(name, self.$class().$members().$size()))) { 21732 $Kernel.$raise($$$('IndexError'), "offset " + (name) + " too large for struct(size:" + (self.$class().$members().$size()) + ")"); 21733 } name = self.$class().$members()['$[]'](name); 21734 } else if ($eqeqeq($$$('String'), name)) { 21735 if (!$truthy(self.$class().$members()['$include?'](name.$to_sym()))) { 21736 $Kernel.$raise($$$('NameError').$new("no member '" + (name) + "' in struct", name)); 21737 } 21738 } else { 21739 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + (name.$class()) + " into Integer"); 21740 } name = $Opal['$coerce_to!'](name, $$$('String'), "to_str"); 21741 return self.$$data[name] = value; }); 21742 21743 $def(self, '$==', function $Struct_$eq_eq$14(other) { 21744 var self = this; 21745 21746 21747 if (!$truthy(other['$instance_of?'](self.$class()))) { 21748 return false 21749 } 21750 var recursed1 = {}, recursed2 = {}; 21751 21752 function _eqeq(struct, other) { 21753 var key, a, b; 21754 21755 recursed1[(struct).$__id__()] = true; 21756 recursed2[(other).$__id__()] = true; 21757 21758 for (key in struct.$$data) { 21759 a = struct.$$data[key]; 21760 b = other.$$data[key]; 21761 21762 if ($$$('Struct')['$==='](a)) { 21763 if (!recursed1.hasOwnProperty((a).$__id__()) || !recursed2.hasOwnProperty((b).$__id__())) { 21764 if (!_eqeq(a, b)) { 21765 return false; 21766 } 21767 } 21768 } else { 21769 if (!(a)['$=='](b)) { 21770 return false; 21771 } 21772 } 21773 } 21774 21775 return true; 21776 } 21777 21778 return _eqeq(self, other); 21779 }); 21780 21781 $def(self, '$eql?', function $Struct_eql$ques$15(other) { 21782 var self = this; 21783 21784 21785 if (!$truthy(other['$instance_of?'](self.$class()))) { 21786 return false 21787 } 21788 var recursed1 = {}, recursed2 = {}; 21789 21790 function _eqeq(struct, other) { 21791 var key, a, b; 21792 21793 recursed1[(struct).$__id__()] = true; 21794 recursed2[(other).$__id__()] = true; 21795 21796 for (key in struct.$$data) { 21797 a = struct.$$data[key]; 21798 b = other.$$data[key]; 21799 21800 if ($$$('Struct')['$==='](a)) { 21801 if (!recursed1.hasOwnProperty((a).$__id__()) || !recursed2.hasOwnProperty((b).$__id__())) { 21802 if (!_eqeq(a, b)) { 21803 return false; 21804 } 21805 } 21806 } else { 21807 if (!(a)['$eql?'](b)) { 21808 return false; 21809 } 21810 } 21811 } 21812 21813 return true; 21814 } 21815 21816 return _eqeq(self, other); 21817 }); 21818 21819 $def(self, '$each', function $$each() { 21820 var $yield = $$each.$$p || nil, self = this; 21821 21822 $$each.$$p = null; 21823 21824 if (!($yield !== nil)) { 21825 return $send(self, 'enum_for', ["each"], function $$16(){var self = $$16.$$s == null ? this : $$16.$$s; 21826 21827 return self.$size()}, {$$s: self}) 21828 } $send(self.$class().$members(), 'each', [], function $$17(name){var self = $$17.$$s == null ? this : $$17.$$s; 21829 21830 21831 if (name == null) name = nil; 21832 return Opal.yield1($yield, self['$[]'](name));}, {$$s: self}); 21833 return self; 21834 }); 21835 21836 $def(self, '$each_pair', function $$each_pair() { 21837 var $yield = $$each_pair.$$p || nil, self = this; 21838 21839 $$each_pair.$$p = null; 21840 21841 if (!($yield !== nil)) { 21842 return $send(self, 'enum_for', ["each_pair"], function $$18(){var self = $$18.$$s == null ? this : $$18.$$s; 21843 21844 return self.$size()}, {$$s: self}) 21845 } $send(self.$class().$members(), 'each', [], function $$19(name){var self = $$19.$$s == null ? this : $$19.$$s; 21846 21847 21848 if (name == null) name = nil; 21849 return Opal.yield1($yield, [name, self['$[]'](name)]);}, {$$s: self}); 21850 return self; 21851 }); 21852 21853 $def(self, '$length', function $$length() { 21854 var self = this; 21855 21856 return self.$class().$members().$length() 21857 }); 21858 21859 $def(self, '$to_a', function $$to_a() { 21860 var self = this; 21861 21862 return $send(self.$class().$members(), 'map', [], function $$20(name){var self = $$20.$$s == null ? this : $$20.$$s; 21863 21864 21865 if (name == null) name = nil; 21866 return self['$[]'](name);}, {$$s: self}) 21867 }); 21868 var inspect_stack = []; 21869 21870 $def(self, '$inspect', function $$inspect() { 21871 var self = this, result = nil, pushed = nil; 21872 21873 return (function() { try { 21874 21875 result = "#<struct "; 21876 if ($truthy((inspect_stack)['$include?'](self.$__id__()))) { 21877 return $rb_plus(result, ":...>") 21878 } else { 21879 21880 (inspect_stack)['$<<'](self.$__id__()); 21881 pushed = true; 21882 if (($eqeqeq($$$('Struct'), self) && ($truthy(self.$class().$name())))) { 21883 result = $rb_plus(result, "" + (self.$class()) + " "); 21884 }; 21885 result = $rb_plus(result, $send(self.$each_pair(), 'map', [], function $$21(name, value){ 21886 21887 if (name == null) name = nil; 21888 if (value == null) value = nil; 21889 return "" + (name) + "=" + ($$('Opal').$inspect(value));}).$join(", ")); 21890 result = $rb_plus(result, ">"); 21891 return result; 21892 }; 21893 } finally { 21894 ($truthy(pushed) ? (inspect_stack.pop()) : nil); 21895 } })() 21896 }); 21897 21898 $def(self, '$to_h', function $$to_h() { 21899 var block = $$to_h.$$p || nil, self = this; 21900 21901 $$to_h.$$p = null; 21902 if ((block !== nil)) { 21903 return $send($send(self, 'map', [], block.$to_proc()), 'to_h', $to_a(self.$args())) 21904 } return $send(self.$class().$members(), 'each_with_object', [$hash2([], {})], function $$22(name, h){var $a, self = $$22.$$s == null ? this : $$22.$$s; 21905 21906 21907 if (name == null) name = nil; 21908 if (h == null) h = nil; 21909 return ($a = [name, self['$[]'](name)], $send(h, '[]=', $a), $a[$a.length - 1]);}, {$$s: self}); 21910 }); 21911 21912 $def(self, '$values_at', function $$values_at($a) { 21913 var $post_args, args, self = this; 21914 21915 21916 $post_args = $slice(arguments); 21917 args = $post_args; 21918 args = $send(args, 'map', [], function $$23(arg){ 21919 21920 if (arg == null) arg = nil; 21921 return arg.$$is_range ? arg.$to_a() : arg;}).$flatten(); 21922 21923 var result = []; 21924 for (var i = 0, len = args.length; i < len; i++) { 21925 if (!args[i].$$is_number) { 21926 $Kernel.$raise($$$('TypeError'), "no implicit conversion of " + ((args[i]).$class()) + " into Integer"); 21927 } 21928 result.push(self['$[]'](args[i])); 21929 } 21930 return result; 21931 }, -1); 21932 21933 $def(self, '$dig', function $$dig(key, $a) { 21934 var $post_args, keys, self = this, item = nil; 21935 21936 21937 $post_args = $slice(arguments, 1); 21938 keys = $post_args; 21939 item = ($truthy(key.$$is_string && self.$$data.hasOwnProperty(key)) ? (self.$$data[key] || nil) : nil); 21940 21941 if (item === nil || keys.length === 0) { 21942 return item; 21943 } 21944 if (!$truthy(item['$respond_to?']("dig"))) { 21945 $Kernel.$raise($$$('TypeError'), "" + (item.$class()) + " does not have #dig method"); 21946 } return $send(item, 'dig', $to_a(keys)); 21947 }, -2); 21948 $alias(self, "size", "length"); 21949 $alias(self, "to_s", "inspect"); 21950 return $alias(self, "values", "to_a"); 21951 })('::', null, $nesting); 21952}; 21953 21954Opal.modules["corelib/set"] = function(Opal) {/* Generated by Opal 1.7.3 */ 21955 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.$$$; 21956 21957 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!'); 21958 return (function($base, $super, $parent_nesting) { 21959 var self = $klass($base, $super, 'Set'); 21960 21961 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $ret_or_1 = nil, $proto = self.$$prototype; 21962 21963 $proto.hash = nil; 21964 21965 self.$include($$$('Enumerable')); 21966 $defs(self, '$[]', function $Set_$$$1($a) { 21967 var $post_args, ary, self = this; 21968 21969 21970 $post_args = $slice(arguments); 21971 ary = $post_args; 21972 return self.$new(ary); 21973 }, -1); 21974 21975 $def(self, '$initialize', function $$initialize(enum$) { 21976 var block = $$initialize.$$p || nil, self = this; 21977 21978 $$initialize.$$p = null; 21979 if (enum$ == null) enum$ = nil; 21980 self.hash = $hash2([], {}); 21981 if ($truthy(enum$['$nil?']())) { 21982 return nil 21983 } if (!$eqeqeq($$$('Enumerable'), enum$)) { 21984 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable"); 21985 } if ($truthy(block)) { 21986 return $send(enum$, 'each', [], function $$2(item){var self = $$2.$$s == null ? this : $$2.$$s; 21987 21988 21989 if (item == null) item = nil; 21990 return self.$add(Opal.yield1(block, item));}, {$$s: self}) 21991 } else { 21992 return self.$merge(enum$) 21993 } }, -1); 21994 21995 $def(self, '$dup', function $$dup() { 21996 var self = this, result = nil; 21997 21998 21999 result = self.$class().$new(); 22000 return result.$merge(self); 22001 }); 22002 22003 $def(self, '$-', function $Set_$minus$3(enum$) { 22004 var self = this; 22005 22006 22007 if (!$truthy(enum$['$respond_to?']("each"))) { 22008 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable"); 22009 } return self.$dup().$subtract(enum$); 22010 }); 22011 22012 $def(self, '$inspect', function $$inspect() { 22013 var self = this; 22014 22015 return "#<Set: {" + (self.$to_a().$join(",")) + "}>" 22016 }); 22017 22018 $def(self, '$==', function $Set_$eq_eq$4(other) { 22019 var self = this; 22020 22021 if ($truthy(self['$equal?'](other))) { 22022 return true 22023 } else if ($truthy(other['$instance_of?'](self.$class()))) { 22024 return self.hash['$=='](other.$instance_variable_get("@hash")) 22025 } else if (($truthy(other['$is_a?']($$$('Set'))) && ($eqeq(self.$size(), other.$size())))) { 22026 return $send(other, 'all?', [], function $$5(o){var self = $$5.$$s == null ? this : $$5.$$s; 22027 if (self.hash == null) self.hash = nil; 22028 22029 22030 if (o == null) o = nil; 22031 return self.hash['$include?'](o);}, {$$s: self}) 22032 } else { 22033 return false 22034 } 22035 }); 22036 22037 $def(self, '$add', function $$add(o) { 22038 var self = this; 22039 22040 22041 self.hash['$[]='](o, true); 22042 return self; 22043 }); 22044 22045 $def(self, '$classify', function $$classify() { 22046 var block = $$classify.$$p || nil, self = this, result = nil; 22047 22048 $$classify.$$p = null; 22049 if (!(block !== nil)) { 22050 return self.$enum_for("classify") 22051 } result = $send($$$('Hash'), 'new', [], function $$6(h, k){var $a, self = $$6.$$s == null ? this : $$6.$$s; 22052 22053 22054 if (h == null) h = nil; 22055 if (k == null) k = nil; 22056 return ($a = [k, self.$class().$new()], $send(h, '[]=', $a), $a[$a.length - 1]);}, {$$s: self}); 22057 $send(self, 'each', [], function $$7(item){ 22058 22059 if (item == null) item = nil; 22060 return result['$[]'](Opal.yield1(block, item)).$add(item);}); 22061 return result; 22062 }); 22063 22064 $def(self, '$collect!', function $Set_collect$excl$8() { 22065 var block = $Set_collect$excl$8.$$p || nil, self = this, result = nil; 22066 22067 $Set_collect$excl$8.$$p = null; 22068 if (!(block !== nil)) { 22069 return self.$enum_for("collect!") 22070 } result = self.$class().$new(); 22071 $send(self, 'each', [], function $$9(item){ 22072 22073 if (item == null) item = nil; 22074 return result['$<<'](Opal.yield1(block, item));}); 22075 return self.$replace(result); 22076 }); 22077 22078 $def(self, '$compare_by_identity', function $$compare_by_identity() { 22079 var self = this; 22080 22081 if ($truthy(self.hash['$respond_to?']("compare_by_identity"))) { 22082 22083 self.hash.$compare_by_identity(); 22084 return self; 22085 } else { 22086 return self.$raise($$('NotImplementedError'), "" + (self.$class().$name()) + "#" + ("compare_by_identity") + " is not implemented") 22087 } 22088 }); 22089 22090 $def(self, '$compare_by_identity?', function $Set_compare_by_identity$ques$10() { 22091 var self = this, $ret_or_1 = nil; 22092 22093 if ($truthy(($ret_or_1 = self.hash['$respond_to?']("compare_by_identity?")))) { 22094 return self.hash['$compare_by_identity?']() 22095 } else { 22096 return $ret_or_1 22097 } 22098 }); 22099 22100 $def(self, '$delete', function $Set_delete$11(o) { 22101 var self = this; 22102 22103 22104 self.hash.$delete(o); 22105 return self; 22106 }); 22107 22108 $def(self, '$delete?', function $Set_delete$ques$12(o) { 22109 var self = this; 22110 22111 if ($truthy(self['$include?'](o))) { 22112 22113 self.$delete(o); 22114 return self; 22115 } else { 22116 return nil 22117 } 22118 }); 22119 22120 $def(self, '$delete_if', function $$delete_if() { 22121 var $yield = $$delete_if.$$p || nil, self = this; 22122 22123 $$delete_if.$$p = null; 22124 22125 if (!($yield !== nil)) { 22126 return self.$enum_for("delete_if") 22127 } $send($send(self, 'select', [], function $$13(o){ 22128 22129 if (o == null) o = nil; 22130 return Opal.yield1($yield, o);}), 'each', [], function $$14(o){var self = $$14.$$s == null ? this : $$14.$$s; 22131 if (self.hash == null) self.hash = nil; 22132 22133 22134 if (o == null) o = nil; 22135 return self.hash.$delete(o);}, {$$s: self}); 22136 return self; 22137 }); 22138 22139 $def(self, '$freeze', function $$freeze() { 22140 var self = this; 22141 22142 22143 if ($truthy(self['$frozen?']())) { 22144 return self 22145 } self.hash.$freeze(); 22146 return $freeze(self); }); 22147 22148 $def(self, '$keep_if', function $$keep_if() { 22149 var $yield = $$keep_if.$$p || nil, self = this; 22150 22151 $$keep_if.$$p = null; 22152 22153 if (!($yield !== nil)) { 22154 return self.$enum_for("keep_if") 22155 } $send($send(self, 'reject', [], function $$15(o){ 22156 22157 if (o == null) o = nil; 22158 return Opal.yield1($yield, o);}), 'each', [], function $$16(o){var self = $$16.$$s == null ? this : $$16.$$s; 22159 if (self.hash == null) self.hash = nil; 22160 22161 22162 if (o == null) o = nil; 22163 return self.hash.$delete(o);}, {$$s: self}); 22164 return self; 22165 }); 22166 22167 $def(self, '$reject!', function $Set_reject$excl$17() { 22168 var block = $Set_reject$excl$17.$$p || nil, self = this, before = nil; 22169 22170 $Set_reject$excl$17.$$p = null; 22171 if (!(block !== nil)) { 22172 return self.$enum_for("reject!") 22173 } before = self.$size(); 22174 $send(self, 'delete_if', [], block.$to_proc()); 22175 if ($eqeq(self.$size(), before)) { 22176 return nil 22177 } else { 22178 return self 22179 } }); 22180 22181 $def(self, '$select!', function $Set_select$excl$18() { 22182 var block = $Set_select$excl$18.$$p || nil, self = this, before = nil; 22183 22184 $Set_select$excl$18.$$p = null; 22185 if (!(block !== nil)) { 22186 return self.$enum_for("select!") 22187 } before = self.$size(); 22188 $send(self, 'keep_if', [], block.$to_proc()); 22189 if ($eqeq(self.$size(), before)) { 22190 return nil 22191 } else { 22192 return self 22193 } }); 22194 22195 $def(self, '$add?', function $Set_add$ques$19(o) { 22196 var self = this; 22197 22198 if ($truthy(self['$include?'](o))) { 22199 return nil 22200 } else { 22201 return self.$add(o) 22202 } 22203 }); 22204 22205 $def(self, '$each', function $$each() { 22206 var block = $$each.$$p || nil, self = this; 22207 22208 $$each.$$p = null; 22209 if (!(block !== nil)) { 22210 return self.$enum_for("each") 22211 } $send(self.hash, 'each_key', [], block.$to_proc()); 22212 return self; 22213 }); 22214 22215 $def(self, '$empty?', function $Set_empty$ques$20() { 22216 var self = this; 22217 22218 return self.hash['$empty?']() 22219 }); 22220 22221 $def(self, '$eql?', function $Set_eql$ques$21(other) { 22222 var self = this; 22223 22224 return self.hash['$eql?']($send(other, 'instance_eval', [], function $$22(){var self = $$22.$$s == null ? this : $$22.$$s; 22225 if (self.hash == null) self.hash = nil; 22226 22227 return self.hash}, {$$s: self})) 22228 }); 22229 22230 $def(self, '$clear', function $$clear() { 22231 var self = this; 22232 22233 22234 self.hash.$clear(); 22235 return self; 22236 }); 22237 22238 $def(self, '$include?', function $Set_include$ques$23(o) { 22239 var self = this; 22240 22241 return self.hash['$include?'](o) 22242 }); 22243 22244 $def(self, '$merge', function $$merge(enum$) { 22245 var self = this; 22246 22247 22248 $send(enum$, 'each', [], function $$24(item){var self = $$24.$$s == null ? this : $$24.$$s; 22249 22250 22251 if (item == null) item = nil; 22252 return self.$add(item);}, {$$s: self}); 22253 return self; 22254 }); 22255 22256 $def(self, '$replace', function $$replace(enum$) { 22257 var self = this; 22258 22259 22260 self.$clear(); 22261 self.$merge(enum$); 22262 return self; 22263 }); 22264 22265 $def(self, '$size', function $$size() { 22266 var self = this; 22267 22268 return self.hash.$size() 22269 }); 22270 22271 $def(self, '$subtract', function $$subtract(enum$) { 22272 var self = this; 22273 22274 22275 $send(enum$, 'each', [], function $$25(item){var self = $$25.$$s == null ? this : $$25.$$s; 22276 22277 22278 if (item == null) item = nil; 22279 return self.$delete(item);}, {$$s: self}); 22280 return self; 22281 }); 22282 22283 $def(self, '$|', function $Set_$$26(enum$) { 22284 var self = this; 22285 22286 22287 if (!$truthy(enum$['$respond_to?']("each"))) { 22288 $Kernel.$raise($$$('ArgumentError'), "value must be enumerable"); 22289 } return self.$dup().$merge(enum$); 22290 }); 22291 22292 function is_set(set) { 22293 ($truthy(($ret_or_1 = (set)['$is_a?']($$$('Set')))) ? ($ret_or_1) : ($Kernel.$raise($$$('ArgumentError'), "value must be a set"))); 22294 } 22295 22296 $def(self, '$superset?', function $Set_superset$ques$27(set) { 22297 var self = this; 22298 22299 22300 is_set(set); 22301 if ($truthy($rb_lt(self.$size(), set.$size()))) { 22302 return false 22303 } return $send(set, 'all?', [], function $$28(o){var self = $$28.$$s == null ? this : $$28.$$s; 22304 22305 22306 if (o == null) o = nil; 22307 return self['$include?'](o);}, {$$s: self}); 22308 }); 22309 22310 $def(self, '$proper_superset?', function $Set_proper_superset$ques$29(set) { 22311 var self = this; 22312 22313 22314 is_set(set); 22315 if ($truthy($rb_le(self.$size(), set.$size()))) { 22316 return false 22317 } return $send(set, 'all?', [], function $$30(o){var self = $$30.$$s == null ? this : $$30.$$s; 22318 22319 22320 if (o == null) o = nil; 22321 return self['$include?'](o);}, {$$s: self}); 22322 }); 22323 22324 $def(self, '$subset?', function $Set_subset$ques$31(set) { 22325 var self = this; 22326 22327 22328 is_set(set); 22329 if ($truthy($rb_lt(set.$size(), self.$size()))) { 22330 return false 22331 } return $send(self, 'all?', [], function $$32(o){ 22332 22333 if (o == null) o = nil; 22334 return set['$include?'](o);}); 22335 }); 22336 22337 $def(self, '$proper_subset?', function $Set_proper_subset$ques$33(set) { 22338 var self = this; 22339 22340 22341 is_set(set); 22342 if ($truthy($rb_le(set.$size(), self.$size()))) { 22343 return false 22344 } return $send(self, 'all?', [], function $$34(o){ 22345 22346 if (o == null) o = nil; 22347 return set['$include?'](o);}); 22348 }); 22349 22350 $def(self, '$intersect?', function $Set_intersect$ques$35(set) { 22351 var self = this; 22352 22353 22354 is_set(set); 22355 if ($truthy($rb_lt(self.$size(), set.$size()))) { 22356 return $send(self, 'any?', [], function $$36(o){ 22357 22358 if (o == null) o = nil; 22359 return set['$include?'](o);}) 22360 } else { 22361 return $send(set, 'any?', [], function $$37(o){var self = $$37.$$s == null ? this : $$37.$$s; 22362 22363 22364 if (o == null) o = nil; 22365 return self['$include?'](o);}, {$$s: self}) 22366 } }); 22367 22368 $def(self, '$disjoint?', function $Set_disjoint$ques$38(set) { 22369 var self = this; 22370 22371 return self['$intersect?'](set)['$!']() 22372 }); 22373 22374 $def(self, '$to_a', function $$to_a() { 22375 var self = this; 22376 22377 return self.hash.$keys() 22378 }); 22379 $alias(self, "+", "|"); 22380 $alias(self, "<", "proper_subset?"); 22381 $alias(self, "<<", "add"); 22382 $alias(self, "<=", "subset?"); 22383 $alias(self, ">", "proper_superset?"); 22384 $alias(self, ">=", "superset?"); 22385 $alias(self, "difference", "-"); 22386 $alias(self, "filter!", "select!"); 22387 $alias(self, "length", "size"); 22388 $alias(self, "map!", "collect!"); 22389 $alias(self, "member?", "include?"); 22390 return $alias(self, "union", "|"); 22391 })('::', null, $nesting) 22392}; 22393 22394Opal.modules["corelib/dir"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22395 var $klass = Opal.klass, $def = Opal.def, $truthy = Opal.truthy, $alias = Opal.alias, nil = Opal.nil, $$$ = Opal.$$$; 22396 22397 Opal.add_stubs('[],pwd'); 22398 return (function($base, $super, $parent_nesting) { 22399 var self = $klass($base, $super, 'Dir'); 22400 22401 return (function(self, $parent_nesting) { 22402 22403 22404 22405 $def(self, '$chdir', function $$chdir(dir) { 22406 var $yield = $$chdir.$$p || nil, prev_cwd = nil; 22407 22408 $$chdir.$$p = null; 22409 return (function() { try { 22410 22411 prev_cwd = Opal.current_dir; 22412 Opal.current_dir = dir; 22413 return Opal.yieldX($yield, []);; 22414 } finally { 22415 Opal.current_dir = prev_cwd; 22416 } })() 22417 }); 22418 22419 $def(self, '$pwd', function $$pwd() { 22420 22421 return Opal.current_dir || '.'; 22422 }); 22423 22424 $def(self, '$home', function $$home() { 22425 var $ret_or_1 = nil; 22426 22427 if ($truthy(($ret_or_1 = $$$('ENV')['$[]']("HOME")))) { 22428 return $ret_or_1 22429 } else { 22430 return "." 22431 } 22432 }); 22433 return $alias(self, "getwd", "pwd"); 22434 })(Opal.get_singleton_class(self)) 22435 })('::', null) 22436}; 22437 22438Opal.modules["corelib/file"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22439 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.$$$; 22440 22441 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?'); 22442 return (function($base, $super, $parent_nesting) { 22443 var self = $klass($base, $super, 'File'); 22444 22445 var $nesting = [self].concat($parent_nesting), windows_root_rx = nil; 22446 22447 22448 $const_set($nesting[0], 'Separator', $const_set($nesting[0], 'SEPARATOR', "/")); 22449 $const_set($nesting[0], 'ALT_SEPARATOR', nil); 22450 $const_set($nesting[0], 'PATH_SEPARATOR', ":"); 22451 $const_set($nesting[0], 'FNM_SYSCASE', 0); 22452 windows_root_rx = /^[a-zA-Z]:(?:\\|\/)/; 22453 return (function(self, $parent_nesting) { 22454 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 22455 22456 22457 22458 $def(self, '$absolute_path', function $$absolute_path(path, basedir) { 22459 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; 22460 22461 22462 if (basedir == null) basedir = nil; 22463 sep = $$('SEPARATOR'); 22464 sep_chars = $sep_chars(); 22465 new_parts = []; 22466 path = ($truthy(path['$respond_to?']("to_path")) ? (path.$to_path()) : (path)); 22467 path = $Opal['$coerce_to!'](path, $$$('String'), "to_str"); 22468 basedir = ($truthy(($ret_or_1 = basedir)) ? ($ret_or_1) : ($$$('Dir').$pwd())); 22469 path_abs = path.substr(0, sep.length) === sep || windows_root_rx.test(path); 22470 basedir_abs = basedir.substr(0, sep.length) === sep || windows_root_rx.test(basedir); 22471 if ($truthy(path_abs)) { 22472 22473 parts = path.$split($regexp(["[", sep_chars, "]"])); 22474 leading_sep = windows_root_rx.test(path) ? '' : path.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 22475 abs = true; 22476 } else { 22477 22478 parts = $rb_plus(basedir.$split($regexp(["[", sep_chars, "]"])), path.$split($regexp(["[", sep_chars, "]"]))); 22479 leading_sep = windows_root_rx.test(basedir) ? '' : basedir.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 22480 abs = basedir_abs; 22481 } 22482 var part; 22483 for (var i = 0, ii = parts.length; i < ii; i++) { 22484 part = parts[i]; 22485 22486 if ( 22487 (part === nil) || 22488 (part === '' && ((new_parts.length === 0) || abs)) || 22489 (part === '.' && ((new_parts.length === 0) || abs)) 22490 ) { 22491 continue; 22492 } 22493 if (part === '..') { 22494 new_parts.pop(); 22495 } else { 22496 new_parts.push(part); 22497 } 22498 } 22499 22500 if (!abs && parts[0] !== '.') { 22501 new_parts.$unshift("."); 22502 } 22503 new_path = new_parts.$join(sep); 22504 if ($truthy(abs)) { 22505 new_path = $rb_plus(leading_sep, new_path); 22506 } return new_path; 22507 }, -2); 22508 22509 $def(self, '$expand_path', function $$expand_path(path, basedir) { 22510 var self = this, sep = nil, sep_chars = nil, home = nil, leading_sep = nil, home_path_regexp = nil; 22511 22512 22513 if (basedir == null) basedir = nil; 22514 sep = $$('SEPARATOR'); 22515 sep_chars = $sep_chars(); 22516 if ($truthy(path[0] === '~' || (basedir && basedir[0] === '~'))) { 22517 22518 home = $$('Dir').$home(); 22519 if (!$truthy(home)) { 22520 $Kernel.$raise($$$('ArgumentError'), "couldn't find HOME environment -- expanding `~'"); 22521 } leading_sep = windows_root_rx.test(home) ? '' : home.$sub($regexp(["^([", sep_chars, "]+).*$"]), "\\1"); 22522 if (!$truthy(home['$start_with?'](leading_sep))) { 22523 $Kernel.$raise($$$('ArgumentError'), "non-absolute home"); 22524 } home = $rb_plus(home, sep); 22525 home_path_regexp = $regexp(["^\\~(?:", sep, "|$)"]); 22526 path = path.$sub(home_path_regexp, home); 22527 if ($truthy(basedir)) { 22528 basedir = basedir.$sub(home_path_regexp, home); 22529 } } return self.$absolute_path(path, basedir); 22530 }, -2); 22531 22532 // Coerce a given path to a path string using #to_path and #to_str 22533 function $coerce_to_path(path) { 22534 if ($truthy((path)['$respond_to?']("to_path"))) { 22535 path = path.$to_path(); 22536 } 22537 22538 path = $Opal['$coerce_to!'](path, $$$('String'), "to_str"); 22539 22540 return path; 22541 } 22542 22543 // Return a RegExp compatible char class 22544 function $sep_chars() { 22545 if ($$('ALT_SEPARATOR') === nil) { 22546 return Opal.escape_regexp($$('SEPARATOR')); 22547 } else { 22548 return Opal.escape_regexp($rb_plus($$('SEPARATOR'), $$('ALT_SEPARATOR'))); 22549 } 22550 } 22551 22552 $def(self, '$dirname', function $$dirname(path, level) { 22553 var self = this, sep_chars = nil; 22554 22555 22556 if (level == null) level = 1; 22557 if ($eqeq(level, 0)) { 22558 return path 22559 } if ($truthy($rb_lt(level, 0))) { 22560 $Kernel.$raise($$$('ArgumentError'), "level can't be negative"); 22561 } sep_chars = $sep_chars(); 22562 path = $coerce_to_path(path); 22563 22564 var absolute = path.match(new RegExp("^[" + (sep_chars) + "]")), out; 22565 22566 path = path.replace(new RegExp("[" + (sep_chars) + "]+$"), ''); // remove trailing separators 22567 path = path.replace(new RegExp("[^" + (sep_chars) + "]+$"), ''); // remove trailing basename 22568 path = path.replace(new RegExp("[" + (sep_chars) + "]+$"), ''); // remove final trailing separators 22569 22570 if (path === '') { 22571 out = absolute ? '/' : '.'; 22572 } 22573 else { 22574 out = path; 22575 } 22576 22577 if (level == 1) { 22578 return out; 22579 } 22580 else { 22581 return self.$dirname(out, $rb_minus(level, 1)) 22582 } 22583 }, -2); 22584 22585 $def(self, '$basename', function $$basename(name, suffix) { 22586 var sep_chars = nil; 22587 22588 22589 if (suffix == null) suffix = nil; 22590 sep_chars = $sep_chars(); 22591 name = $coerce_to_path(name); 22592 22593 if (name.length == 0) { 22594 return name; 22595 } 22596 22597 if (suffix !== nil) { 22598 suffix = $Opal['$coerce_to!'](suffix, $$$('String'), "to_str"); 22599 } else { 22600 suffix = null; 22601 } 22602 22603 name = name.replace(new RegExp("(.)[" + (sep_chars) + "]*$"), '$1'); 22604 name = name.replace(new RegExp("^(?:.*[" + (sep_chars) + "])?([^" + (sep_chars) + "]+)$"), '$1'); 22605 22606 if (suffix === ".*") { 22607 name = name.replace(/\.[^\.]+$/, ''); 22608 } else if(suffix !== null) { 22609 suffix = Opal.escape_regexp(suffix); 22610 name = name.replace(new RegExp("" + (suffix) + "$"), ''); 22611 } 22612 22613 return name; 22614 }, -2); 22615 22616 $def(self, '$extname', function $$extname(path) { 22617 var self = this, filename = nil, last_dot_idx = nil; 22618 22619 22620 path = $coerce_to_path(path); 22621 filename = self.$basename(path); 22622 if ($truthy(filename['$empty?']())) { 22623 return "" 22624 } last_dot_idx = filename['$[]']($range(1, -1, false)).$rindex("."); 22625 if (($truthy(last_dot_idx['$nil?']()) || ($eqeq($rb_plus(last_dot_idx, 1), $rb_minus(filename.$length(), 1))))) { 22626 return "" 22627 } else { 22628 return filename['$[]'](Opal.Range.$new($rb_plus(last_dot_idx, 1), -1, false)) 22629 } }); 22630 22631 $def(self, '$exist?', function $exist$ques$1(path) { 22632 22633 return Opal.modules[path] != null 22634 }); 22635 22636 $def(self, '$directory?', function $directory$ques$2(path) { 22637 var files = nil; 22638 22639 22640 files = []; 22641 22642 for (var key in Opal.modules) { 22643 files.push(key); 22644 } 22645 path = path.$gsub($regexp(["(^.", $$('SEPARATOR'), "+|", $$('SEPARATOR'), "+$)"])); 22646 return $send(files, 'find', [], function $$3(f){ 22647 22648 if (f == null) f = nil; 22649 return f['$=~']($regexp(["^", path]));}); 22650 }); 22651 22652 $def(self, '$join', function $$join($a) { 22653 var $post_args, paths, result = nil; 22654 22655 22656 $post_args = $slice(arguments); 22657 paths = $post_args; 22658 if ($truthy(paths['$empty?']())) { 22659 return "" 22660 } result = ""; 22661 paths = $send(paths.$flatten().$each_with_index(), 'map', [], function $$4(item, index){ 22662 22663 if (item == null) item = nil; 22664 if (index == null) index = nil; 22665 if (($eqeq(index, 0) && ($truthy(item['$empty?']())))) { 22666 return $$('SEPARATOR') 22667 } else if (($eqeq(paths.$length(), $rb_plus(index, 1)) && ($truthy(item['$empty?']())))) { 22668 return $$('SEPARATOR') 22669 } else { 22670 return item 22671 }}); 22672 paths = $send(paths, 'reject', [], "empty?".$to_proc()); 22673 $send(paths, 'each_with_index', [], function $$5(item, index){var next_item = nil; 22674 22675 22676 if (item == null) item = nil; 22677 if (index == null) index = nil; 22678 next_item = paths['$[]']($rb_plus(index, 1)); 22679 if ($truthy(next_item['$nil?']())) { 22680 return (result = "" + (result) + (item)) 22681 } else { 22682 22683 if (($truthy(item['$end_with?']($$('SEPARATOR'))) && ($truthy(next_item['$start_with?']($$('SEPARATOR')))))) { 22684 item = item.$sub($regexp([$$('SEPARATOR'), "+$"]), ""); 22685 } return (result = (($truthy(item['$end_with?']($$('SEPARATOR'))) || ($truthy(next_item['$start_with?']($$('SEPARATOR'))))) ? ("" + (result) + (item)) : ("" + (result) + (item) + ($$('SEPARATOR'))))); 22686 }}); 22687 return result; 22688 }, -1); 22689 22690 $def(self, '$split', function $$split(path) { 22691 22692 return path.$split($$('SEPARATOR')) 22693 }); 22694 $alias(self, "realpath", "expand_path"); 22695 return $alias(self, "exists?", "exist?"); 22696 })(Opal.get_singleton_class(self), $nesting); 22697 })('::', $$$('IO'), $nesting) 22698}; 22699 22700Opal.modules["corelib/process/base"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22701 var $klass = Opal.klass, $slice = Opal.slice, $defs = Opal.defs, $return_val = Opal.return_val, nil = Opal.nil; 22702 22703 22704 (function($base, $super) { 22705 var self = $klass($base, $super, 'Signal'); 22706 22707 22708 return $defs(self, '$trap', function $$trap($a) { 22709 22710 22711 $slice(arguments); 22712 return nil; 22713 }, -1) 22714 })('::', null); 22715 return (function($base, $super) { 22716 var self = $klass($base, $super, 'GC'); 22717 22718 22719 return $defs(self, '$start', $return_val(nil)) 22720 })('::', null); 22721}; 22722 22723Opal.modules["corelib/process"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22724 var $module = Opal.module, $defs = Opal.defs, $truthy = Opal.truthy, $return_val = Opal.return_val, $Kernel = Opal.Kernel, nil = Opal.nil, $$$ = Opal.$$$; 22725 22726 Opal.add_stubs('const_set,size,<<,__register_clock__,to_f,now,new,[],raise'); 22727 return (function($base) { 22728 var self = $module($base, 'Process'); 22729 22730 var monotonic = nil; 22731 22732 22733 self.__clocks__ = []; 22734 $defs(self, '$__register_clock__', function $$__register_clock__(name, func) { 22735 var self = this; 22736 if (self.__clocks__ == null) self.__clocks__ = nil; 22737 22738 22739 self.$const_set(name, self.__clocks__.$size()); 22740 return self.__clocks__['$<<'](func); 22741 }); 22742 self.$__register_clock__("CLOCK_REALTIME", function() { return Date.now() }); 22743 monotonic = false; 22744 22745 if (Opal.global.performance) { 22746 monotonic = function() { 22747 return performance.now() 22748 }; 22749 } 22750 else if (Opal.global.process && process.hrtime) { 22751 // let now be the base to get smaller numbers 22752 var hrtime_base = process.hrtime(); 22753 22754 monotonic = function() { 22755 var hrtime = process.hrtime(hrtime_base); 22756 var us = (hrtime[1] / 1000) | 0; // cut below microsecs; 22757 return ((hrtime[0] * 1000) + (us / 1000)); 22758 }; 22759 } 22760 if ($truthy(monotonic)) { 22761 self.$__register_clock__("CLOCK_MONOTONIC", monotonic); 22762 } $defs(self, '$pid', $return_val(0)); 22763 $defs(self, '$times', function $$times() { 22764 var t = nil; 22765 22766 22767 t = $$$('Time').$now().$to_f(); 22768 return $$$($$$('Benchmark'), 'Tms').$new(t, t, t, t, t); 22769 }); 22770 return $defs(self, '$clock_gettime', function $$clock_gettime(clock_id, unit) { 22771 var self = this, clock = nil; 22772 if (self.__clocks__ == null) self.__clocks__ = nil; 22773 22774 22775 if (unit == null) unit = "float_second"; 22776 if ($truthy(((clock = self.__clocks__['$[]'](clock_id))))) ; else { 22777 $Kernel.$raise($$$($$$('Errno'), 'EINVAL'), "clock_gettime(" + (clock_id) + ") " + (self.__clocks__['$[]'](clock_id))); 22778 } 22779 var ms = clock(); 22780 switch (unit) { 22781 case 'float_second': return (ms / 1000); // number of seconds as a float (default) 22782 case 'float_millisecond': return (ms / 1); // number of milliseconds as a float 22783 case 'float_microsecond': return (ms * 1000); // number of microseconds as a float 22784 case 'second': return ((ms / 1000) | 0); // number of seconds as an integer 22785 case 'millisecond': return ((ms / 1) | 0); // number of milliseconds as an integer 22786 case 'microsecond': return ((ms * 1000) | 0); // number of microseconds as an integer 22787 case 'nanosecond': return ((ms * 1000000) | 0); // number of nanoseconds as an integer 22788 default: $Kernel.$raise($$$('ArgumentError'), "unexpected unit: " + (unit)); 22789 } 22790 }, -2); 22791 })('::') 22792}; 22793 22794Opal.modules["corelib/random/formatter"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22795 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.$$$; 22796 22797 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'); 22798 return (function($base, $super, $parent_nesting) { 22799 var self = $klass($base, $super, 'Random'); 22800 22801 var $nesting = [self].concat($parent_nesting); 22802 22803 22804 (function($base, $parent_nesting) { 22805 var self = $module($base, 'Formatter'); 22806 22807 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 22808 22809 22810 22811 $def(self, '$hex', function $$hex(count) { 22812 var self = this; 22813 22814 22815 if (count == null) count = nil; 22816 count = $$$('Random').$_verify_count(count); 22817 22818 var bytes = self.$bytes(count); 22819 var out = ""; 22820 for (var i = 0; i < count; i++) { 22821 out += bytes.charCodeAt(i).toString(16).padStart(2, '0'); 22822 } 22823 return (out).$encode("US-ASCII"); 22824 }, -1); 22825 22826 $def(self, '$random_bytes', function $$random_bytes(count) { 22827 var self = this; 22828 22829 22830 if (count == null) count = nil; 22831 return self.$bytes(count); 22832 }, -1); 22833 22834 $def(self, '$base64', function $$base64(count) { 22835 var self = this; 22836 22837 22838 if (count == null) count = nil; 22839 return $$$('Base64').$strict_encode64(self.$random_bytes(count)).$encode("US-ASCII"); 22840 }, -1); 22841 22842 $def(self, '$urlsafe_base64', function $$urlsafe_base64(count, padding) { 22843 var self = this; 22844 22845 22846 if (count == null) count = nil; 22847 if (padding == null) padding = false; 22848 return $$$('Base64').$urlsafe_encode64(self.$random_bytes(count), padding).$encode("US-ASCII"); 22849 }, -1); 22850 22851 $def(self, '$uuid', function $$uuid() { 22852 var self = this, str = nil; 22853 22854 22855 str = self.$hex(16).$split(""); 22856 str['$[]='](12, "4"); 22857 str['$[]='](16, (parseInt(str['$[]'](16), 16) & 3 | 8).toString(16)); 22858 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))]; 22859 str = $send(str, 'map', [], "join".$to_proc()); 22860 return str.$join("-"); 22861 }); 22862 22863 $def(self, '$random_float', function $$random_float() { 22864 var self = this, bs = nil, num = nil; 22865 22866 22867 bs = self.$bytes(4); 22868 num = 0; 22869 $send((4), 'times', [], function $$1(i){ 22870 22871 if (i == null) i = nil; 22872 num = num['$<<'](8); 22873 return (num = num['$|'](bs['$[]'](i).$ord()));}); 22874 return $rb_divide(num.$abs(), 2147483647); 22875 }); 22876 22877 $def(self, '$random_number', function $$random_number(limit) { 22878 var self = this; 22879 22880 function randomFloat() { 22881 return self.$random_float(); 22882 } 22883 22884 function randomInt(max) { 22885 return Math.floor(randomFloat() * max); 22886 } 22887 22888 function randomRange() { 22889 var min = limit.begin, 22890 max = limit.end; 22891 22892 if (min === nil || max === nil) { 22893 return nil; 22894 } 22895 22896 var length = max - min; 22897 22898 if (length < 0) { 22899 return nil; 22900 } 22901 22902 if (length === 0) { 22903 return min; 22904 } 22905 22906 if (max % 1 === 0 && min % 1 === 0 && !limit.excl) { 22907 length++; 22908 } 22909 22910 return randomInt(length) + min; 22911 } 22912 22913 if (limit == null) { 22914 return randomFloat(); 22915 } else if (limit.$$is_range) { 22916 return randomRange(); 22917 } else if (limit.$$is_number) { 22918 if (limit <= 0) { 22919 $Kernel.$raise($$$('ArgumentError'), "invalid argument - " + (limit)); 22920 } 22921 22922 if (limit % 1 === 0) { 22923 // integer 22924 return randomInt(limit); 22925 } else { 22926 return randomFloat() * limit; 22927 } 22928 } else { 22929 limit = $Opal['$coerce_to!'](limit, $$$('Integer'), "to_int"); 22930 22931 if (limit <= 0) { 22932 $Kernel.$raise($$$('ArgumentError'), "invalid argument - " + (limit)); 22933 } 22934 22935 return randomInt(limit); 22936 } 22937 }, -1); 22938 return $def(self, '$alphanumeric', function $$alphanumeric(count) { 22939 var self = this, map = nil; 22940 22941 22942 if (count == null) count = nil; 22943 count = $$('Random').$_verify_count(count); 22944 map = $send([$range("0", "9", false), $range("a", "z", false), $range("A", "Z", false)], 'map', [], "to_a".$to_proc()).$flatten(); 22945 return $send($$$('Array'), 'new', [count], function $$2(i){var self = $$2.$$s == null ? this : $$2.$$s; 22946 return map['$[]'](self.$random_number(map.$length()));}, {$$s: self}).$join(); 22947 }, -1); 22948 })(self, $nesting); 22949 self.$include($$$($$$('Random'), 'Formatter')); 22950 return self.$extend($$$($$$('Random'), 'Formatter')); 22951 })('::', null, $nesting) 22952}; 22953 22954Opal.modules["corelib/random/mersenne_twister"] = function(Opal) {/* Generated by Opal 1.7.3 */ 22955 var $klass = Opal.klass, $const_set = Opal.const_set, $send = Opal.send, nil = Opal.nil, $$$ = Opal.$$$, mersenne_twister = nil; 22956 22957 Opal.add_stubs('generator='); 22958 22959 mersenne_twister = (function() { 22960 /* Period parameters */ 22961 var N = 624; 22962 var M = 397; 22963 var MATRIX_A = 0x9908b0df; /* constant vector a */ 22964 var UMASK = 0x80000000; /* most significant w-r bits */ 22965 var LMASK = 0x7fffffff; /* least significant r bits */ 22966 var MIXBITS = function(u,v) { return ( ((u) & UMASK) | ((v) & LMASK) ); }; 22967 var TWIST = function(u,v) { return (MIXBITS((u),(v)) >>> 1) ^ ((v & 0x1) ? MATRIX_A : 0x0); }; 22968 22969 function init(s) { 22970 var mt = {left: 0, next: N, state: new Array(N)}; 22971 init_genrand(mt, s); 22972 return mt; 22973 } 22974 22975 /* initializes mt[N] with a seed */ 22976 function init_genrand(mt, s) { 22977 var j; 22978 mt.state[0] = s >>> 0; 22979 for (j=1; j<N; j++) { 22980 mt.state[j] = (1812433253 * ((mt.state[j-1] ^ (mt.state[j-1] >> 30) >>> 0)) + j); 22981 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */ 22982 /* In the previous versions, MSBs of the seed affect */ 22983 /* only MSBs of the array state[]. */ 22984 /* 2002/01/09 modified by Makoto Matsumoto */ 22985 mt.state[j] &= 0xffffffff; /* for >32 bit machines */ 22986 } 22987 mt.left = 1; 22988 mt.next = N; 22989 } 22990 22991 /* generate N words at one time */ 22992 function next_state(mt) { 22993 var p = 0, _p = mt.state; 22994 var j; 22995 22996 mt.left = N; 22997 mt.next = 0; 22998 22999 for (j=N-M+1; --j; p++) 23000 _p[p] = _p[p+(M)] ^ TWIST(_p[p+(0)], _p[p+(1)]); 23001 23002 for (j=M; --j; p++) 23003 _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[p+(1)]); 23004 23005 _p[p] = _p[p+(M-N)] ^ TWIST(_p[p+(0)], _p[0]); 23006 } 23007 23008 /* generates a random number on [0,0xffffffff]-interval */ 23009 function genrand_int32(mt) { 23010 /* mt must be initialized */ 23011 var y; 23012 23013 if (--mt.left <= 0) next_state(mt); 23014 y = mt.state[mt.next++]; 23015 23016 /* Tempering */ 23017 y ^= (y >>> 11); 23018 y ^= (y << 7) & 0x9d2c5680; 23019 y ^= (y << 15) & 0xefc60000; 23020 y ^= (y >>> 18); 23021 23022 return y >>> 0; 23023 } 23024 23025 function int_pair_to_real_exclusive(a, b) { 23026 a >>>= 5; 23027 b >>>= 6; 23028 return (a*67108864.0+b)*(1.0/9007199254740992.0); 23029 } 23030 23031 // generates a random number on [0,1) with 53-bit resolution 23032 function genrand_real(mt) { 23033 /* mt must be initialized */ 23034 var a = genrand_int32(mt), b = genrand_int32(mt); 23035 return int_pair_to_real_exclusive(a, b); 23036 } 23037 23038 return { genrand_real: genrand_real, init: init }; 23039})(); 23040 return (function($base, $super) { 23041 var self = $klass($base, $super, 'Random'); 23042 23043 var $a; 23044 23045 23046 var MAX_INT = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; 23047 $const_set(self, 'MERSENNE_TWISTER_GENERATOR', { 23048 new_seed: function() { return Math.round(Math.random() * MAX_INT); }, 23049 reseed: function(seed) { return mersenne_twister.init(seed); }, 23050 rand: function(mt) { return mersenne_twister.genrand_real(mt); } 23051 }); 23052 return ($a = [$$$(self, 'MERSENNE_TWISTER_GENERATOR')], $send(self, 'generator=', $a), $a[$a.length - 1]); 23053 })('::', null); 23054}; 23055 23056Opal.modules["corelib/random"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23057 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.$$$; 23058 23059 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'); 23060 23061 self.$require("corelib/random/formatter"); 23062 (function($base, $super) { 23063 var self = $klass($base, $super, 'Random'); 23064 23065 23066 23067 self.$attr_reader("seed", "state"); 23068 $defs(self, '$_verify_count', function $$_verify_count(count) { 23069 23070 23071 if (!$truthy(count)) count = 16; 23072 if (typeof count !== "number") count = (count).$to_int(); 23073 if (count < 0) $Kernel.$raise($$$('ArgumentError'), "negative string size (or size too big)"); 23074 count = Math.floor(count); 23075 return count; 23076 23077 }); 23078 23079 $def(self, '$initialize', function $$initialize(seed) { 23080 var self = this; 23081 23082 23083 if (seed == null) seed = $$$('Random').$new_seed(); 23084 seed = $Opal['$coerce_to!'](seed, $$$('Integer'), "to_int"); 23085 self.state = seed; 23086 return self.$reseed(seed); 23087 }, -1); 23088 23089 $def(self, '$reseed', function $$reseed(seed) { 23090 var self = this; 23091 23092 23093 self.seed = seed; 23094 return self.$rng = Opal.$$rand.reseed(seed); }); 23095 $defs(self, '$new_seed', function $$new_seed() { 23096 23097 return Opal.$$rand.new_seed(); 23098 }); 23099 $defs(self, '$rand', function $$rand(limit) { 23100 var self = this; 23101 return $$$(self, 'DEFAULT').$rand(limit); 23102 }, -1); 23103 $defs(self, '$srand', function $$srand(n) { 23104 var self = this, previous_seed = nil; 23105 23106 23107 if (n == null) n = $$$('Random').$new_seed(); 23108 n = $Opal['$coerce_to!'](n, $$$('Integer'), "to_int"); 23109 previous_seed = $$$(self, 'DEFAULT').$seed(); 23110 $$$(self, 'DEFAULT').$reseed(n); 23111 return previous_seed; 23112 }, -1); 23113 $defs(self, '$urandom', function $$urandom(size) { 23114 23115 return $$$('SecureRandom').$bytes(size) 23116 }); 23117 23118 $def(self, '$==', function $Random_$eq_eq$1(other) { 23119 var self = this, $ret_or_1 = nil; 23120 23121 23122 if (!$eqeqeq($$$('Random'), other)) { 23123 return false 23124 } if ($truthy(($ret_or_1 = self.$seed()['$=='](other.$seed())))) { 23125 return self.$state()['$=='](other.$state()) 23126 } else { 23127 return $ret_or_1 23128 } }); 23129 23130 $def(self, '$bytes', function $$bytes(length) { 23131 var self = this; 23132 23133 23134 length = $$$('Random').$_verify_count(length); 23135 return $send($$$('Array'), 'new', [length], function $$2(){var self = $$2.$$s == null ? this : $$2.$$s; 23136 23137 return self.$rand(255).$chr()}, {$$s: self}).$join().$encode("ASCII-8BIT"); 23138 }); 23139 $defs(self, '$bytes', function $$bytes(length) { 23140 var self = this; 23141 23142 return $$$(self, 'DEFAULT').$bytes(length) 23143 }); 23144 23145 $def(self, '$rand', function $$rand(limit) { 23146 var self = this; 23147 return self.$random_number(limit); 23148 }, -1); 23149 23150 $def(self, '$random_float', function $$random_float() { 23151 var self = this; 23152 23153 23154 self.state++; 23155 return Opal.$$rand.rand(self.$rng); 23156 23157 }); 23158 $defs(self, '$random_float', function $$random_float() { 23159 var self = this; 23160 23161 return $$$(self, 'DEFAULT').$random_float() 23162 }); 23163 return $defs(self, '$generator=', function $Random_generator$eq$3(generator) { 23164 var self = this; 23165 23166 23167 Opal.$$rand = generator; 23168 if ($truthy(self['$const_defined?']("DEFAULT"))) { 23169 return $$$(self, 'DEFAULT').$reseed() 23170 } else { 23171 return self.$const_set("DEFAULT", self.$new(self.$new_seed())) 23172 } }); 23173 })('::', null); 23174 return self.$require("corelib/random/mersenne_twister"); 23175}; 23176 23177Opal.modules["corelib/unsupported"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23178 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.$$$; 23179 23180 Opal.add_stubs('raise,warn,each,define_method,%,public,private_method_defined?,private_class_method,instance_method,instance_methods,method_defined?,private_methods'); 23181 23182 23183 var warnings = {}; 23184 23185 function handle_unsupported_feature(message) { 23186 switch (Opal.config.unsupported_features_severity) { 23187 case 'error': 23188 $Kernel.$raise($$$('NotImplementedError'), message); 23189 break; 23190 case 'warning': 23191 warn(message); 23192 break; 23193 // noop 23194 } 23195 } 23196 23197 function warn(string) { 23198 if (warnings[string]) { 23199 return; 23200 } 23201 23202 warnings[string] = true; 23203 self.$warn(string); 23204 } 23205 (function($base, $super) { 23206 var self = $klass($base, $super, 'String'); 23207 23208 23209 23210 var ERROR = "String#%s not supported. Mutable String methods are not supported in Opal."; 23211 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; 23212 23213 23214 if (method_name == null) method_name = nil; 23215 return $send(self, 'define_method', [method_name], function $$2($a){ 23216 23217 $slice(arguments); 23218 return $Kernel.$raise($$$('NotImplementedError'), (ERROR)['$%'](method_name));}, -1);}, {$$s: self}); 23219 })('::', null); 23220 (function($base) { 23221 var self = $module($base, 'Kernel'); 23222 23223 23224 23225 var ERROR = "Object tainting is not supported by Opal"; 23226 23227 $def(self, '$taint', function $$taint() { 23228 var self = this; 23229 23230 23231 handle_unsupported_feature(ERROR); 23232 return self; 23233 }); 23234 23235 $def(self, '$untaint', function $$untaint() { 23236 var self = this; 23237 23238 23239 handle_unsupported_feature(ERROR); 23240 return self; 23241 }); 23242 return $def(self, '$tainted?', function $Kernel_tainted$ques$3() { 23243 23244 23245 handle_unsupported_feature(ERROR); 23246 return false; 23247 }); 23248 })('::'); 23249 (function($base, $super) { 23250 var self = $klass($base, $super, 'Module'); 23251 23252 23253 23254 23255 $def(self, '$public', function $Module_public$4($a) { 23256 var $post_args, methods, self = this; 23257 23258 23259 $post_args = $slice(arguments); 23260 methods = $post_args; 23261 23262 if (methods.length === 0) { 23263 self.$$module_function = false; 23264 return nil; 23265 } 23266 return (methods.length === 1) ? methods[0] : methods; 23267 }, -1); 23268 23269 $def(self, '$private_class_method', function $$private_class_method($a) { 23270 var $post_args, methods; 23271 23272 23273 $post_args = $slice(arguments); 23274 methods = $post_args; 23275 return (methods.length === 1) ? methods[0] : methods; }, -1); 23276 23277 $def(self, '$private_method_defined?', $return_val(false)); 23278 23279 $def(self, '$private_constant', function $$private_constant($a) { 23280 23281 23282 $slice(arguments); 23283 return nil; 23284 }, -1); 23285 $alias(self, "nesting", "public"); 23286 $alias(self, "private", "public"); 23287 $alias(self, "protected", "public"); 23288 $alias(self, "protected_method_defined?", "private_method_defined?"); 23289 $alias(self, "public_class_method", "private_class_method"); 23290 $alias(self, "public_instance_method", "instance_method"); 23291 $alias(self, "public_instance_methods", "instance_methods"); 23292 return $alias(self, "public_method_defined?", "method_defined?"); 23293 })('::', null); 23294 (function($base) { 23295 var self = $module($base, 'Kernel'); 23296 23297 23298 23299 23300 $def(self, '$private_methods', function $$private_methods($a) { 23301 23302 23303 $slice(arguments); 23304 return []; 23305 }, -1); 23306 $alias(self, "protected_methods", "private_methods"); 23307 $alias(self, "private_instance_methods", "private_methods"); 23308 return $alias(self, "protected_instance_methods", "private_methods"); 23309 })('::'); 23310 (function($base, $parent_nesting) { 23311 var self = $module($base, 'Kernel'); 23312 23313 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 23314 23315 return $def(self, '$eval', function $Kernel_eval$5($a) { 23316 23317 23318 $slice(arguments); 23319 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.")); 23320 }, -1) 23321 })('::', $nesting); 23322 $defs(self, '$public', function $public$6($a) { 23323 var $post_args, methods; 23324 23325 23326 $post_args = $slice(arguments); 23327 methods = $post_args; 23328 return (methods.length === 1) ? methods[0] : methods; }, -1); 23329 return $defs(self, '$private', function $private$7($a) { 23330 var $post_args, methods; 23331 23332 23333 $post_args = $slice(arguments); 23334 methods = $post_args; 23335 return (methods.length === 1) ? methods[0] : methods; }, -1); 23336}; 23337 23338Opal.queue(function(Opal) {/* Generated by Opal 1.7.3 */ 23339 var $Object = Opal.Object; Opal.nil; 23340 23341 Opal.add_stubs('require,autoload'); 23342 23343 $Object.$require("opal/base"); 23344 $Object.$require("opal/mini"); 23345 $Object.$require("corelib/kernel/format"); 23346 $Object.$require("corelib/string/encoding"); 23347 $Object.$autoload("Math", "corelib/math"); 23348 $Object.$require("corelib/complex/base"); 23349 $Object.$autoload("Complex", "corelib/complex"); 23350 $Object.$require("corelib/rational/base"); 23351 $Object.$autoload("Rational", "corelib/rational"); 23352 $Object.$require("corelib/time"); 23353 $Object.$autoload("Struct", "corelib/struct"); 23354 $Object.$autoload("Set", "corelib/set"); 23355 $Object.$autoload("Dir", "corelib/dir"); 23356 $Object.$autoload("File", "corelib/file"); 23357 $Object.$require("corelib/process/base"); 23358 $Object.$autoload("Process", "corelib/process"); 23359 $Object.$autoload("Random", "corelib/random"); 23360 return $Object.$require("corelib/unsupported"); 23361}); 23362 23363 23364var Opal$1 = Opal; 23365 23366Opal.modules["corelib/comparable"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23367 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.$$$; 23368 23369 Opal.add_stubs('>,<,===,raise,class,<=>,equal?'); 23370 return (function($base) { 23371 var self = $module($base, 'Comparable'); 23372 23373 var $ret_or_1 = nil; 23374 23375 23376 23377 function normalize(what) { 23378 if (Opal.is_a(what, Opal.Integer)) { return what; } 23379 23380 if ($rb_gt(what, 0)) { return 1; } 23381 if ($rb_lt(what, 0)) { return -1; } 23382 return 0; 23383 } 23384 23385 function fail_comparison(lhs, rhs) { 23386 var class_name; 23387 (($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)); 23388 $Kernel.$raise($$$('ArgumentError'), "comparison of " + ((lhs).$class()) + " with " + (class_name) + " failed"); 23389 } 23390 23391 function cmp_or_fail(lhs, rhs) { 23392 var cmp = (lhs)['$<=>'](rhs); 23393 if (!$truthy(cmp)) fail_comparison(lhs, rhs); 23394 return normalize(cmp); 23395 } 23396 23397 $def(self, '$==', function $Comparable_$eq_eq$1(other) { 23398 var self = this, cmp = nil; 23399 23400 23401 if ($truthy(self['$equal?'](other))) { 23402 return true 23403 } 23404 if (self["$<=>"] == Opal.Kernel["$<=>"]) { 23405 return false; 23406 } 23407 23408 // check for infinite recursion 23409 if (self.$$comparable) { 23410 self.$$comparable = false; 23411 return false; 23412 } 23413 if (!$truthy((cmp = self['$<=>'](other)))) { 23414 return false 23415 } return normalize(cmp) == 0; }); 23416 23417 $def(self, '$>', function $Comparable_$gt$2(other) { 23418 var self = this; 23419 23420 return cmp_or_fail(self, other) > 0; 23421 }); 23422 23423 $def(self, '$>=', function $Comparable_$gt_eq$3(other) { 23424 var self = this; 23425 23426 return cmp_or_fail(self, other) >= 0; 23427 }); 23428 23429 $def(self, '$<', function $Comparable_$lt$4(other) { 23430 var self = this; 23431 23432 return cmp_or_fail(self, other) < 0; 23433 }); 23434 23435 $def(self, '$<=', function $Comparable_$lt_eq$5(other) { 23436 var self = this; 23437 23438 return cmp_or_fail(self, other) <= 0; 23439 }); 23440 23441 $def(self, '$between?', function $Comparable_between$ques$6(min, max) { 23442 var self = this; 23443 23444 23445 if ($rb_lt(self, min)) { 23446 return false 23447 } if ($rb_gt(self, max)) { 23448 return false 23449 } return true; 23450 }); 23451 return $def(self, '$clamp', function $$clamp(min, max) { 23452 var self = this; 23453 23454 23455 if (max == null) max = nil; 23456 23457 var c, excl; 23458 23459 if (max === nil) { 23460 // We are dealing with a new Ruby 2.7 behaviour that we are able to 23461 // provide a single Range argument instead of 2 Comparables. 23462 23463 if (!Opal.is_a(min, Opal.Range)) { 23464 $Kernel.$raise($$$('TypeError'), "wrong argument type " + (min.$class()) + " (expected Range)"); 23465 } 23466 23467 excl = min.excl; 23468 max = min.end; 23469 min = min.begin; 23470 23471 if (max !== nil && excl) { 23472 $Kernel.$raise($$$('ArgumentError'), "cannot clamp with an exclusive range"); 23473 } 23474 } 23475 23476 if (min !== nil && max !== nil && cmp_or_fail(min, max) > 0) { 23477 $Kernel.$raise($$$('ArgumentError'), "min argument must be smaller than max argument"); 23478 } 23479 23480 if (min !== nil) { 23481 c = cmp_or_fail(self, min); 23482 23483 if (c == 0) return self; 23484 if (c < 0) return min; 23485 } 23486 23487 if (max !== nil) { 23488 c = cmp_or_fail(self, max); 23489 23490 if (c > 0) return max; 23491 } 23492 23493 return self; 23494 }, -2); 23495 })('::') 23496}; 23497 23498Opal.modules["pathname"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23499 var $klass = Opal.klass, $const_set = Opal.const_set, $regexp = Opal.regexp, $eqeqeq = Opal.eqeqeq, $truthy = Opal.truthy, $eqeq = Opal.eqeq, $def = Opal.def, $defs = Opal.defs, $to_ary = Opal.to_ary, $slice = Opal.slice, $send = Opal.send, $to_a = Opal.to_a, $return_ivar = Opal.return_ivar, $neqeq = Opal.neqeq, $rb_plus = Opal.rb_plus, $not = Opal.not, $thrower = Opal.thrower, $alias = Opal.alias, $module = Opal.module, self = Opal.top, $nesting = [], nil = Opal.nil, $$$ = Opal.$$$; 23500 23501 Opal.add_stubs('require,include,quote,===,to_s,path,respond_to?,to_path,is_a?,nil?,raise,class,==,new,pwd,attr_reader,!,relative?,chop_basename,basename,=~,source,[],rindex,sub,absolute?,expand_path,plus,unshift,length,!=,empty?,first,shift,+,join,dirname,pop,reverse_each,directory?,extname,<=>,nonzero?,proc,casecmp,cleanpath,inspect,include?,fill,map,entries'); 23502 23503 self.$require("corelib/comparable"); 23504 (function($base, $super, $parent_nesting) { 23505 var self = $klass($base, $super, 'Pathname'); 23506 23507 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 23508 23509 $proto.path = nil; 23510 23511 self.$include($$('Comparable')); 23512 $const_set($nesting[0], 'SEPARATOR_PAT', $regexp([$$('Regexp').$quote($$$($$('File'), 'SEPARATOR'))])); 23513 23514 $def(self, '$initialize', function $$initialize(path) { 23515 var self = this; 23516 23517 23518 if ($eqeqeq($$('Pathname'), path)) { 23519 self.path = path.$path().$to_s(); 23520 } else if ($truthy(path['$respond_to?']("to_path"))) { 23521 self.path = path.$to_path(); 23522 } else if ($truthy(path['$is_a?']($$('String')))) { 23523 self.path = path; 23524 } else if ($truthy(path['$nil?']())) { 23525 self.$raise($$('TypeError'), "no implicit conversion of nil into String"); 23526 } else { 23527 self.$raise($$('TypeError'), "no implicit conversion of " + (path.$class()) + " into String"); 23528 } if ($eqeq(self.path, "\u0000")) { 23529 return self.$raise($$('ArgumentError')) 23530 } else { 23531 return nil 23532 } }); 23533 $defs(self, '$pwd', function $$pwd() { 23534 var self = this; 23535 23536 return self.$new($$('Dir').$pwd()) 23537 }); 23538 self.$attr_reader("path"); 23539 23540 $def(self, '$==', function $Pathname_$eq_eq$1(other) { 23541 var self = this; 23542 23543 return other.$path()['$=='](self.path) 23544 }); 23545 23546 $def(self, '$absolute?', function $Pathname_absolute$ques$2() { 23547 var self = this; 23548 23549 return self['$relative?']()['$!']() 23550 }); 23551 23552 $def(self, '$relative?', function $Pathname_relative$ques$3() { 23553 var $a, $b, self = this, path = nil, r = nil; 23554 23555 23556 path = self.path; 23557 while ($truthy((r = self.$chop_basename(path)))) { 23558 $b = r, $a = $to_ary($b), (path = ($a[0] == null ? nil : $a[0])); 23559 } return path['$=='](""); 23560 }); 23561 23562 $def(self, '$chop_basename', function $$chop_basename(path) { 23563 var base = nil; 23564 23565 23566 base = $$('File').$basename(path); 23567 if ($truthy($$('Regexp').$new("^" + ($$$($$('Pathname'), 'SEPARATOR_PAT').$source()) + "?$")['$=~'](base))) { 23568 return nil 23569 } else { 23570 return [path['$[]'](0, path.$rindex(base)), base] 23571 } }); 23572 23573 $def(self, '$root?', function $Pathname_root$ques$4() { 23574 var self = this; 23575 23576 return self.path['$==']("/") 23577 }); 23578 23579 $def(self, '$parent', function $$parent() { 23580 var self = this, new_path = nil; 23581 23582 23583 new_path = self.path.$sub(/\/([^\/]+\/?$)/, ""); 23584 if ($eqeq(new_path, "")) { 23585 new_path = ($truthy(self['$absolute?']()) ? ("/") : (".")); 23586 } return $$('Pathname').$new(new_path); 23587 }); 23588 23589 $def(self, '$sub', function $$sub($a) { 23590 var $post_args, args, self = this; 23591 23592 23593 $post_args = $slice(arguments); 23594 args = $post_args; 23595 return $$('Pathname').$new($send(self.path, 'sub', $to_a(args))); 23596 }, -1); 23597 23598 $def(self, '$cleanpath', function $$cleanpath() { 23599 var self = this; 23600 23601 return Opal.normalize(self.path) 23602 }); 23603 23604 $def(self, '$to_path', $return_ivar("path")); 23605 23606 $def(self, '$hash', $return_ivar("path")); 23607 23608 $def(self, '$expand_path', function $$expand_path() { 23609 var self = this; 23610 23611 return $$('Pathname').$new($$('File').$expand_path(self.path)) 23612 }); 23613 23614 $def(self, '$+', function $Pathname_$plus$5(other) { 23615 var self = this; 23616 23617 23618 if (!$eqeqeq($$('Pathname'), other)) { 23619 other = $$('Pathname').$new(other); 23620 } return $$('Pathname').$new(self.$plus(self.path, other.$to_s())); 23621 }); 23622 23623 $def(self, '$plus', function $$plus(path1, path2) { 23624 var $a, $b, self = this, prefix2 = nil, index_list2 = nil, basename_list2 = nil, r2 = nil, basename2 = nil, prefix1 = nil, $ret_or_1 = nil, r1 = nil, basename1 = nil, suffix2 = nil; 23625 23626 23627 prefix2 = path2; 23628 index_list2 = []; 23629 basename_list2 = []; 23630 while ($truthy((r2 = self.$chop_basename(prefix2)))) { 23631 23632 $b = r2, $a = $to_ary($b), (prefix2 = ($a[0] == null ? nil : $a[0])), (basename2 = ($a[1] == null ? nil : $a[1])); 23633 index_list2.$unshift(prefix2.$length()); 23634 basename_list2.$unshift(basename2); 23635 } if ($neqeq(prefix2, "")) { 23636 return path2 23637 } prefix1 = path1; 23638 while ($truthy(true)) { 23639 23640 while ($truthy(($truthy(($ret_or_1 = basename_list2['$empty?']()['$!']())) ? (basename_list2.$first()['$=='](".")) : ($ret_or_1)))) { 23641 23642 index_list2.$shift(); 23643 basename_list2.$shift(); 23644 } if (!$truthy((r1 = self.$chop_basename(prefix1)))) { 23645 break 23646 } $b = r1, $a = $to_ary($b), (prefix1 = ($a[0] == null ? nil : $a[0])), (basename1 = ($a[1] == null ? nil : $a[1])); 23647 if ($eqeq(basename1, ".")) { 23648 continue 23649 } if ((($eqeq(basename1, "..") || ($truthy(basename_list2['$empty?']()))) || ($neqeq(basename_list2.$first(), "..")))) { 23650 23651 prefix1 = $rb_plus(prefix1, basename1); 23652 break; 23653 } index_list2.$shift(); 23654 basename_list2.$shift(); 23655 } r1 = self.$chop_basename(prefix1); 23656 if (($not(r1) && ($truthy($regexp([$$('SEPARATOR_PAT')])['$=~']($$('File').$basename(prefix1)))))) { 23657 while ($truthy(($truthy(($ret_or_1 = basename_list2['$empty?']()['$!']())) ? (basename_list2.$first()['$==']("..")) : ($ret_or_1)))) { 23658 23659 index_list2.$shift(); 23660 basename_list2.$shift(); 23661 } 23662 } if ($not(basename_list2['$empty?']())) { 23663 23664 suffix2 = path2['$[]'](Opal.Range.$new(index_list2.$first(), -1, false)); 23665 if ($truthy(r1)) { 23666 return $$('File').$join(prefix1, suffix2) 23667 } else { 23668 return $rb_plus(prefix1, suffix2) 23669 } } else if ($truthy(r1)) { 23670 return prefix1 23671 } else { 23672 return $$('File').$dirname(prefix1) 23673 } }); 23674 23675 $def(self, '$join', function $$join($a) {try { var $t_return = $thrower('return'); 23676 var $post_args, args, self = this, result = nil; 23677 23678 23679 $post_args = $slice(arguments); 23680 args = $post_args; 23681 if ($truthy(args['$empty?']())) { 23682 return self 23683 }; 23684 result = args.$pop(); 23685 if (!$eqeqeq($$('Pathname'), result)) { 23686 result = $$('Pathname').$new(result); 23687 }; 23688 if ($truthy(result['$absolute?']())) { 23689 return result 23690 }; 23691 $send(args, 'reverse_each', [], function $$6(arg){ 23692 23693 if (arg == null) arg = nil; 23694 if (!$eqeqeq($$('Pathname'), arg)) { 23695 arg = $$('Pathname').$new(arg); 23696 }; 23697 result = $rb_plus(arg, result); 23698 if ($truthy(result['$absolute?']())) { 23699 $t_return.$throw(result); 23700 } else { 23701 return nil 23702 };}, {$$ret: $t_return}); 23703 return $rb_plus(self, result);} catch($e) { 23704 if ($e === $t_return) return $e.$v; 23705 throw $e; 23706 } 23707 }, -1); 23708 23709 $def(self, '$split', function $$split() { 23710 var self = this; 23711 23712 return [self.$dirname(), self.$basename()] 23713 }); 23714 23715 $def(self, '$dirname', function $$dirname() { 23716 var self = this; 23717 23718 return $$('Pathname').$new($$('File').$dirname(self.path)) 23719 }); 23720 23721 $def(self, '$basename', function $$basename() { 23722 var self = this; 23723 23724 return $$('Pathname').$new($$('File').$basename(self.path)) 23725 }); 23726 23727 $def(self, '$directory?', function $Pathname_directory$ques$7() { 23728 var self = this; 23729 23730 return $$('File')['$directory?'](self.path) 23731 }); 23732 23733 $def(self, '$extname', function $$extname() { 23734 var self = this; 23735 23736 return $$('File').$extname(self.path) 23737 }); 23738 23739 $def(self, '$<=>', function $Pathname_$lt_eq_gt$8(other) { 23740 var self = this; 23741 23742 return self.$path()['$<=>'](other.$path()) 23743 }); 23744 $const_set($nesting[0], 'SAME_PATHS', ($truthy($$$($$('File'), 'FNM_SYSCASE')['$nonzero?']()) ? ($send(self, 'proc', [], function $Pathname$9(a, b){ 23745 23746 if (a == null) a = nil; 23747 if (b == null) b = nil; 23748 return a.$casecmp(b)['$=='](0);})) : ($send(self, 'proc', [], function $Pathname$10(a, b){ 23749 23750 if (a == null) a = nil; 23751 if (b == null) b = nil; 23752 return a['$=='](b);})))); 23753 23754 $def(self, '$relative_path_from', function $$relative_path_from(base_directory) { 23755 var $a, $b, self = this, dest_directory = nil, dest_prefix = nil, dest_names = nil, r = nil, basename = nil, base_prefix = nil, base_names = nil, $ret_or_1 = nil, $ret_or_2 = nil, relpath_names = nil; 23756 23757 23758 dest_directory = self.$cleanpath().$to_s(); 23759 base_directory = base_directory.$cleanpath().$to_s(); 23760 dest_prefix = dest_directory; 23761 dest_names = []; 23762 while ($truthy((r = self.$chop_basename(dest_prefix)))) { 23763 23764 $b = r, $a = $to_ary($b), (dest_prefix = ($a[0] == null ? nil : $a[0])), (basename = ($a[1] == null ? nil : $a[1])); 23765 if ($neqeq(basename, ".")) { 23766 dest_names.$unshift(basename); 23767 } } base_prefix = base_directory; 23768 base_names = []; 23769 while ($truthy((r = self.$chop_basename(base_prefix)))) { 23770 23771 $b = r, $a = $to_ary($b), (base_prefix = ($a[0] == null ? nil : $a[0])), (basename = ($a[1] == null ? nil : $a[1])); 23772 if ($neqeq(basename, ".")) { 23773 base_names.$unshift(basename); 23774 } } if (!$truthy($$('SAME_PATHS')['$[]'](dest_prefix, base_prefix))) { 23775 self.$raise($$('ArgumentError'), "different prefix: " + (dest_prefix.$inspect()) + " and " + (base_directory.$inspect())); 23776 } while ($truthy(($truthy(($ret_or_1 = ($truthy(($ret_or_2 = dest_names['$empty?']()['$!']())) ? (base_names['$empty?']()['$!']()) : ($ret_or_2)))) ? ($$('SAME_PATHS')['$[]'](dest_names.$first(), base_names.$first())) : ($ret_or_1)))) { 23777 23778 dest_names.$shift(); 23779 base_names.$shift(); 23780 } if ($truthy(base_names['$include?'](".."))) { 23781 self.$raise($$('ArgumentError'), "base_directory has ..: " + (base_directory.$inspect())); 23782 } base_names.$fill(".."); 23783 relpath_names = $rb_plus(base_names, dest_names); 23784 if ($truthy(relpath_names['$empty?']())) { 23785 return $$('Pathname').$new(".") 23786 } else { 23787 return $$('Pathname').$new($send($$('File'), 'join', $to_a(relpath_names))) 23788 } }); 23789 23790 $def(self, '$entries', function $$entries() { 23791 var self = this; 23792 23793 return $send($$('Dir').$entries(self.path), 'map', [], function $$11(f){var self = $$11.$$s == null ? this : $$11.$$s; 23794 23795 23796 if (f == null) f = nil; 23797 return self.$class().$new(f);}, {$$s: self}) 23798 }); 23799 $alias(self, "===", "=="); 23800 $alias(self, "eql?", "=="); 23801 $alias(self, "to_s", "to_path"); 23802 return $alias(self, "to_str", "to_path"); 23803 })($nesting[0], null, $nesting); 23804 return (function($base, $parent_nesting) { 23805 var self = $module($base, 'Kernel'); 23806 23807 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting); 23808 23809 return $def(self, '$Pathname', function $$Pathname(path) { 23810 23811 return $$('Pathname').$new(path) 23812 }) 23813 })($nesting[0], $nesting); 23814}; 23815 23816Opal.modules["stringio"] = function(Opal) {/* Generated by Opal 1.7.3 */ 23817 var $klass = Opal.klass, $defs = Opal.defs, $send2 = Opal.send2, $find_super = Opal.find_super, $def = Opal.def, $eqeqeq = Opal.eqeqeq, $truthy = Opal.truthy, $rb_ge = Opal.rb_ge, $rb_gt = Opal.rb_gt, $rb_plus = Opal.rb_plus, $rb_minus = Opal.rb_minus, $return_ivar = Opal.return_ivar, $eqeq = Opal.eqeq, $alias = Opal.alias, $nesting = [], $$ = Opal.$r($nesting), nil = Opal.nil, $$$ = Opal.$$$; 23818 23819 Opal.add_stubs('new,call,close,attr_accessor,check_readable,==,length,===,>=,raise,>,+,-,seek,check_writable,String,[],eof?,write,read,tell'); 23820 return (function($base, $super, $parent_nesting) { 23821 var self = $klass($base, $super, 'StringIO'); 23822 23823 var $nesting = [self].concat($parent_nesting), $$ = Opal.$r($nesting), $proto = self.$$prototype; 23824 23825 $proto.position = $proto.string = nil; 23826 23827 $defs(self, '$open', function $$open(string, mode) { 23828 var block = $$open.$$p || nil, self = this, io = nil, res = nil; 23829 23830 $$open.$$p = null; 23831 if (string == null) string = ""; 23832 if (mode == null) mode = nil; 23833 io = self.$new(string, mode); 23834 res = block.$call(io); 23835 io.$close(); 23836 return res; 23837 }, -1); 23838 self.$attr_accessor("string"); 23839 23840 $def(self, '$initialize', function $$initialize(string, mode) { 23841 var self = this; 23842 23843 $$initialize.$$p = null; 23844 23845 if (string == null) string = ""; 23846 if (mode == null) mode = "rw"; 23847 self.string = string; 23848 self.position = 0; 23849 return $send2(self, $find_super(self, 'initialize', $$initialize, false, true), 'initialize', [nil, mode], null); 23850 }, -1); 23851 23852 $def(self, '$eof?', function $StringIO_eof$ques$1() { 23853 var self = this; 23854 23855 23856 self.$check_readable(); 23857 return self.position['$=='](self.string.$length()); 23858 }); 23859 23860 $def(self, '$seek', function $$seek(pos, whence) { 23861 var self = this, $ret_or_1 = nil; 23862 23863 23864 if (whence == null) whence = $$$($$('IO'), 'SEEK_SET'); 23865 self.read_buffer = ""; 23866 if ($eqeqeq($$$($$('IO'), 'SEEK_SET'), ($ret_or_1 = whence))) { 23867 23868 if (!$truthy($rb_ge(pos, 0))) { 23869 self.$raise($$$($$('Errno'), 'EINVAL')); 23870 } self.position = pos; 23871 } else if ($eqeqeq($$$($$('IO'), 'SEEK_CUR'), $ret_or_1)) { 23872 if ($truthy($rb_gt($rb_plus(self.position, pos), self.string.$length()))) { 23873 self.position = self.string.$length(); 23874 } else { 23875 self.position = $rb_plus(self.position, pos); 23876 } 23877 } else if ($eqeqeq($$$($$('IO'), 'SEEK_END'), $ret_or_1)) { 23878 if ($truthy($rb_gt(pos, self.string.$length()))) { 23879 self.position = 0; 23880 } else { 23881 self.position = $rb_minus(self.position, pos); 23882 } 23883 } else ; return 0; 23884 }, -2); 23885 23886 $def(self, '$tell', $return_ivar("position")); 23887 23888 $def(self, '$rewind', function $$rewind() { 23889 var self = this; 23890 23891 return self.$seek(0) 23892 }); 23893 23894 $def(self, '$write', function $$write(string) { 23895 var self = this, before = nil, after = nil; 23896 23897 23898 self.$check_writable(); 23899 self.read_buffer = ""; 23900 string = self.$String(string); 23901 if ($eqeq(self.string.$length(), self.position)) { 23902 23903 self.string = $rb_plus(self.string, string); 23904 return (self.position = $rb_plus(self.position, string.$length())); 23905 } else { 23906 23907 before = self.string['$[]'](Opal.Range.$new(0, $rb_minus(self.position, 1), false)); 23908 after = self.string['$[]'](Opal.Range.$new($rb_plus(self.position, string.$length()), -1, false)); 23909 self.string = $rb_plus($rb_plus(before, string), after); 23910 return (self.position = $rb_plus(self.position, string.$length())); 23911 } }); 23912 23913 $def(self, '$read', function $$read(length, outbuf) { 23914 var self = this, string = nil, str = nil; 23915 23916 23917 if (length == null) length = nil; 23918 if (outbuf == null) outbuf = nil; 23919 self.$check_readable(); 23920 if ($truthy(self['$eof?']())) { 23921 return nil 23922 } string = ($truthy(length) ? (((str = self.string['$[]'](self.position, length)), (self.position = $rb_plus(self.position, length)), ($truthy($rb_gt(self.position, self.string.$length())) ? ((self.position = self.string.$length())) : nil), str)) : (((str = self.string['$[]'](Opal.Range.$new(self.position, -1, false))), (self.position = self.string.$length()), str))); 23923 if ($truthy(outbuf)) { 23924 return outbuf.$write(string) 23925 } else { 23926 return string 23927 } }, -1); 23928 23929 $def(self, '$sysread', function $$sysread(length) { 23930 var self = this; 23931 23932 23933 self.$check_readable(); 23934 return self.$read(length); 23935 }); 23936 $alias(self, "eof", "eof?"); 23937 $alias(self, "pos", "tell"); 23938 $alias(self, "pos=", "seek"); 23939 return $alias(self, "readpartial", "read"); 23940 })($nesting[0], $$('IO'), $nesting) 23941}; 23942 23943export { Opal$1 as default }; 23944