Class: Module
- Defined in:
- opal/opal/corelib/module.rb,
opal/opal/corelib/unsupported.rb,
opal/opal/corelib/marshal/write_buffer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #<(other) ⇒ Object
- #<=(other) ⇒ Object
- #<=>(other) ⇒ Object
- #===(object) ⇒ Object
- #>(other) ⇒ Object
- #>=(other) ⇒ Object
- #__marshal__(buffer) ⇒ Object
- #alias_method(newname, oldname) ⇒ Object
- #alias_native(mid, jsid = mid) ⇒ Object
- #ancestors ⇒ Object
- #append_features(klass) ⇒ Object
- #attr_accessor(*names) ⇒ Object (also: #attr)
- #attr_reader(*names) ⇒ Object
- #attr_writer(*names) ⇒ Object
- #autoload(const, path) ⇒ Object
- #class_variable_get(name) ⇒ Object
- #class_variable_set(name, value) ⇒ Object
-
#const_defined?(name, inherit = true) ⇒ Boolean
check for constant within current scope if inherit is true or self is Object, will also check ancestors.
- #const_get(name, inherit = true) ⇒ Object
- #const_missing(name) ⇒ Object
- #const_set(name, value) ⇒ Object
- #constants ⇒ Object
- #define_method(name, method = undefined, &block) ⇒ Object
- #extended(mod) ⇒ Object
- #include(*mods) ⇒ Object
- #include?(mod) ⇒ Boolean
- #included(mod) ⇒ Object
- #included_modules ⇒ Object
-
#initialize(&block) ⇒ Module
constructor
A new instance of Module.
- #instance_method(name) ⇒ Object
- #instance_methods(include_super = true) ⇒ Object (also: #public_instance_methods)
- #instance_variables ⇒ Object
- #method_added ⇒ Object
- #method_defined?(method) ⇒ Boolean (also: #public_method_defined?)
- #method_removed ⇒ Object
- #method_undefined ⇒ Object
- #module_eval(*args, &block) ⇒ Object (also: #class_eval)
- #module_exec(*args, &block) ⇒ Object (also: #class_exec)
- #module_function(*methods) ⇒ Object
- #name ⇒ Object
- #private_class_method ⇒ Object (also: #public_class_method)
- #private_constant ⇒ Object
- #private_method_defined?(obj) ⇒ Boolean (also: #protected_method_defined?)
- #public(*methods) ⇒ Object (also: #private, #protected, #nesting)
- #remove_class_variable ⇒ Object
- #remove_const(name) ⇒ Object
- #remove_method(*names) ⇒ Object
- #singleton_class? ⇒ Boolean
- #to_s ⇒ Object
- #undef_method(*names) ⇒ Object
Constructor Details
#initialize(&block) ⇒ Module
Returns a new instance of Module
12 13 14 |
# File 'opal/opal/corelib/module.rb', line 12 def initialize(&block) `Opal.module_initialize(self, block)` end |
Class Method Details
.allocate ⇒ Object
2 3 4 5 6 7 8 9 10 |
# File 'opal/opal/corelib/module.rb', line 2 def self.allocate %x{ var module; module = Opal.module_allocate(); Opal.create_scope(Opal.Module.$$scope, module, null); return module; } end |
Instance Method Details
#<(other) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'opal/opal/corelib/module.rb', line 22 def <(other) unless Module === other raise TypeError, "compared with non class/module" end # class cannot be a descendant of itself %x{ var working = self, ancestors, i, length; if (working === other) { return false; } for (i = 0, ancestors = Opal.ancestors(self), length = ancestors.length; i < length; i++) { if (ancestors[i] === other) { return true; } } for (i = 0, ancestors = Opal.ancestors(other), length = ancestors.length; i < length; i++) { if (ancestors[i] === self) { return false; } } return nil; } end |
#<=(other) ⇒ Object
53 54 55 |
# File 'opal/opal/corelib/module.rb', line 53 def <=(other) equal?(other) || self < other end |
#<=>(other) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'opal/opal/corelib/module.rb', line 69 def <=>(other) %x{ if (self === other) { return 0; } } unless Module === other return nil end lt = self < other return nil if lt.nil? lt ? -1 : 1 end |
#===(object) ⇒ Object
16 17 18 19 20 |
# File 'opal/opal/corelib/module.rb', line 16 def ===(object) return false if `object == null` `Opal.is_a(object, self)` end |
#>(other) ⇒ Object
57 58 59 60 61 62 63 |
# File 'opal/opal/corelib/module.rb', line 57 def >(other) unless Module === other raise TypeError, "compared with non class/module" end other < self end |
#>=(other) ⇒ Object
65 66 67 |
# File 'opal/opal/corelib/module.rb', line 65 def >=(other) equal?(other) || self > other end |
#__marshal__(buffer) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'opal/opal/corelib/marshal/write_buffer.rb', line 108 def __marshal__(buffer) unless name raise TypeError, "can't dump anonymous module" end buffer.save_link(self) buffer.append("m") buffer.write_module(self) end |
#alias_method(newname, oldname) ⇒ Object
85 86 87 88 89 |
# File 'opal/opal/corelib/module.rb', line 85 def alias_method(newname, oldname) `Opal.alias(self, newname, oldname)` self end |
#alias_native(mid, jsid = mid) ⇒ Object
91 92 93 94 95 |
# File 'opal/opal/corelib/module.rb', line 91 def alias_native(mid, jsid = mid) `Opal.alias_native(self, mid, jsid)` self end |
#ancestors ⇒ Object
97 98 99 |
# File 'opal/opal/corelib/module.rb', line 97 def ancestors `Opal.ancestors(self)` end |
#append_features(klass) ⇒ Object
101 102 103 104 |
# File 'opal/opal/corelib/module.rb', line 101 def append_features(klass) `Opal.append_features(self, klass)` self end |
#attr_accessor(*names) ⇒ Object Also known as: attr
106 107 108 109 |
# File 'opal/opal/corelib/module.rb', line 106 def attr_accessor(*names) attr_reader(*names) attr_writer(*names) end |
#attr_reader(*names) ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'opal/opal/corelib/module.rb', line 113 def attr_reader(*names) %x{ var proto = self.$$proto; for (var i = names.length - 1; i >= 0; i--) { var name = names[i], id = '$' + name, ivar = Opal.ivar(name); // the closure here is needed because name will change at the next // cycle, I wish we could use let. var body = (function(ivar) { return function() { if (this[ivar] == null) { return nil; } else { return this[ivar]; } }; })(ivar); // initialize the instance variable as nil proto[ivar] = nil; body.$$parameters = []; body.$$arity = 0; if (self.$$is_singleton) { proto.constructor.prototype[id] = body; } else { Opal.defn(self, id, body); } } } nil end |
#attr_writer(*names) ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'opal/opal/corelib/module.rb', line 153 def attr_writer(*names) %x{ var proto = self.$$proto; for (var i = names.length - 1; i >= 0; i--) { var name = names[i], id = '$' + name + '=', ivar = Opal.ivar(name); // the closure here is needed because name will change at the next // cycle, I wish we could use let. var body = (function(ivar){ return function(value) { return this[ivar] = value; } })(ivar); body.$$parameters = [['req']]; body.$$arity = 1; // initialize the instance variable as nil proto[ivar] = nil; if (self.$$is_singleton) { proto.constructor.prototype[id] = body; } else { Opal.defn(self, id, body); } } } nil end |
#autoload(const, path) ⇒ Object
188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'opal/opal/corelib/module.rb', line 188 def autoload(const, path) %x{ var autoloaders; if (!(autoloaders = self.$$autoload)) { autoloaders = self.$$autoload = {}; } autoloaders[#{const}] = #{path}; return nil; } end |
#class_variable_get(name) ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'opal/opal/corelib/module.rb', line 201 def class_variable_get(name) name = Opal.coerce_to!(name, String, :to_str) raise NameError.new('class vars should start with @@', name) if `name.length < 3 || name.slice(0,2) !== '@@'` %x{ var value = Opal.cvars[name.slice(2)]; #{raise NameError.new('uninitialized class variable @@a in', name) if `value == null`} return value; } end |
#class_variable_set(name, value) ⇒ Object
211 212 213 214 215 216 217 218 |
# File 'opal/opal/corelib/module.rb', line 211 def class_variable_set(name, value) name = Opal.coerce_to!(name, String, :to_str) raise NameError if `name.length < 3 || name.slice(0,2) !== '@@'` %x{ Opal.cvars[name.slice(2)] = value; return value; } end |
#const_defined?(name, inherit = true) ⇒ Boolean
check for constant within current scope if inherit is true or self is Object, will also check ancestors
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'opal/opal/corelib/module.rb', line 226 def const_defined?(name, inherit = true) name = Opal.const_name!(name) raise NameError.new("wrong constant name #{name}", name) unless name =~ Opal::CONST_NAME_REGEXP %x{ var scopes = [self.$$scope]; if (inherit || self === Opal.Object) { var parent = self.$$super; while (parent !== Opal.BasicObject) { scopes.push(parent.$$scope); parent = parent.$$super; } } for (var i = 0, length = scopes.length; i < length; i++) { if (scopes[i].hasOwnProperty(name)) { return true; } } return false; } end |
#const_get(name, inherit = true) ⇒ Object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
# File 'opal/opal/corelib/module.rb', line 254 def const_get(name, inherit = true) name = Opal.const_name!(name) %x{ if (name.indexOf('::') === 0 && name !== '::'){ name = name.slice(2); } } if `name.indexOf('::') != -1 && name != '::'` return name.split('::').inject(self) { |o, c| o.const_get(c) } end raise NameError.new("wrong constant name #{name}", name) unless name =~ Opal::CONST_NAME_REGEXP %x{ var scopes = [self.$$scope]; if (inherit || self == Opal.Object) { var parent = self.$$super; while (parent !== Opal.BasicObject) { scopes.push(parent.$$scope); parent = parent.$$super; } } for (var i = 0, length = scopes.length; i < length; i++) { if (scopes[i].hasOwnProperty(name)) { return scopes[i][name]; } } return #{const_missing name}; } end |
#const_missing(name) ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'opal/opal/corelib/module.rb', line 292 def const_missing(name) %x{ if (self.$$autoload) { var file = self.$$autoload[name]; if (file) { self.$require(file); return #{const_get name}; } } } full_const_name = self == Object ? name : "#{self}::#{name}" raise NameError.new("uninitialized constant #{full_const_name}", name) end |
#const_set(name, value) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 |
# File 'opal/opal/corelib/module.rb', line 310 def const_set(name, value) name = Opal.const_name!(name) if !(name =~ Opal::CONST_NAME_REGEXP) || name.start_with?('::') raise NameError.new("wrong constant name #{name}", name) end `Opal.casgn(self, name, value)` value end |
#constants ⇒ Object
220 221 222 |
# File 'opal/opal/corelib/module.rb', line 220 def constants `self.$$scope.constants.slice(0)` end |
#define_method(name, method = undefined, &block) ⇒ Object
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'opal/opal/corelib/module.rb', line 322 def define_method(name, method = undefined, &block) if `method === undefined && block === nil` raise ArgumentError, "tried to create a Proc object without a block" end block ||= case method when Proc method when Method `#{method.to_proc}.$$unbound` when UnboundMethod lambda {|*args| bound = method.bind(self) bound.call(*args) } else raise TypeError, "wrong argument type #{block.class} (expected Proc/Method)" end %x{ var id = '$' + name; block.$$jsid = name; block.$$s = null; block.$$def = block; block.$$define_meth = true; Opal.defn(self, id, block); return name; } end |
#extended(mod) ⇒ Object
490 491 |
# File 'opal/opal/corelib/module.rb', line 490 def extended(mod) end |
#include(*mods) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'opal/opal/corelib/module.rb', line 372 def include(*mods) %x{ for (var i = mods.length - 1; i >= 0; i--) { var mod = mods[i]; if (mod === self) { continue; } if (!mod.$$is_module) { #{raise TypeError, "wrong argument type #{`mod`.class} (expected Module)"}; } #{`mod`.append_features self}; #{`mod`.included self}; } } self end |
#include?(mod) ⇒ Boolean
422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'opal/opal/corelib/module.rb', line 422 def include?(mod) %x{ for (var cls = self; cls; cls = cls.$$super) { for (var i = 0; i != cls.$$inc.length; i++) { var mod2 = cls.$$inc[i]; if (mod === mod2) { return true; } } } return false; } end |
#included(mod) ⇒ Object
487 488 |
# File 'opal/opal/corelib/module.rb', line 487 def included(mod) end |
#included_modules ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'opal/opal/corelib/module.rb', line 393 def included_modules %x{ var results; var module_chain = function(klass) { var included = []; for (var i = 0; i != klass.$$inc.length; i++) { var mod_or_class = klass.$$inc[i]; included.push(mod_or_class); included = included.concat(module_chain(mod_or_class)); } return included; }; results = module_chain(self); // need superclass's modules if (self.$$is_class) { for (var cls = self; cls; cls = cls.$$super) { results = results.concat(module_chain(cls)); } } return results; } end |
#instance_method(name) ⇒ Object
436 437 438 439 440 441 442 443 444 445 446 |
# File 'opal/opal/corelib/module.rb', line 436 def instance_method(name) %x{ var meth = self.$$proto['$' + name]; if (!meth || meth.$$stub) { #{raise NameError.new("undefined method `#{name}' for class `#{self.name}'", name)}; } return #{UnboundMethod.new(self, `meth`, name)}; } end |
#instance_methods(include_super = true) ⇒ Object Also known as: public_instance_methods
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'opal/opal/corelib/module.rb', line 448 def instance_methods(include_super = true) %x{ var methods = [], proto = self.$$proto; for (var prop in proto) { if (prop.charAt(0) !== '$') { continue; } if (typeof(proto[prop]) !== "function") { continue; } if (proto[prop].$$stub) { continue; } if (!self.$$is_module) { if (self !== Opal.BasicObject && proto[prop] === Opal.BasicObject.$$proto[prop]) { continue; } if (!include_super && !proto.hasOwnProperty(prop)) { continue; } if (!include_super && proto[prop].$$donated) { continue; } } methods.push(prop.substr(1)); } return methods; } end |
#instance_variables ⇒ Object
634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
# File 'opal/opal/corelib/module.rb', line 634 def instance_variables consts = constants %x{ var result = []; for (var name in self) { if (self.hasOwnProperty(name) && name.charAt(0) !== '$' && name !== 'constructor' && !#{consts.include?(`name`)}) { result.push('@' + name); } } return result; } end |
#method_added ⇒ Object
493 494 |
# File 'opal/opal/corelib/module.rb', line 493 def method_added(*) end |
#method_defined?(method) ⇒ Boolean Also known as: public_method_defined?
553 554 555 556 557 558 |
# File 'opal/opal/corelib/module.rb', line 553 def method_defined?(method) %x{ var body = self.$$proto['$' + method]; return (!!body) && !body.$$stub; } end |
#method_removed ⇒ Object
496 497 |
# File 'opal/opal/corelib/module.rb', line 496 def method_removed(*) end |
#method_undefined ⇒ Object
499 500 |
# File 'opal/opal/corelib/module.rb', line 499 def method_undefined(*) end |
#module_eval(*args, &block) ⇒ Object Also known as: class_eval
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
# File 'opal/opal/corelib/module.rb', line 502 def module_eval(*args, &block) if block.nil? && `!!Opal.compile` Kernel.raise ArgumentError, "wrong number of arguments (0 for 1..3)" unless (1..3).cover? args.size string, file, _lineno = *args = { file: (file || '(eval)'), eval: true } = __OPAL_COMPILER_CONFIG__.merge() compiled = Opal.compile string, block = Kernel.proc do %x{ return (function(self) { return eval(compiled); })(self) } end elsif args.size > 0 Kernel.raise ArgumentError, "wrong number of arguments (#{args.size} for 0)" end %x{ var old = block.$$s, result; block.$$s = null; result = block.apply(self, [self]); block.$$s = old; return result; } end |
#module_exec(*args, &block) ⇒ Object Also known as: class_exec
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'opal/opal/corelib/module.rb', line 535 def module_exec(*args, &block) %x{ if (block === nil) { #{raise LocalJumpError, 'no block given'} } var block_self = block.$$s, result; block.$$s = null; result = block.apply(self, args); block.$$s = block_self; return result; } end |
#module_function(*methods) ⇒ Object
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'opal/opal/corelib/module.rb', line 560 def module_function(*methods) %x{ if (methods.length === 0) { self.$$module_function = true; } else { for (var i = 0, length = methods.length; i < length; i++) { var meth = methods[i], id = '$' + meth, func = self.$$proto[id]; Opal.defs(self, id, func); } } return self; } end |
#name ⇒ Object
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 |
# File 'opal/opal/corelib/module.rb', line 579 def name %x{ if (self.$$full_name) { return self.$$full_name; } var result = [], base = self; while (base) { if (base.$$name === nil) { return result.length === 0 ? nil : result.join('::'); } result.unshift(base.$$name); base = base.$$base_module; if (base === Opal.Object) { break; } } if (result.length === 0) { return nil; } return self.$$full_name = result.join('::'); } end |
#private_class_method ⇒ Object Also known as: public_class_method
153 154 155 |
# File 'opal/opal/corelib/unsupported.rb', line 153 def private_class_method(*) self end |
#private_constant ⇒ Object
163 164 |
# File 'opal/opal/corelib/unsupported.rb', line 163 def private_constant(*) end |
#private_method_defined?(obj) ⇒ Boolean Also known as: protected_method_defined?
159 160 161 |
# File 'opal/opal/corelib/unsupported.rb', line 159 def private_method_defined?(obj) false end |
#public(*methods) ⇒ Object Also known as: private, protected, nesting
137 138 139 140 141 142 143 144 145 |
# File 'opal/opal/corelib/unsupported.rb', line 137 def public(*methods) %x{ if (methods.length === 0) { self.$$module_function = false; } return nil; } end |
#remove_class_variable ⇒ Object
609 610 |
# File 'opal/opal/corelib/module.rb', line 609 def remove_class_variable(*) end |
#remove_const(name) ⇒ Object
612 613 614 615 616 617 618 |
# File 'opal/opal/corelib/module.rb', line 612 def remove_const(name) %x{ var old = self.$$scope[name]; delete self.$$scope[name]; return old; } end |
#remove_method(*names) ⇒ Object
358 359 360 361 362 363 364 365 366 |
# File 'opal/opal/corelib/module.rb', line 358 def remove_method(*names) %x{ for (var i = 0, length = names.length; i < length; i++) { Opal.rdef(self, "$" + names[i]); } } self end |
#singleton_class? ⇒ Boolean
368 369 370 |
# File 'opal/opal/corelib/module.rb', line 368 def singleton_class? `!!self.$$is_singleton` end |
#to_s ⇒ Object
620 621 622 |
# File 'opal/opal/corelib/module.rb', line 620 def to_s `Opal.Module.$name.call(self)` || "#<#{`self.$$is_module ? 'Module' : 'Class'`}:0x#{__id__.to_s(16)}>" end |
#undef_method(*names) ⇒ Object
624 625 626 627 628 629 630 631 632 |
# File 'opal/opal/corelib/module.rb', line 624 def undef_method(*names) %x{ for (var i = 0, length = names.length; i < length; i++) { Opal.udef(self, "$" + names[i]); } } self end |