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:



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'opal/opal/corelib/module.rb', line 193

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

check for constant within current scope if inherit is true or self is Object, will also check ancestors

Raises:



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'opal/opal/corelib/module.rb', line 218

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:



241
242
243
244
245
# File 'opal/opal/corelib/module.rb', line 241

def const_missing(const)
  name = `#{self}._name`

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

#const_set(name, value) ⇒ Object

Raises:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'opal/opal/corelib/module.rb', line 247

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

when self is Module (or Class), implement 1st form:

  • global constants, classes and modules in global scope when self is not Module (or Class), implement 2nd form:
  • constants, classes and modules scoped to instance


187
188
189
# File 'opal/opal/corelib/module.rb', line 187

def constants
  `#{self}._scope.constants`
end

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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'opal/opal/corelib/module.rb', line 262

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 null;
  }
end

#include(*mods) ⇒ Object



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

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

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

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

    return #{self};
  }
end

#included(mod) ⇒ Object



349
350
# File 'opal/opal/corelib/module.rb', line 349

def included(mod)
end

#instance_method(name) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
# File 'opal/opal/corelib/module.rb', line 315

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



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'opal/opal/corelib/module.rb', line 327

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:



388
389
390
391
392
393
# File 'opal/opal/corelib/module.rb', line 388

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



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

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

    var block_self = block._s, result;

    block._s = null;
    result = block.call(#{self});
    block._s = block_self;

    return result;
  }
end

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



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'opal/opal/corelib/module.rb', line 370

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



395
396
397
398
399
400
401
402
403
404
405
# File 'opal/opal/corelib/module.rb', line 395

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



407
408
409
410
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
# File 'opal/opal/corelib/module.rb', line 407

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



440
441
442
# File 'opal/opal/corelib/module.rb', line 440

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

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

Returns:



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

def private_method_defined?(obj)
  false
end

#publicObject Also known as: private, protected



437
438
# File 'opal/opal/corelib/module.rb', line 437

def public(*)
end

#remove_class_variableObject



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

def remove_class_variable(*)
end

#remove_const(name) ⇒ Object



460
461
462
463
464
465
466
# File 'opal/opal/corelib/module.rb', line 460

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

#remove_method(name) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
# File 'opal/opal/corelib/module.rb', line 284

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



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

def to_s
  name.to_s
end

#undef_method(symbol) ⇒ Object



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

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