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._klass  = Opal.Module;
    klass.__dep__ = []
    klass.__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
# File 'opal/opal/corelib/module.rb', line 48

def alias_method(newname, oldname)
  %x{
    self._proto['$' + newname] = self._proto['$' + oldname];

    if (self._methods) {
      $opal.donate(self, ['$' + newname ])
    }
  }
  self
end

#alias_native(mid, jsid = mid) ⇒ Object



59
60
61
# File 'opal/opal/corelib/module.rb', line 59

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

#ancestorsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'opal/opal/corelib/module.rb', line 63

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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
127
128
129
130
131
# File 'opal/opal/corelib/module.rb', line 79

def append_features(klass)
  %x{
    var module   = self,
        included = klass.__inc__;

    // check if this module is already included in the klass
    for (var i = 0, length = included.length; i < length; i++) {
      if (included[i] === module) {
        return;
      }
    }

    included.push(module);
    module.__dep__.push(klass);

    // iclass
    var iclass = {
      name: module._name,

      _proto:   module._proto,
      __parent: klass.__parent,
      __iclass: true
    };

    klass.__parent = iclass;

    var donator   = module._proto,
        prototype = klass._proto,
        methods   = module._methods;

    for (var i = 0, length = methods.length; i < length; i++) {
      var method = methods[i];

      if (prototype.hasOwnProperty(method) && !prototype[method]._donated) {
        // if the target class already has a method of the same name defined
        // and that method was NOT donated, then it must be a method defined
        // by the class so we do not want to override it
      }
      else {
        prototype[method] = donator[method];
        prototype[method]._donated = true;
      }
    }

    if (klass.__dep__) {
      $opal.donate(klass, methods.slice(), true);
    }

    $opal.donate_constants(module, klass);
  }

  self
end

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



133
134
135
136
# File 'opal/opal/corelib/module.rb', line 133

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

#attr_reader(*names) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'opal/opal/corelib/module.rb', line 138

def attr_reader(*names)
  %x{
    var proto = self._proto, cls = self;
    for (var i = 0, length = names.length; i < length; i++) {
      (function(name) {
        proto[name] = nil;
        var func = function() { return this[name] };

        if (cls._isSingleton) {
          proto.constructor.prototype['$' + name] = func;
        }
        else {
          proto['$' + name] = func;
          $opal.donate(self, ['$' + name ]);
        }
      })(names[i]);
    }
  }

  nil
end

#attr_writer(*names) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'opal/opal/corelib/module.rb', line 160

def attr_writer(*names)
  %x{
    var proto = self._proto, cls = self;
    for (var i = 0, length = names.length; i < length; i++) {
      (function(name) {
        proto[name] = nil;
        var func = function(value) { return this[name] = value; };

        if (cls._isSingleton) {
          proto.constructor.prototype['$' + name + '='] = func;
        }
        else {
          proto['$' + name + '='] = func;
          $opal.donate(self, ['$' + name + '=']);
        }
      })(names[i]);
    }
  }
  nil
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:



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'opal/opal/corelib/module.rb', line 189

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

  %x{
    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, len = scopes.length; i < len; i++) {
      if (scopes[i].hasOwnProperty(name)) {
        return true;
      }
    }

    return false;
  }
end

#const_get(name, inherit = true) ⇒ Object

Raises:



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'opal/opal/corelib/module.rb', line 212

def const_get(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, len = scopes.length; i < len; i++) {
      if (scopes[i].hasOwnProperty(name)) {
        return scopes[i][name];
      }
    }

    return #{const_missing name};
  }
end

#const_missing(const) ⇒ Object

Raises:



235
236
237
238
239
# File 'opal/opal/corelib/module.rb', line 235

def const_missing(const)
  name = `self._name`

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

#const_set(name, value) ⇒ Object

Raises:



241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'opal/opal/corelib/module.rb', line 241

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

  %x{
    $opal.casgn(self, name, value);
    return #{value}
  }
end

#constantsObject



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

def constants
  `self._scope.constants`
end

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'opal/opal/corelib/module.rb', line 256

def define_method(name, method = undefined, &block)
  %x{
    if (method) {
      block = #{method.to_proc};
    }

    if (block === nil) {
      throw new Error("no block given");
    }

    var jsid    = '$' + name;
    block._jsid = name;
    block._s    = null;
    block._def  = block;

    self._proto[jsid] = block;
    $opal.donate(self, [jsid]);

    return name;
  }
end

#extended(mod) ⇒ Object



358
359
# File 'opal/opal/corelib/module.rb', line 358

def extended(mod)
end

#include(*mods) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'opal/opal/corelib/module.rb', line 290

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:



307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'opal/opal/corelib/module.rb', line 307

def include?(mod)
  %x{
    for (var cls = self; cls; cls = cls.parent) {
      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



355
356
# File 'opal/opal/corelib/module.rb', line 355

def included(mod)
end

#instance_method(name) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
# File 'opal/opal/corelib/module.rb', line 321

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

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

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

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



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'opal/opal/corelib/module.rb', line 333

def instance_methods(include_super = false)
  %x{
    var methods = [], proto = self._proto;

    for (var prop in self._proto) {
      if (!include_super && !proto.hasOwnProperty(prop)) {
        continue;
      }

      if (!include_super && proto[prop]._donated) {
        continue;
      }

      if (prop.charAt(0) === '$') {
        methods.push(prop.substr(1));
      }
    }

    return methods;
  }
end

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

Returns:



396
397
398
399
400
401
# File 'opal/opal/corelib/module.rb', line 396

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

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

Raises:



361
362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'opal/opal/corelib/module.rb', line 361

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



378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'opal/opal/corelib/module.rb', line 378

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



403
404
405
406
407
408
409
410
411
412
413
# File 'opal/opal/corelib/module.rb', line 403

def module_function(*methods)
  %x{
    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



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

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



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

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

#private_constantObject



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

def private_constant(*)
end

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

Returns:



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

def private_method_defined?(obj)
  false
end

#publicObject Also known as: private, protected



445
446
# File 'opal/opal/corelib/module.rb', line 445

def public(*)
end

#remove_class_variableObject



468
469
# File 'opal/opal/corelib/module.rb', line 468

def remove_class_variable(*)
end

#remove_const(name) ⇒ Object



471
472
473
474
475
476
477
# File 'opal/opal/corelib/module.rb', line 471

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

#remove_method(name) ⇒ Object



278
279
280
281
282
283
284
285
286
287
288
# File 'opal/opal/corelib/module.rb', line 278

def remove_method(name)
  %x{
    var jsid    = '$' + name;
    var current = self._proto[jsid];
    delete self._proto[jsid];

    // Check if we need to reverse $opal.donate
    // $opal.retire(self, [jsid]);
    return self;
  }
end

#to_sObject



479
480
481
# File 'opal/opal/corelib/module.rb', line 479

def to_s
  name.to_s
end

#undef_method(symbol) ⇒ Object



483
484
485
486
# File 'opal/opal/corelib/module.rb', line 483

def undef_method(symbol)
  `$opal.add_stub_for(self._proto, "$" + symbol)`
  self
end