Class: Module

Inherits:
Object show all
Defined in:
opal/opal/corelib/module.rb,
opal/opal/corelib/unsupported.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(&block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'opal/opal/corelib/module.rb', line 2

def self.new(&block)
  %x{
    var klass         = Opal.boot_module_object();
    klass.$$name      = nil;
    klass.$$class     = Opal.Module;
    klass.$$dep       = []
    klass.$$is_module = true;
    klass.$$proto     = {};

    // inherit scope from parent
    Opal.create_scope(Opal.Module.$$scope, klass);

    if (block !== nil) {
      var block_self = block.$$s;
      block.$$s = null;
      block.call(klass);
      block.$$s = block_self;
    }

    return klass;
  }
end

Instance Method Details

#<(other) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'opal/opal/corelib/module.rb', line 31

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



62
63
64
# File 'opal/opal/corelib/module.rb', line 62

def <=(other)
  equal?(other) || self < other
end

#<=>(other) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'opal/opal/corelib/module.rb', line 78

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



25
26
27
28
29
# File 'opal/opal/corelib/module.rb', line 25

def ===(object)
  return false if `object == null`

  `Opal.is_a(object, self)`
end

#>(other) ⇒ Object



66
67
68
69
70
71
72
# File 'opal/opal/corelib/module.rb', line 66

def >(other)
  unless Module === other
    raise TypeError, "compared with non class/module"
  end

  other < self
end

#>=(other) ⇒ Object



74
75
76
# File 'opal/opal/corelib/module.rb', line 74

def >=(other)
  equal?(other) || self > other
end

#alias_method(newname, oldname) ⇒ Object



94
95
96
97
98
# File 'opal/opal/corelib/module.rb', line 94

def alias_method(newname, oldname)
  `Opal.alias(self, newname, oldname)`

  self
end

#alias_native(mid, jsid = mid) ⇒ Object



100
101
102
103
104
# File 'opal/opal/corelib/module.rb', line 100

def alias_native(mid, jsid = mid)
  `Opal.alias_native(self, mid, jsid)`

  self
end

#ancestorsObject



106
107
108
# File 'opal/opal/corelib/module.rb', line 106

def ancestors
  `Opal.ancestors(self)`
end

#append_features(klass) ⇒ Object



110
111
112
113
# File 'opal/opal/corelib/module.rb', line 110

def append_features(klass)
  `Opal.append_features(self, klass)`
  self
end

#attr_accessor(*names) ⇒ Object Also known as: attr



115
116
117
118
# File 'opal/opal/corelib/module.rb', line 115

def attr_accessor(*names)
  attr_reader(*names)
  attr_writer(*names)
end

#attr_reader(*names) ⇒ Object



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
152
153
154
155
156
157
# File 'opal/opal/corelib/module.rb', line 122

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;

      if (self.$$is_singleton) {
        proto.constructor.prototype[id] = body;
      }
      else {
        Opal.defn(self, id, body);
      }
    }
  }

  nil
end

#attr_writer(*names) ⇒ Object



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
187
188
189
# File 'opal/opal/corelib/module.rb', line 159

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);

      // 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



191
192
193
194
195
196
197
198
199
200
201
202
# File 'opal/opal/corelib/module.rb', line 191

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

Raises:



204
205
206
207
208
209
210
211
212
# File 'opal/opal/corelib/module.rb', line 204

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

Raises:



214
215
216
217
218
219
220
221
# File 'opal/opal/corelib/module.rb', line 214

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

Returns:

Raises:



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'opal/opal/corelib/module.rb', line 229

def const_defined?(name, inherit = true)
  raise NameError.new("wrong constant name #{name}", name) unless name =~ /^[A-Z]\w*$/

  %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

Raises:



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
# File 'opal/opal/corelib/module.rb', line 255

def const_get(name, inherit = true)
  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 `/^[A-Z]\w*$/.test(name)`

  %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

Raises:



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'opal/opal/corelib/module.rb', line 285

def const_missing(name)
  %x{
    if (self.$$autoload) {
      var file = self.$$autoload[name];

      if (file) {
        self.$require(file);

        return #{const_get name};
      }
    }
  }

  raise NameError.new("uninitialized constant #{self}::#{name}", name)
end

#const_set(name, value) ⇒ Object

Raises:



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'opal/opal/corelib/module.rb', line 301

def const_set(name, value)
  raise NameError.new("wrong constant name #{name}", name) unless name =~ /^[A-Z]\w*$/

  begin
    name = name.to_str
  rescue
    raise TypeError, 'conversion with #to_str failed'
  end

  `Opal.casgn(self, name, value)`

  value
end

#constantsObject



223
224
225
# File 'opal/opal/corelib/module.rb', line 223

def constants
  `self.$$scope.constants.slice(0)`
end

#define_method(name, method = undefined, &block) ⇒ Object



315
316
317
318
319
320
321
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
# File 'opal/opal/corelib/module.rb', line 315

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;

    Opal.defn(self, id, block);

    return name;
  }
end

#extended(mod) ⇒ Object



453
454
# File 'opal/opal/corelib/module.rb', line 453

def extended(mod)
end

#include(*mods) ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# File 'opal/opal/corelib/module.rb', line 364

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

Returns:



385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'opal/opal/corelib/module.rb', line 385

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



450
451
# File 'opal/opal/corelib/module.rb', line 450

def included(mod)
end

#instance_method(name) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
# File 'opal/opal/corelib/module.rb', line 399

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



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'opal/opal/corelib/module.rb', line 411

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

#method_addedObject



456
457
# File 'opal/opal/corelib/module.rb', line 456

def method_added(*)
end

#method_defined?(method) ⇒ Boolean Also known as: public_method_defined?

Returns:



500
501
502
503
504
505
# File 'opal/opal/corelib/module.rb', line 500

def method_defined?(method)
  %x{
    var body = self.$$proto['$' + method];
    return (!!body) && !body.$$stub;
  }
end

#method_removedObject



459
460
# File 'opal/opal/corelib/module.rb', line 459

def method_removed(*)
end

#method_undefinedObject



462
463
# File 'opal/opal/corelib/module.rb', line 462

def method_undefined(*)
end

#module_eval(&block) ⇒ Object Also known as: class_eval

Raises:



465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'opal/opal/corelib/module.rb', line 465

def module_eval(&block)
  raise ArgumentError, 'no block given' unless block

  %x{
    var old = block.$$s,
        result;

    block.$$s = null;
    result = block.call(self);
    block.$$s = old;

    return result;
  }
end

#module_exec(*args, &block) ⇒ Object Also known as: class_exec



482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
# File 'opal/opal/corelib/module.rb', line 482

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



507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
# File 'opal/opal/corelib/module.rb', line 507

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

#nameObject



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# File 'opal/opal/corelib/module.rb', line 526

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_methodObject Also known as: public_class_method



183
184
185
# File 'opal/opal/corelib/unsupported.rb', line 183

def private_class_method(*)
  self
end

#private_constantObject



193
194
# File 'opal/opal/corelib/unsupported.rb', line 193

def private_constant(*)
end

#private_method_defined?(obj) ⇒ Boolean Also known as: protected_method_defined?

Returns:



189
190
191
# File 'opal/opal/corelib/unsupported.rb', line 189

def private_method_defined?(obj)
  false
end

#public(*methods) ⇒ Object Also known as: private, protected, nesting



167
168
169
170
171
172
173
174
175
# File 'opal/opal/corelib/unsupported.rb', line 167

def public(*methods)
  %x{
    if (methods.length === 0) {
      self.$$module_function = false;
    }

    return nil;
  }
end

#remove_class_variableObject



556
557
# File 'opal/opal/corelib/module.rb', line 556

def remove_class_variable(*)
end

#remove_const(name) ⇒ Object



559
560
561
562
563
564
565
# File 'opal/opal/corelib/module.rb', line 559

def remove_const(name)
  %x{
    var old = self.$$scope[name];
    delete self.$$scope[name];
    return old;
  }
end

#remove_method(*names) ⇒ Object



350
351
352
353
354
355
356
357
358
# File 'opal/opal/corelib/module.rb', line 350

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

Returns:



360
361
362
# File 'opal/opal/corelib/module.rb', line 360

def singleton_class?
  `!!self.$$is_singleton`
end

#to_sObject



567
568
569
# File 'opal/opal/corelib/module.rb', line 567

def to_s
  `Opal.Module.$name.call(self)` || "#<#{`self.$$is_module ? 'Module' : 'Class'`}:0x#{__id__.to_s(16)}>"
end

#undef_method(*names) ⇒ Object



571
572
573
574
575
576
577
578
579
# File 'opal/opal/corelib/module.rb', line 571

def undef_method(*names)
  %x{
    for (var i = 0, length = names.length; i < length; i++) {
      Opal.udef(self, "$" + names[i]);
    }
  }

  self
end