Class: Module

Inherits:
Object
  • Object
show all
Defined in:
opal/opal/corelib/module.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
24
# File 'opal/opal/corelib/module.rb', line 2

def self.new(&block)
  %x{
    function AnonModule(){}
    var klass      = Opal.boot(Opal.Module, AnonModule);
    klass.$$name   = nil;
    klass.$$class  = Opal.Module;
    klass.$$dep    = []
    klass.$$is_mod = 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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'opal/opal/corelib/module.rb', line 32

def <(other)
  %x{
    var working = self;

    while (working) {
      if (working === other) {
        return true;
      }

      working = working.$$parent;
    }

    return false;
  }
end

#===(object) ⇒ Object



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

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

  `Opal.is_a(object, self)`
end

#alias_method(newname, oldname) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'opal/opal/corelib/module.rb', line 48

def alias_method(newname, oldname)
  %x{
    var newjsid = '$' + newname,
        body    = self.$$proto['$' + oldname];

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

    return self;
  }
  self
end

#alias_native(mid, jsid = mid) ⇒ Object



65
66
67
# File 'opal/opal/corelib/module.rb', line 65

def alias_native(mid, jsid = mid)
  `self.$$proto['$' + mid] = self.$$proto[jsid]`
end

#ancestorsObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'opal/opal/corelib/module.rb', line 69

def ancestors
  %x{
    var parent = self,
        result = [];

    while (parent) {
      result.push(parent);
      result = result.concat(parent.$$inc);

      parent = parent.$$super;
    }

    return result;
  }
end

#append_features(klass) ⇒ Object



85
86
87
88
# File 'opal/opal/corelib/module.rb', line 85

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

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



90
91
92
93
# File 'opal/opal/corelib/module.rb', line 90

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

#attr_reader(*names) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'opal/opal/corelib/module.rb', line 97

def attr_reader(*names)
  %x{
    var proto = self.$$proto;

    for (var i = names.length - 1; i >= 0; i--) {
      var name = names[i],
          id   = '$' + name;

      // the closure here is needed because name will change at the next
      // cycle, I wish we could use let.
      var body = (function(name) {
        return function() {
          return this[name];
        };
      })(name);

      // initialize the instance variable as nil
      proto[name] = nil;

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

  nil
end

#attr_writer(*names) ⇒ Object



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 128

def attr_writer(*names)
  %x{
    var proto = self.$$proto;

    for (var i = names.length - 1; i >= 0; i--) {
      var name = names[i],
          id   = '$' + name + '=';

      // the closure here is needed because name will change at the next
      // cycle, I wish we could use let.
      var body = (function(name){
        return function(value) {
          return this[name] = value;
        }
      })(name);

      // initialize the instance variable as nil
      proto[name] = nil;

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

  nil
end

#autoload(const, path) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# File 'opal/opal/corelib/module.rb', line 159

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:



172
173
174
175
176
177
178
179
180
# File 'opal/opal/corelib/module.rb', line 172

def class_variable_get(name)
  name = Opal.coerce_to!(name, String, :to_str)
  raise NameError, 'class vars should start with @@' if `name.length < 3 || name.slice(0,2) !== '@@'`
  %x{
    var value = Opal.cvars[name.slice(2)];
    #{raise NameError, 'uninitialized class variable @@a in' if `value == null`}
    return value;
  }
end

#class_variable_set(name, value) ⇒ Object

Raises:



182
183
184
185
186
187
188
189
# File 'opal/opal/corelib/module.rb', line 182

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:



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'opal/opal/corelib/module.rb', line 197

def const_defined?(name, inherit = true)
  raise NameError, "wrong constant 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:



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'opal/opal/corelib/module.rb', line 223

def const_get(name, inherit = true)
  if name['::'] && name != '::'
    return name.split('::').inject(self){|o, c| o.const_get(c)}
  end
  raise NameError, "wrong constant 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 scopes[i][name];
      }
    }

    return #{const_missing name};
  }
end

#const_missing(name) ⇒ Object

Raises:



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'opal/opal/corelib/module.rb', line 251

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

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

        return #{const_get name};
      }
    }
  }

  raise NameError, "uninitialized constant #{self}::#{name}"
end

#const_set(name, value) ⇒ Object

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'opal/opal/corelib/module.rb', line 267

def const_set(name, value)
  raise NameError, "wrong constant 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



191
192
193
# File 'opal/opal/corelib/module.rb', line 191

def constants
  `self.$$scope.constants`
end

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



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'opal/opal/corelib/module.rb', line 281

def define_method(name, method = undefined, &block)
  if `method === undefined && !#{block_given?}`
    raise ArgumentError, "tried to create a Proc object without a block"
  end

  block ||= case method
            when Proc
              method
            when Method
              method.to_proc
            when UnboundMethod
              lambda do |*args|
                bound = method.bind(self)
                bound.call *args
              end
            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;

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

    return name;
  }
end

#extended(mod) ⇒ Object



409
410
# File 'opal/opal/corelib/module.rb', line 409

def extended(mod)
end

#include(*mods) ⇒ Object



324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'opal/opal/corelib/module.rb', line 324

def include(*mods)
  %x{
    for (var i = mods.length - 1; i >= 0; i--) {
      var mod = mods[i];

      if (mod === self) {
        continue;
      }

      #{`mod`.append_features self};
      #{`mod`.included self};
    }
  }

  self
end

#include?(mod) ⇒ Boolean

Returns:



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'opal/opal/corelib/module.rb', line 341

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



406
407
# File 'opal/opal/corelib/module.rb', line 406

def included(mod)
end

#instance_method(name) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
# File 'opal/opal/corelib/module.rb', line 355

def instance_method(name)
  %x{
    var meth = self.$$proto['$' + name];

    if (!meth || meth.$$stub) {
      #{raise NameError, "undefined method `#{name}' for class `#{self.name}'"};
    }

    return #{UnboundMethod.new(self, `meth`, name)};
  }
end

#instance_methods(include_super = true) ⇒ Object Also known as: public_instance_methods



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'opal/opal/corelib/module.rb', line 367

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_mod) {
        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_defined?(method) ⇒ Boolean Also known as: public_method_defined?

Returns:



447
448
449
450
451
452
# File 'opal/opal/corelib/module.rb', line 447

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

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

Raises:



412
413
414
415
416
417
418
419
420
421
422
423
424
425
# File 'opal/opal/corelib/module.rb', line 412

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(&block) ⇒ Object Also known as: class_exec



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'opal/opal/corelib/module.rb', line 429

def module_exec(&block)
  %x{
    if (block === nil) {
      throw new Error("no block given");
    }

    var block_self = block.$$s, result;

    block.$$s = null;
    result = block.apply(self, $slice.call(arguments));
    block.$$s = block_self;

    return result;
  }
end

#module_function(*methods) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'opal/opal/corelib/module.rb', line 454

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], func = self.$$proto['$' + meth];

        self.constructor.prototype['$' + meth] = func;
      }
    }

    return self;
  }
end

#nameObject



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
# File 'opal/opal/corelib/module.rb', line 471

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(name) ⇒ Object Also known as: public_class_method



515
516
517
# File 'opal/opal/corelib/module.rb', line 515

def private_class_method(name)
  `self['$' + name] || nil`
end

#private_constantObject



524
525
# File 'opal/opal/corelib/module.rb', line 524

def private_constant(*)
end

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

Returns:



520
521
522
# File 'opal/opal/corelib/module.rb', line 520

def private_method_defined?(obj)
  false
end

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



501
502
503
504
505
506
507
508
509
# File 'opal/opal/corelib/module.rb', line 501

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

    return nil;
  }
end

#remove_class_variableObject



533
534
# File 'opal/opal/corelib/module.rb', line 533

def remove_class_variable(*)
end

#remove_const(name) ⇒ Object



536
537
538
539
540
541
542
# File 'opal/opal/corelib/module.rb', line 536

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

#remove_method(name) ⇒ Object



318
319
320
321
322
# File 'opal/opal/corelib/module.rb', line 318

def remove_method(name)
  `Opal.undef(self, '$' + name)`

  self
end

#to_sObject



544
545
546
# File 'opal/opal/corelib/module.rb', line 544

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

#undef_method(symbol) ⇒ Object



548
549
550
551
552
# File 'opal/opal/corelib/module.rb', line 548

def undef_method(symbol)
  `Opal.add_stub_for(self.$$proto, "$" + symbol)`

  self
end