Module: Kernel

Included in:
Object
Defined in:
opal/opal/corelib/kernel.rb,
opal/opal/corelib/binding.rb,
opal/opal/corelib/unsupported.rb,
opal/opal/corelib/unsupported.rb,
opal/opal/corelib/unsupported.rb,
opal/opal/corelib/complex/base.rb,
opal/opal/corelib/kernel/format.rb,
opal/opal/corelib/rational/base.rb

Overview

helpers: coerce_to backtick_javascript: true

Instance Method Summary collapse

Instance Method Details

#!~(obj) ⇒ Object



10
11
12
# File 'opal/opal/corelib/kernel.rb', line 10

def !~(obj)
  !(self =~ obj)
end

#<=>(other) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'opal/opal/corelib/kernel.rb', line 18

def <=>(other)
  %x{
    // set guard for infinite recursion
    self.$$comparable = true;

    var x = #{self == other};

    if (x && x !== nil) {
      return 0;
    }

    return nil;
  }
end

#===(other) ⇒ Object



14
15
16
# File 'opal/opal/corelib/kernel.rb', line 14

def ===(other)
  object_id == other.object_id || self == other
end

#=~(obj) ⇒ Object



6
7
8
# File 'opal/opal/corelib/kernel.rb', line 6

def =~(obj)
  false
end

#Array(object) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'opal/opal/corelib/kernel.rb', line 80

def Array(object)
  %x{
    var coerced;

    if (object === nil) {
      return [];
    }

    if (object.$$is_array) {
      return object;
    }

    coerced = #{::Opal.coerce_to?(object, ::Array, :to_ary)};
    if (coerced !== nil) { return coerced; }

    coerced = #{::Opal.coerce_to?(object, ::Array, :to_a)};
    if (coerced !== nil) { return coerced; }

    return [object];
  }
end

#at_exit(&block) ⇒ Object



102
103
104
105
106
# File 'opal/opal/corelib/kernel.rb', line 102

def at_exit(&block)
  $__at_exit__ ||= []
  $__at_exit__ << block
  block
end

#bindingObject



50
51
52
# File 'opal/opal/corelib/binding.rb', line 50

def binding
  ::Kernel.raise "Opal doesn't support dynamic calls to binding"
end

#caller(start = 1, length = nil) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'opal/opal/corelib/kernel.rb', line 108

def caller(start = 1, length = nil)
  %x{
    var stack, result;

    stack = new Error().$backtrace();
    result = [];

    for (var i = #{start} + 1, ii = stack.length; i < ii; i++) {
      if (!stack[i].match(/runtime\.js/)) {
        result.push(stack[i]);
      }
    }
    if (length != nil) result = result.slice(0, length);
    return result;
  }
end

#caller_locations(*args) ⇒ Object



125
126
127
128
129
# File 'opal/opal/corelib/kernel.rb', line 125

def caller_locations(*args)
  caller(*args).map do |loc|
    ::Thread::Backtrace::Location.new(loc)
  end
end

#catch(tag = nil) ⇒ Object



839
840
841
842
843
844
845
# File 'opal/opal/corelib/kernel.rb', line 839

def catch(tag = nil)
  tag ||= ::Object.new
  yield(tag)
rescue ::UncaughtThrowError => e
  return e.value if e.tag == tag
  ::Kernel.raise
end

#classObject



131
132
133
# File 'opal/opal/corelib/kernel.rb', line 131

def class
  `self.$$class`
end

#clone(freeze: nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'opal/opal/corelib/kernel.rb', line 179

def clone(freeze: nil)
  unless freeze.nil? || freeze == true || freeze == false
    raise ArgumentError, "unexpected value for freeze: #{freeze.class}"
  end

  copy = self.class.allocate

  copy.copy_instance_variables(self)
  copy.copy_singleton_methods(self)
  copy.initialize_clone(self, freeze: freeze)

  if freeze == true || (freeze.nil? && frozen?)
    copy.freeze
  end

  copy
end

#Complex(real, imag = nil) ⇒ Object



2
3
4
5
6
7
8
# File 'opal/opal/corelib/complex/base.rb', line 2

def Complex(real, imag = nil)
  if imag
    Complex.new(real, imag)
  else
    Complex.new(real, 0)
  end
end

#copy_instance_variables(other) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
# File 'opal/opal/corelib/kernel.rb', line 135

def copy_instance_variables(other)
  %x{
    var keys = Object.keys(other), i, ii, name;
    for (i = 0, ii = keys.length; i < ii; i++) {
      name = keys[i];
      if (name.charAt(0) !== '$' && other.hasOwnProperty(name)) {
        self[name] = other[name];
      }
    }
  }
end

#copy_singleton_methods(other) ⇒ Object



147
148
149
150
151
152
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
# File 'opal/opal/corelib/kernel.rb', line 147

def copy_singleton_methods(other)
  %x{
    var i, name, names, length;

    if (other.hasOwnProperty('$$meta') && other.$$meta !== null) {
      var other_singleton_class = Opal.get_singleton_class(other);
      var self_singleton_class = Opal.get_singleton_class(self);
      names = Object.getOwnPropertyNames(other_singleton_class.$$prototype);

      for (i = 0, length = names.length; i < length; i++) {
        name = names[i];
        if (Opal.is_method(name)) {
          self_singleton_class.$$prototype[name] = other_singleton_class.$$prototype[name];
        }
      }

      self_singleton_class.$$const = Object.assign({}, other_singleton_class.$$const);
      Object.setPrototypeOf(
        self_singleton_class.$$prototype,
        Object.getPrototypeOf(other_singleton_class.$$prototype)
      );
    }

    for (i = 0, names = Object.getOwnPropertyNames(other), length = names.length; i < length; i++) {
      name = names[i];
      if (name.charAt(0) === '$' && name.charAt(1) !== '$' && other.hasOwnProperty(name)) {
        self[name] = other[name];
      }
    }
  }
end

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



202
203
204
# File 'opal/opal/corelib/kernel.rb', line 202

def define_singleton_method(name, method = undefined, &block)
  singleton_class.define_method(name, method, &block)
end

#dupObject



206
207
208
209
210
211
212
213
# File 'opal/opal/corelib/kernel.rb', line 206

def dup
  copy = self.class.allocate

  copy.copy_instance_variables(self)
  copy.initialize_dup(self)

  copy
end

#enum_for(method = :each, *args, &block) ⇒ Object Also known as: to_enum



219
220
221
# File 'opal/opal/corelib/kernel.rb', line 219

def enum_for(method = :each, *args, &block)
  ::Enumerator.for(self, method, *args, &block)
end

#equal?(other) ⇒ Boolean

Returns:



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

def equal?(other)
  `self === other`
end

#evalObject



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

def eval(*)
  ::Kernel.raise ::NotImplementedError, "To use Kernel#eval, you must first require 'opal-parser'. "\
                                        "See https://github.com/opal/opal/blob/#{RUBY_ENGINE_VERSION}/docs/opal_parser.md for details."
end

#exit(status = true) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'opal/opal/corelib/kernel.rb', line 227

def exit(status = true)
  $__at_exit__ ||= []

  until $__at_exit__.empty?
    block = $__at_exit__.pop
    block.call
  end

  %x{
    if (status.$$is_boolean) {
      status = status ? 0 : 1;
    } else {
      status = $coerce_to(status, #{::Integer}, 'to_int')
    }

    Opal.exit(status);
  }
  nil
end

#extend(*mods) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'opal/opal/corelib/kernel.rb', line 247

def extend(*mods)
  %x{
    if (mods.length == 0) {
      #{raise ::ArgumentError, 'wrong number of arguments (given 0, expected 1+)'}
    }

    $deny_frozen_access(self);

    var singleton = #{singleton_class};

    for (var i = mods.length - 1; i >= 0; i--) {
      var mod = mods[i];

      if (!mod.$$is_module) {
        #{::Kernel.raise ::TypeError, "wrong argument type #{`mod`.class} (expected Module)"};
      }

      #{`mod`.append_features `singleton`};
      #{`mod`.extend_object self};
      #{`mod`.extended self};
    }
  }

  self
end

#Float(value, exception: true) ⇒ Object



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'opal/opal/corelib/kernel.rb', line 533

def Float(value, exception: true)
  %x{
    var str;

    exception = $truthy(#{exception});

    if (value === nil) {
      if (exception) {
        #{::Kernel.raise ::TypeError, "can't convert nil into Float"}
      } else {
        return nil;
      }
    }

    if (value.$$is_string) {
      str = value.toString();

      str = str.replace(/(\d)_(?=\d)/g, '$1');

      //Special case for hex strings only:
      if (/^\s*[-+]?0[xX][0-9a-fA-F]+\s*$/.test(str)) {
        return #{::Kernel.Integer(`str`)};
      }

      if (!/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/.test(str)) {
        if (exception) {
          #{::Kernel.raise ::ArgumentError, "invalid value for Float(): \"#{value}\""}
        } else {
          return nil;
        }
      }

      return parseFloat(str);
    }

    if (exception) {
      return #{::Opal.coerce_to!(value, ::Float, :to_f)};
    } else {
      return $coerce_to(value, #{::Float}, 'to_f');
    }
  }
end

#format(format_string, *args) ⇒ Object Also known as: sprintf



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
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
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
187
188
189
190
191
192
193
194
195
196
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
222
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
250
251
252
253
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
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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
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
405
406
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
436
437
438
439
440
441
442
443
444
445
446
447
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'opal/opal/corelib/kernel/format.rb', line 5

def format(format_string, *args)
  if args.length == 1 && args[0].respond_to?(:to_ary)
    ary = ::Opal.coerce_to?(args[0], ::Array, :to_ary)
    args = ary.to_a unless ary.nil?
  end

  %x{
    var result = '',
        //used for slicing:
        begin_slice = 0,
        end_slice,
        //used for iterating over the format string:
        i,
        len = format_string.length,
        //used for processing field values:
        arg,
        str,
        //used for processing %g and %G fields:
        exponent,
        //used for keeping track of width and precision:
        width,
        precision,
        //used for holding temporary values:
        tmp_num,
        //used for processing %{} and %<> fileds:
        hash_parameter_key,
        closing_brace_char,
        //used for processing %b, %B, %o, %x, and %X fields:
        base_number,
        base_prefix,
        base_neg_zero_regex,
        base_neg_zero_digit,
        //used for processing arguments:
        next_arg,
        seq_arg_num = 1,
        pos_arg_num = 0,
        //used for keeping track of flags:
        flags,
        FNONE  = 0,
        FSHARP = 1,
        FMINUS = 2,
        FPLUS  = 4,
        FZERO  = 8,
        FSPACE = 16,
        FWIDTH = 32,
        FPREC  = 64,
        FPREC0 = 128;

    function CHECK_FOR_FLAGS() {
      if (flags&FWIDTH) { #{::Kernel.raise ::ArgumentError, 'flag after width'} }
      if (flags&FPREC0) { #{::Kernel.raise ::ArgumentError, 'flag after precision'} }
    }

    function CHECK_FOR_WIDTH() {
      if (flags&FWIDTH) { #{::Kernel.raise ::ArgumentError, 'width given twice'} }
      if (flags&FPREC0) { #{::Kernel.raise ::ArgumentError, 'width after precision'} }
    }

    function GET_NTH_ARG(num) {
      if (num >= args.length) { #{::Kernel.raise ::ArgumentError, 'too few arguments'} }
      return args[num];
    }

    function GET_NEXT_ARG() {
      switch (pos_arg_num) {
      case -1: #{::Kernel.raise ::ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with numbered"} // raise
      case -2: #{::Kernel.raise ::ArgumentError, "unnumbered(#{`seq_arg_num`}) mixed with named"} // raise
      }
      pos_arg_num = seq_arg_num++;
      return GET_NTH_ARG(pos_arg_num - 1);
    }

    function GET_POS_ARG(num) {
      if (pos_arg_num > 0) {
        #{::Kernel.raise ::ArgumentError, "numbered(#{`num`}) after unnumbered(#{`pos_arg_num`})"}
      }
      if (pos_arg_num === -2) {
        #{::Kernel.raise ::ArgumentError, "numbered(#{`num`}) after named"}
      }
      if (num < 1) {
        #{::Kernel.raise ::ArgumentError, "invalid index - #{`num`}$"}
      }
      pos_arg_num = -1;
      return GET_NTH_ARG(num - 1);
    }

    function GET_ARG() {
      return (next_arg === undefined ? GET_NEXT_ARG() : next_arg);
    }

    function READ_NUM(label) {
      var num, str = '';
      for (;; i++) {
        if (i === len) {
          #{::Kernel.raise ::ArgumentError, 'malformed format string - %*[0-9]'}
        }
        if (format_string.charCodeAt(i) < 48 || format_string.charCodeAt(i) > 57) {
          i--;
          num = parseInt(str, 10) || 0;
          if (num > 2147483647) {
            #{::Kernel.raise ::ArgumentError, "#{`label`} too big"}
          }
          return num;
        }
        str += format_string.charAt(i);
      }
    }

    function READ_NUM_AFTER_ASTER(label) {
      var arg, num = READ_NUM(label);
      if (format_string.charAt(i + 1) === '$') {
        i++;
        arg = GET_POS_ARG(num);
      } else {
        arg = GET_NEXT_ARG();
      }
      return #{`arg`.to_int};
    }

    for (i = format_string.indexOf('%'); i !== -1; i = format_string.indexOf('%', i)) {
      str = undefined;

      flags = FNONE;
      width = -1;
      precision = -1;
      next_arg = undefined;

      end_slice = i;

      i++;

      switch (format_string.charAt(i)) {
      case '%':
        begin_slice = i;
        // no-break
      case '':
      case '\n':
      case '\0':
        i++;
        continue;
      }

      format_sequence: for (; i < len; i++) {
        switch (format_string.charAt(i)) {

        case ' ':
          CHECK_FOR_FLAGS();
          flags |= FSPACE;
          continue format_sequence;

        case '#':
          CHECK_FOR_FLAGS();
          flags |= FSHARP;
          continue format_sequence;

        case '+':
          CHECK_FOR_FLAGS();
          flags |= FPLUS;
          continue format_sequence;

        case '-':
          CHECK_FOR_FLAGS();
          flags |= FMINUS;
          continue format_sequence;

        case '0':
          CHECK_FOR_FLAGS();
          flags |= FZERO;
          continue format_sequence;

        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
          tmp_num = READ_NUM('width');
          if (format_string.charAt(i + 1) === '$') {
            if (i + 2 === len) {
              str = '%';
              i++;
              break format_sequence;
            }
            if (next_arg !== undefined) {
              #{::Kernel.raise ::ArgumentError, "value given twice - %#{`tmp_num`}$"}
            }
            next_arg = GET_POS_ARG(tmp_num);
            i++;
          } else {
            CHECK_FOR_WIDTH();
            flags |= FWIDTH;
            width = tmp_num;
          }
          continue format_sequence;

        case '<':
        case '\{':
          closing_brace_char = (format_string.charAt(i) === '<' ? '>' : '\}');
          hash_parameter_key = '';

          i++;

          for (;; i++) {
            if (i === len) {
              #{::Kernel.raise ::ArgumentError, 'malformed name - unmatched parenthesis'}
            }
            if (format_string.charAt(i) === closing_brace_char) {

              if (pos_arg_num > 0) {
                #{::Kernel.raise ::ArgumentError, "named #{`hash_parameter_key`} after unnumbered(#{`pos_arg_num`})"}
              }
              if (pos_arg_num === -1) {
                #{::Kernel.raise ::ArgumentError, "named #{`hash_parameter_key`} after numbered"}
              }
              pos_arg_num = -2;

              if (args[0] === undefined || !args[0].$$is_hash) {
                #{::Kernel.raise ::ArgumentError, 'one hash required'}
              }

              next_arg = #{`args[0]`.fetch(`hash_parameter_key`)};

              if (closing_brace_char === '>') {
                continue format_sequence;
              } else {
                str = next_arg.toString();
                if (precision !== -1) { str = str.slice(0, precision); }
                if (flags&FMINUS) {
                  while (str.length < width) { str = str + ' '; }
                } else {
                  while (str.length < width) { str = ' ' + str; }
                }
                break format_sequence;
              }
            }
            hash_parameter_key += format_string.charAt(i);
          }
          // raise

        case '*':
          i++;
          CHECK_FOR_WIDTH();
          flags |= FWIDTH;
          width = READ_NUM_AFTER_ASTER('width');
          if (width < 0) {
            flags |= FMINUS;
            width = -width;
          }
          continue format_sequence;

        case '.':
          if (flags&FPREC0) {
            #{::Kernel.raise ::ArgumentError, 'precision given twice'}
          }
          flags |= FPREC|FPREC0;
          precision = 0;
          i++;
          if (format_string.charAt(i) === '*') {
            i++;
            precision = READ_NUM_AFTER_ASTER('precision');
            if (precision < 0) {
              flags &= ~FPREC;
            }
            continue format_sequence;
          }
          precision = READ_NUM('precision');
          continue format_sequence;

        case 'd':
        case 'i':
        case 'u':
          arg = #{::Kernel.Integer(`GET_ARG()`)};
          if (arg >= 0) {
            str = arg.toString();
            while (str.length < precision) { str = '0' + str; }
            if (flags&FMINUS) {
              if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              while (str.length < width) { str = str + ' '; }
            } else {
              if (flags&FZERO && precision === -1) {
                while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; }
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              } else {
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
                while (str.length < width) { str = ' ' + str; }
              }
            }
          } else {
            str = (-arg).toString();
            while (str.length < precision) { str = '0' + str; }
            if (flags&FMINUS) {
              str = '-' + str;
              while (str.length < width) { str = str + ' '; }
            } else {
              if (flags&FZERO && precision === -1) {
                while (str.length < width - 1) { str = '0' + str; }
                str = '-' + str;
              } else {
                str = '-' + str;
                while (str.length < width) { str = ' ' + str; }
              }
            }
          }
          break format_sequence;

        case 'b':
        case 'B':
        case 'o':
        case 'x':
        case 'X':
          switch (format_string.charAt(i)) {
          case 'b':
          case 'B':
            base_number = 2;
            base_prefix = '0b';
            base_neg_zero_regex = /^1+/;
            base_neg_zero_digit = '1';
            break;
          case 'o':
            base_number = 8;
            base_prefix = '0';
            base_neg_zero_regex = /^3?7+/;
            base_neg_zero_digit = '7';
            break;
          case 'x':
          case 'X':
            base_number = 16;
            base_prefix = '0x';
            base_neg_zero_regex = /^f+/;
            base_neg_zero_digit = 'f';
            break;
          }
          arg = #{::Kernel.Integer(`GET_ARG()`)};
          if (arg >= 0) {
            str = arg.toString(base_number);
            while (str.length < precision) { str = '0' + str; }
            if (flags&FMINUS) {
              if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              if (flags&FSHARP && arg !== 0) { str = base_prefix + str; }
              while (str.length < width) { str = str + ' '; }
            } else {
              if (flags&FZERO && precision === -1) {
                while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0) - ((flags&FSHARP && arg !== 0) ? base_prefix.length : 0)) { str = '0' + str; }
                if (flags&FSHARP && arg !== 0) { str = base_prefix + str; }
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              } else {
                if (flags&FSHARP && arg !== 0) { str = base_prefix + str; }
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
                while (str.length < width) { str = ' ' + str; }
              }
            }
          } else {
            if (flags&FPLUS || flags&FSPACE) {
              str = (-arg).toString(base_number);
              while (str.length < precision) { str = '0' + str; }
              if (flags&FMINUS) {
                if (flags&FSHARP) { str = base_prefix + str; }
                str = '-' + str;
                while (str.length < width) { str = str + ' '; }
              } else {
                if (flags&FZERO && precision === -1) {
                  while (str.length < width - 1 - (flags&FSHARP ? 2 : 0)) { str = '0' + str; }
                  if (flags&FSHARP) { str = base_prefix + str; }
                  str = '-' + str;
                } else {
                  if (flags&FSHARP) { str = base_prefix + str; }
                  str = '-' + str;
                  while (str.length < width) { str = ' ' + str; }
                }
              }
            } else {
              str = (arg >>> 0).toString(base_number).replace(base_neg_zero_regex, base_neg_zero_digit);
              while (str.length < precision - 2) { str = base_neg_zero_digit + str; }
              if (flags&FMINUS) {
                str = '..' + str;
                if (flags&FSHARP) { str = base_prefix + str; }
                while (str.length < width) { str = str + ' '; }
              } else {
                if (flags&FZERO && precision === -1) {
                  while (str.length < width - 2 - (flags&FSHARP ? base_prefix.length : 0)) { str = base_neg_zero_digit + str; }
                  str = '..' + str;
                  if (flags&FSHARP) { str = base_prefix + str; }
                } else {
                  str = '..' + str;
                  if (flags&FSHARP) { str = base_prefix + str; }
                  while (str.length < width) { str = ' ' + str; }
                }
              }
            }
          }
          if (format_string.charAt(i) === format_string.charAt(i).toUpperCase()) {
            str = str.toUpperCase();
          }
          break format_sequence;

        case 'f':
        case 'e':
        case 'E':
        case 'g':
        case 'G':
          arg = #{::Kernel.Float(`GET_ARG()`)};
          if (arg >= 0 || isNaN(arg)) {
            if (arg === Infinity) {
              str = 'Inf';
            } else {
              switch (format_string.charAt(i)) {
              case 'f':
                str = arg.toFixed(precision === -1 ? 6 : precision);
                break;
              case 'e':
              case 'E':
                str = arg.toExponential(precision === -1 ? 6 : precision);
                break;
              case 'g':
              case 'G':
                str = arg.toExponential();
                exponent = parseInt(str.split('e')[1], 10);
                if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) {
                  str = arg.toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision);
                }
                break;
              }
            }
            if (flags&FMINUS) {
              if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              while (str.length < width) { str = str + ' '; }
            } else {
              if (flags&FZERO && arg !== Infinity && !isNaN(arg)) {
                while (str.length < width - ((flags&FPLUS || flags&FSPACE) ? 1 : 0)) { str = '0' + str; }
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
              } else {
                if (flags&FPLUS || flags&FSPACE) { str = (flags&FPLUS ? '+' : ' ') + str; }
                while (str.length < width) { str = ' ' + str; }
              }
            }
          } else {
            if (arg === -Infinity) {
              str = 'Inf';
            } else {
              switch (format_string.charAt(i)) {
              case 'f':
                str = (-arg).toFixed(precision === -1 ? 6 : precision);
                break;
              case 'e':
              case 'E':
                str = (-arg).toExponential(precision === -1 ? 6 : precision);
                break;
              case 'g':
              case 'G':
                str = (-arg).toExponential();
                exponent = parseInt(str.split('e')[1], 10);
                if (!(exponent < -4 || exponent >= (precision === -1 ? 6 : precision))) {
                  str = (-arg).toPrecision(precision === -1 ? (flags&FSHARP ? 6 : undefined) : precision);
                }
                break;
              }
            }
            if (flags&FMINUS) {
              str = '-' + str;
              while (str.length < width) { str = str + ' '; }
            } else {
              if (flags&FZERO && arg !== -Infinity) {
                while (str.length < width - 1) { str = '0' + str; }
                str = '-' + str;
              } else {
                str = '-' + str;
                while (str.length < width) { str = ' ' + str; }
              }
            }
          }
          if (format_string.charAt(i) === format_string.charAt(i).toUpperCase() && arg !== Infinity && arg !== -Infinity && !isNaN(arg)) {
            str = str.toUpperCase();
          }
          str = str.replace(/([eE][-+]?)([0-9])$/, '$10$2');
          break format_sequence;

        case 'a':
        case 'A':
          // Not implemented because there are no specs for this field type.
          #{::Kernel.raise ::NotImplementedError, '`A` and `a` format field types are not implemented in Opal yet'}
          // raise

        case 'c':
          arg = GET_ARG();
          if (#{`arg`.respond_to?(:to_ary)}) { arg = #{`arg`.to_ary}[0]; }
          if (#{`arg`.respond_to?(:to_str)}) {
            str = #{`arg`.to_str};
          } else {
            str = String.fromCharCode($coerce_to(arg, #{::Integer}, 'to_int'));
          }
          if (str.length !== 1) {
            #{::Kernel.raise ::ArgumentError, '%c requires a character'}
          }
          if (flags&FMINUS) {
            while (str.length < width) { str = str + ' '; }
          } else {
            while (str.length < width) { str = ' ' + str; }
          }
          break format_sequence;

        case 'p':
          str = #{`GET_ARG()`.inspect};
          if (precision !== -1) { str = str.slice(0, precision); }
          if (flags&FMINUS) {
            while (str.length < width) { str = str + ' '; }
          } else {
            while (str.length < width) { str = ' ' + str; }
          }
          break format_sequence;

        case 's':
          str = #{`GET_ARG()`.to_s};
          if (precision !== -1) { str = str.slice(0, precision); }
          if (flags&FMINUS) {
            while (str.length < width) { str = str + ' '; }
          } else {
            while (str.length < width) { str = ' ' + str; }
          }
          break format_sequence;

        default:
          #{::Kernel.raise ::ArgumentError, "malformed format string - %#{`format_string.charAt(i)`}"}
        }
      }

      if (str === undefined) {
        #{::Kernel.raise ::ArgumentError, 'malformed format string - %'}
      }

      result += format_string.slice(begin_slice, end_slice) + str;
      begin_slice = i + 1;
    }

    if (#{$DEBUG} && pos_arg_num >= 0 && seq_arg_num < args.length) {
      #{::Kernel.raise ::ArgumentError, 'too many arguments for format string'}
    }

    return result + format_string.slice(begin_slice);
  }
end

#freezeObject



273
274
275
276
277
278
279
280
281
282
283
# File 'opal/opal/corelib/kernel.rb', line 273

def freeze
  return self if frozen?

  %x{
    if (typeof(self) === "object") {
      $freeze_props(self);
      return $freeze(self);
    }
    return self;
  }
end

#frozen?Boolean

Returns:



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

def frozen?
  %x{
    switch (typeof(self)) {
    case "string":
    case "symbol":
    case "number":
    case "boolean":
      return true;
    case "object":
      return (self.$$frozen || false);
    default:
      return false;
    }
  }
end

#gets(*args) ⇒ Object



301
302
303
# File 'opal/opal/corelib/kernel.rb', line 301

def gets(*args)
  $stdin.gets(*args)
end

#hashObject



305
306
307
# File 'opal/opal/corelib/kernel.rb', line 305

def hash
  __id__
end

#Hash(arg) ⇒ Object



576
577
578
579
580
# File 'opal/opal/corelib/kernel.rb', line 576

def Hash(arg)
  return {} if arg.nil? || arg == []
  return arg if ::Hash === arg
  ::Opal.coerce_to!(arg, ::Hash, :to_hash)
end

#initialize_clone(other, freeze: nil) ⇒ Object



197
198
199
200
# File 'opal/opal/corelib/kernel.rb', line 197

def initialize_clone(other, freeze: nil)
  initialize_copy(other)
  self
end

#initialize_copy(other) ⇒ Object



309
310
# File 'opal/opal/corelib/kernel.rb', line 309

def initialize_copy(other)
end

#initialize_dup(other) ⇒ Object



215
216
217
# File 'opal/opal/corelib/kernel.rb', line 215

def initialize_dup(other)
  initialize_copy(other)
end

#inspectObject



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'opal/opal/corelib/kernel.rb', line 314

def inspect
  ivs = ''
  id = __id__
  if `inspect_stack`.include? id
    ivs = ' ...'
  else
    `inspect_stack` << id
    pushed = true
    instance_variables.each do |i|
      ivar = instance_variable_get(i)
      inspect = Opal.inspect(ivar)
      ivs += " #{i}=#{inspect}"
    end
  end
  "#<#{self.class}:0x#{id.to_s(16)}#{ivs}>"
rescue => e
  "#<#{self.class}:0x#{id.to_s(16)}>"
ensure
  `inspect_stack`.pop if pushed
end

#instance_of?(klass) ⇒ Boolean

Returns:



335
336
337
338
339
340
341
342
343
# File 'opal/opal/corelib/kernel.rb', line 335

def instance_of?(klass)
  %x{
    if (!klass.$$is_class && !klass.$$is_module) {
      #{::Kernel.raise ::TypeError, 'class or module required'};
    }

    return self.$$class === klass;
  }
end

#instance_variable_defined?(name) ⇒ Boolean

Returns:



345
346
347
348
349
# File 'opal/opal/corelib/kernel.rb', line 345

def instance_variable_defined?(name)
  name = ::Opal.instance_variable_name!(name)

  `Opal.hasOwnProperty.call(self, name.substr(1))`
end

#instance_variable_get(name) ⇒ Object



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

def instance_variable_get(name)
  name = ::Opal.instance_variable_name!(name)

  %x{
    var ivar = self[Opal.ivar(name.substr(1))];

    return ivar == null ? nil : ivar;
  }
end

#instance_variable_set(name, value) ⇒ Object



361
362
363
364
365
366
367
# File 'opal/opal/corelib/kernel.rb', line 361

def instance_variable_set(name, value)
  `$deny_frozen_access(self)`

  name = ::Opal.instance_variable_name!(name)

  `self[Opal.ivar(name.substr(1))] = value`
end

#instance_variablesObject



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

def instance_variables
  %x{
    var result = [], name;

    $each_ivar(self, function(name) {
      if (name[name.length-1] === '$') {
        name = name.slice(0, name.length - 1);
      }
      result.push('@' + name);
    });

    return result;
  }
end

#Integer(value, base = undefined, exception: true) ⇒ Object



400
401
402
403
404
405
406
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
436
437
438
439
440
441
442
443
444
445
446
447
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
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/kernel.rb', line 400

def Integer(value, base = undefined, exception: true)
  %x{
    var i, str, base_digits;

    exception = $truthy(#{exception});

    if (!value.$$is_string) {
      if (base !== undefined) {
        if (exception) {
          #{::Kernel.raise ::ArgumentError, 'base specified for non string value'}
        } else {
          return nil;
        }
      }
      if (value === nil) {
        if (exception) {
          #{::Kernel.raise ::TypeError, "can't convert nil into Integer"}
        } else {
          return nil;
        }
      }
      if (value.$$is_number) {
        if (value === Infinity || value === -Infinity || isNaN(value)) {
          if (exception) {
            #{::Kernel.raise ::FloatDomainError, value}
          } else {
            return nil;
          }
        }
        return Math.floor(value);
      }
      if (#{value.respond_to?(:to_int)}) {
        i = #{value.to_int};
        if (Opal.is_a(i, #{::Integer})) {
          return i;
        }
      }
      if (#{value.respond_to?(:to_i)}) {
        i = #{value.to_i};
        if (Opal.is_a(i, #{::Integer})) {
          return i;
        }
      }

      if (exception) {
        #{::Kernel.raise ::TypeError, "can't convert #{value.class} into Integer"}
      } else {
        return nil;
      }
    }

    if (value === "0") {
      return 0;
    }

    if (base === undefined) {
      base = 0;
    } else {
      base = $coerce_to(base, #{::Integer}, 'to_int');
      if (base === 1 || base < 0 || base > 36) {
        if (exception) {
          #{::Kernel.raise ::ArgumentError, "invalid radix #{base}"}
        } else {
          return nil;
        }
      }
    }

    str = value.toLowerCase();

    str = str.replace(/(\d)_(?=\d)/g, '$1');

    str = str.replace(/^(\s*[+-]?)(0[bodx]?)/, function (_, head, flag) {
      switch (flag) {
      case '0b':
        if (base === 0 || base === 2) {
          base = 2;
          return head;
        }
        // no-break
      case '0':
      case '0o':
        if (base === 0 || base === 8) {
          base = 8;
          return head;
        }
        // no-break
      case '0d':
        if (base === 0 || base === 10) {
          base = 10;
          return head;
        }
        // no-break
      case '0x':
        if (base === 0 || base === 16) {
          base = 16;
          return head;
        }
        // no-break
      }
      if (exception) {
        #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
      } else {
        return nil;
      }
    });

    base = (base === 0 ? 10 : base);

    base_digits = '0-' + (base <= 10 ? base - 1 : '9a-' + String.fromCharCode(97 + (base - 11)));

    if (!(new RegExp('^\\s*[+-]?[' + base_digits + ']+\\s*$')).test(str)) {
      if (exception) {
        #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
      } else {
        return nil;
      }
    }

    i = parseInt(str, base);

    if (isNaN(i)) {
      if (exception) {
        #{::Kernel.raise ::ArgumentError, "invalid value for Integer(): \"#{value}\""}
      } else {
        return nil;
      }
    }

    return i;
  }
end

#is_a?(klass) ⇒ Boolean Also known as: kind_of?

Returns:



582
583
584
585
586
587
588
589
590
# File 'opal/opal/corelib/kernel.rb', line 582

def is_a?(klass)
  %x{
    if (!klass.$$is_class && !klass.$$is_module) {
      #{::Kernel.raise ::TypeError, 'class or module required'};
    }

    return Opal.is_a(self, klass);
  }
end

#itselfObject



592
593
594
# File 'opal/opal/corelib/kernel.rb', line 592

def itself
  self
end

#lambda(&block) ⇒ Object



596
597
598
# File 'opal/opal/corelib/kernel.rb', line 596

def lambda(&block)
  `Opal.lambda(block)`
end

#load(file) ⇒ Object



600
601
602
603
# File 'opal/opal/corelib/kernel.rb', line 600

def load(file)
  file = ::Opal.coerce_to!(file, ::String, :to_str)
  `Opal.load(#{file})`
end

#loopObject



605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'opal/opal/corelib/kernel.rb', line 605

def loop
  return enum_for(:loop) { ::Float::INFINITY } unless block_given?

  while true
    begin
      yield
    rescue ::StopIteration => e
      return e.result
    end
  end

  self
end

#method(name) ⇒ Object



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

def method(name)
  %x{
    var meth = self[$jsid(name)];

    if (meth && !meth.$$stub) {
      return #{::Method.new(self, `meth.$$owner || #{self.class}`, `meth`, name)};
    }

    var respond_to_missing = self['$respond_to_missing?'];
    if (respond_to_missing.$$pristine || !respond_to_missing.call(self, name, true)) {
      #{::Kernel.raise ::NameError.new("undefined method `#{name}' for class `#{self.class}'", name)};
    }

    meth = function wrapper() {
      var method_missing = self.$method_missing;
      if (method_missing == null) {
        #{::Kernel.raise ::NameError.new("undefined method `#{name}' for class `#{self.class}'", name)};
      }
      method_missing.$$p = wrapper.$$p;
      return method_missing.apply(self, [name].concat($slice(arguments)));
    };
    meth.$$parameters = [['rest']]
    meth.$$arity = -1;
    return #{::Method.new(self, self.class, `meth`, name)};
  }
end

#methods(all = true) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'opal/opal/corelib/kernel.rb', line 60

def methods(all = true)
  %x{
    if ($truthy(#{all})) {
      return Opal.methods(self);
    } else {
      return Opal.own_methods(self);
    }
  }
end

#nil?Boolean

Returns:



619
620
621
# File 'opal/opal/corelib/kernel.rb', line 619

def nil?
  false
end

#open(*args, &block) ⇒ Object

basic implementation of open, delegate to File.open



852
853
854
# File 'opal/opal/corelib/kernel.rb', line 852

def open(*args, &block)
  ::File.open(*args, &block)
end

#p(*args) ⇒ Object



645
646
647
648
649
# File 'opal/opal/corelib/kernel.rb', line 645

def p(*args)
  args.each { |obj| $stdout.puts obj.inspect }

  args.length <= 1 ? args[0] : args
end


651
652
653
# File 'opal/opal/corelib/kernel.rb', line 651

def print(*strs)
  $stdout.print(*strs)
end

#printf(*args) ⇒ Object



623
624
625
626
627
628
629
630
# File 'opal/opal/corelib/kernel.rb', line 623

def printf(*args)
  return if args.empty?

  io = `args[0].$$is_string` ? $stdout : args.shift
  io.write format(*args)

  nil
end

#private_methods(*methods) ⇒ Object Also known as: protected_methods, private_instance_methods, protected_instance_methods



96
97
98
# File 'opal/opal/corelib/unsupported.rb', line 96

def private_methods(*methods)
  []
end

#proc(&block) ⇒ Object



632
633
634
635
636
637
638
639
# File 'opal/opal/corelib/kernel.rb', line 632

def proc(&block)
  unless block
    ::Kernel.raise ::ArgumentError, 'tried to create Proc object without a block'
  end

  `block.$$is_lambda = false`
  block
end

#public_methods(all = true) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'opal/opal/corelib/kernel.rb', line 70

def public_methods(all = true)
  %x{
    if ($truthy(#{all})) {
      return Opal.methods(self);
    } else {
      return Opal.receiver_methods(self);
    }
  }
end

#puts(*strs) ⇒ Object



641
642
643
# File 'opal/opal/corelib/kernel.rb', line 641

def puts(*strs)
  $stdout.puts(*strs)
end

#raise(exception = undefined, string = nil, backtrace = nil) ⇒ Object Also known as: fail



671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
# File 'opal/opal/corelib/kernel.rb', line 671

def raise(exception = undefined, string = nil, backtrace = nil)
  %x{
    if (exception == null && #{$!} !== nil) {
      throw #{$!};
    }
    if (exception == null) {
      exception = #{::RuntimeError.new ''};
    }
    else if ($respond_to(exception, '$to_str')) {
      exception = #{::RuntimeError.new exception.to_str};
    }
    // using respond_to? and not an undefined check to avoid method_missing matching as true
    else if (exception.$$is_class && $respond_to(exception, '$exception')) {
      exception = #{exception.exception string};
    }
    else if (exception.$$is_exception) {
      // exception is fine
    }
    else {
      exception = #{::TypeError.new 'exception class/object expected'};
    }

    if (backtrace !== nil) {
      exception.$set_backtrace(backtrace);
    }

    if (#{$!} !== nil) {
      Opal.exceptions.push(#{$!});
    }

    #{$!} = exception;

    throw exception;
  }
end

#rand(max = undefined) ⇒ Object



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'opal/opal/corelib/kernel.rb', line 707

def rand(max = undefined)
  %x{
    if (max === undefined) {
      return #{::Random::DEFAULT.rand};
    }

    if (max.$$is_number) {
      if (max < 0) {
        max = Math.abs(max);
      }

      if (max % 1 !== 0) {
        max = max.$to_i();
      }

      if (max === 0) {
        max = undefined;
      }
    }
  }
  ::Random::DEFAULT.rand(max)
end

#Rational(numerator, denominator = 1) ⇒ Object



2
3
4
# File 'opal/opal/corelib/rational/base.rb', line 2

def Rational(numerator, denominator = 1)
  ::Rational.convert(numerator, denominator)
end

#readline(*args) ⇒ Object



655
656
657
# File 'opal/opal/corelib/kernel.rb', line 655

def readline(*args)
  $stdin.readline(*args)
end

#remove_instance_variable(name) ⇒ Object



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

def remove_instance_variable(name)
  name = ::Opal.instance_variable_name!(name)

  %x{
    var key = Opal.ivar(name.substr(1)),
        val;
    if (self.hasOwnProperty(key)) {
      val = self[key];
      delete self[key];
      return val;
    }
  }

  ::Kernel.raise ::NameError, "instance variable #{name} not defined"
end

#require(file) ⇒ Object



752
753
754
755
756
757
758
759
760
761
# File 'opal/opal/corelib/kernel.rb', line 752

def require(file)
  %x{
    // As Object.require refers to Kernel.require once Kernel has been loaded the String
    // class may not be available yet, the coercion requires both  String and Array to be loaded.
    if (typeof #{file} !== 'string' && Opal.String && Opal.Array) {
      #{file = ::Opal.coerce_to!(file, ::String, :to_str) }
    }
    return Opal.require(#{file})
  }
end

#require_relative(file) ⇒ Object



763
764
765
766
767
768
# File 'opal/opal/corelib/kernel.rb', line 763

def require_relative(file)
  ::Opal.try_convert!(file, ::String, :to_str)
  file = ::File.expand_path ::File.join(`Opal.current_file`, '..', file)

  `Opal.require(#{file})`
end

#require_tree(path, autoload: false) ⇒ Object

path should be the full path to be found in registered modules (Opal.modules)



771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'opal/opal/corelib/kernel.rb', line 771

def require_tree(path, autoload: false)
  %x{
    var result = [];

    path = #{::File.expand_path(path)}
    path = Opal.normalize(path);
    if (path === '.') path = '';
    for (var name in Opal.modules) {
      if (#{`name`.start_with?(path)}) {
        if(!#{autoload}) {
          result.push([name, Opal.require(name)]);
        } else {
          result.push([name, true]); // do nothing, delegated to a autoloading
        }
      }
    }

    return result;
  }
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# File 'opal/opal/corelib/kernel.rb', line 730

def respond_to?(name, include_all = false)
  %x{
    var body = self[$jsid(name)];

    if (typeof(body) === "function" && !body.$$stub) {
      return true;
    }

    if (self['$respond_to_missing?'].$$pristine === true) {
      return false;
    } else {
      return #{respond_to_missing?(name, include_all)};
    }
  }
end

#respond_to_missing?(method_name, include_all = false) ⇒ Boolean

Returns:



746
747
748
# File 'opal/opal/corelib/kernel.rb', line 746

def respond_to_missing?(method_name, include_all = false)
  false
end

#singleton_classObject



792
793
794
# File 'opal/opal/corelib/kernel.rb', line 792

def singleton_class
  `Opal.get_singleton_class(self)`
end

#sleep(seconds = nil) ⇒ Object



796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'opal/opal/corelib/kernel.rb', line 796

def sleep(seconds = nil)
  %x{
    if (seconds === nil) {
      #{::Kernel.raise ::TypeError, "can't convert NilClass into time interval"}
    }
    if (!seconds.$$is_number) {
      #{::Kernel.raise ::TypeError, "can't convert #{seconds.class} into time interval"}
    }
    if (seconds < 0) {
      #{::Kernel.raise ::ArgumentError, 'time interval must be positive'}
    }
    var get_time = Opal.global.performance ?
      function() {return performance.now()} :
      function() {return new Date()}

    var t = get_time();
    while (get_time() - t <= seconds * 1000);
    return Math.round(seconds);
  }
end

#srand(seed = Random.new_seed) ⇒ Object



817
818
819
# File 'opal/opal/corelib/kernel.rb', line 817

def srand(seed = Random.new_seed)
  ::Random.srand(seed)
end

#String(str) ⇒ Object



821
822
823
824
# File 'opal/opal/corelib/kernel.rb', line 821

def String(str)
  ::Opal.coerce_to?(str, ::String, :to_str) ||
    ::Opal.coerce_to!(str, ::String, :to_s)
end

#taintObject



47
48
49
50
# File 'opal/opal/corelib/unsupported.rb', line 47

def taint
  `handle_unsupported_feature(ERROR)`
  self
end

#tainted?Boolean

Returns:



57
58
59
60
# File 'opal/opal/corelib/unsupported.rb', line 57

def tainted?
  `handle_unsupported_feature(ERROR)`
  false
end

#tap {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Kernel)

    the object that the method was called on



826
827
828
829
# File 'opal/opal/corelib/kernel.rb', line 826

def tap(&block)
  yield self
  self
end

#throw(tag, obj = nil) ⇒ Object



847
848
849
# File 'opal/opal/corelib/kernel.rb', line 847

def throw(tag, obj = nil)
  ::Kernel.raise ::UncaughtThrowError.new(tag, obj)
end

#to_procObject



831
832
833
# File 'opal/opal/corelib/kernel.rb', line 831

def to_proc
  self
end

#to_sObject



835
836
837
# File 'opal/opal/corelib/kernel.rb', line 835

def to_s
  `Opal.fallback_to_s(self)`
end

#untaintObject



52
53
54
55
# File 'opal/opal/corelib/unsupported.rb', line 52

def untaint
  `handle_unsupported_feature(ERROR)`
  self
end

#warn(*strs, uplevel: nil) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
# File 'opal/opal/corelib/kernel.rb', line 659

def warn(*strs, uplevel: nil)
  if uplevel
    uplevel = ::Opal.coerce_to!(uplevel, ::Integer, :to_str)
    ::Kernel.raise ::ArgumentError, "negative level (#{uplevel})" if uplevel < 0
    location = caller(uplevel + 1, 1).first&.split(':in `')&.first
    location = "#{location}: " if location
    strs = strs.map { |s| "#{location}warning: #{s}" }
  end

  $stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty?
end

#yield_self {|_self| ... } ⇒ Object Also known as: then

Yields:

  • (_self)

Yield Parameters:

  • _self (Kernel)

    the object that the method was called on



856
857
858
859
# File 'opal/opal/corelib/kernel.rb', line 856

def yield_self
  return enum_for(:yield_self) { 1 } unless block_given?
  yield self
end