Module: Kernel
- Defined in:
- opal/opal/corelib/kernel.rb
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #===(other) ⇒ Object
- #=~(obj) ⇒ Object
- #Array(object, *args, &block) ⇒ Object
- #at_exit(&block) ⇒ Object
-
#caller ⇒ Object
Opal does not support #caller, but we stub it as an empty array to not break dependant libs.
- #class ⇒ Object
- #clone ⇒ Object
- #copy_instance_variables(other) ⇒ Object
- #define_singleton_method(name, &body) ⇒ Object
- #dup ⇒ Object
- #enum_for(method = :each, *args, &block) ⇒ Object (also: #to_enum)
- #equal?(other) ⇒ Boolean
- #extend(*mods) ⇒ Object
- #Float(value) ⇒ Object
- #format(format, *args) ⇒ Object (also: #sprintf)
- #freeze ⇒ Object
- #frozen? ⇒ Boolean
- #hash ⇒ Object
- #initialize_clone(other) ⇒ Object
- #initialize_copy(other) ⇒ Object
- #initialize_dup(other) ⇒ Object
- #inspect ⇒ Object
- #instance_of?(klass) ⇒ Boolean
- #instance_variable_defined?(name) ⇒ Boolean
- #instance_variable_get(name) ⇒ Object
- #instance_variable_set(name, value) ⇒ Object
- #instance_variables ⇒ Object
- #Integer(value, base = nil) ⇒ Object
- #is_a?(klass) ⇒ Boolean (also: #kind_of?)
- #lambda(&block) ⇒ Object
- #load(file) ⇒ Object
- #loop(&block) ⇒ Object
- #method(name) ⇒ Object
- #method_missing(symbol, *args, &block) ⇒ Object
- #methods(all = true) ⇒ Object
- #nil? ⇒ Boolean
- #p(*args) ⇒ Object
- #print(*strs) ⇒ Object
- #printf(*args) ⇒ Object
- #private_methods ⇒ Object (also: #private_instance_methods)
- #proc(&block) ⇒ Object
- #puts(*strs) ⇒ Object
- #raise(exception = undefined, string = undefined) ⇒ Object (also: #fail)
- #rand(max = undefined) ⇒ Object (also: #srand)
- #require(file) ⇒ Object
- #require_relative(file) ⇒ Object
-
#require_tree(path) ⇒ Object
path
should be the full path to be found in registered modules (Opal.modules
). - #respond_to?(name, include_all = false) ⇒ Boolean
- #respond_to_missing?(method_name) ⇒ Boolean
- #singleton_class ⇒ Object
- #String(str) ⇒ Object
- #taint ⇒ Object (also: #untaint)
- #tainted? ⇒ Boolean
- #tap {|_self| ... } ⇒ Object
- #to_proc ⇒ Object
- #to_s ⇒ Object
- #warn(*strs) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
2 3 4 |
# File 'opal/opal/corelib/kernel.rb', line 2 def method_missing(symbol, *args, &block) raise NoMethodError, "undefined method `#{symbol}' for #{inspect}" end |
Instance Method Details
#<=>(other) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'opal/opal/corelib/kernel.rb', line 14 def <=>(other) %x{ if (#{self == other}) { return 0; } return nil; } end |
#===(other) ⇒ Object
10 11 12 |
# File 'opal/opal/corelib/kernel.rb', line 10 def ===(other) self == other end |
#=~(obj) ⇒ Object
6 7 8 |
# File 'opal/opal/corelib/kernel.rb', line 6 def =~(obj) false end |
#Array(object, *args, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'opal/opal/corelib/kernel.rb', line 57 def Array(object, *args, &block) %x{ if (object == null || object === nil) { return []; } else if (#{object.respond_to? :to_ary}) { return #{object.to_ary}; } else if (#{object.respond_to? :to_a}) { return #{object.to_a}; } else { return [object]; } } end |
#at_exit(&block) ⇒ Object
74 75 76 77 |
# File 'opal/opal/corelib/kernel.rb', line 74 def at_exit(&block) $__at_exit__ ||= [] $__at_exit__ << block end |
#caller ⇒ Object
Opal does not support #caller, but we stub it as an empty array to not break dependant libs
81 82 83 |
# File 'opal/opal/corelib/kernel.rb', line 81 def caller [] end |
#class ⇒ Object
85 86 87 |
# File 'opal/opal/corelib/kernel.rb', line 85 def class `self.$$class` end |
#clone ⇒ Object
99 100 101 102 103 104 105 106 |
# File 'opal/opal/corelib/kernel.rb', line 99 def clone copy = self.class.allocate copy.copy_instance_variables(self) copy.initialize_clone(self) copy end |
#copy_instance_variables(other) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'opal/opal/corelib/kernel.rb', line 89 def copy_instance_variables(other) %x{ for (var name in other) { if (name.charAt(0) !== '$') { self[name] = other[name]; } } } end |
#define_singleton_method(name, &body) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'opal/opal/corelib/kernel.rb', line 112 def define_singleton_method(name, &body) unless body raise ArgumentError, "tried to create Proc object without a block" end %x{ var jsid = '$' + name; body.$$jsid = name; body.$$s = null; body.$$def = body; #{singleton_class}.$$proto[jsid] = body; return self; } end |
#dup ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'opal/opal/corelib/kernel.rb', line 129 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
142 143 144 |
# File 'opal/opal/corelib/kernel.rb', line 142 def enum_for(method = :each, *args, &block) Enumerator.for(self, method, *args, &block) end |
#equal?(other) ⇒ Boolean
148 149 150 |
# File 'opal/opal/corelib/kernel.rb', line 148 def equal?(other) `self === other` end |
#extend(*mods) ⇒ Object
152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'opal/opal/corelib/kernel.rb', line 152 def extend(*mods) %x{ var singleton = #{singleton_class}; for (var i = mods.length - 1; i >= 0; i--) { var mod = mods[i]; #{`mod`.append_features `singleton`}; #{`mod`.extended self}; } } self end |
#Float(value) ⇒ Object
390 391 392 393 394 395 396 397 398 |
# File 'opal/opal/corelib/kernel.rb', line 390 def Float(value) if String === value `parseFloat(value)` elsif value.respond_to? :to_f value.to_f else raise TypeError, "can't convert #{value.class} into Float" end end |
#format(format, *args) ⇒ Object Also known as: sprintf
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 |
# File 'opal/opal/corelib/kernel.rb', line 167 def format(format, *args) %x{ var idx = 0; return format.replace(/%(\d+\$)?([-+ 0]*)(\d*|\*(\d+\$)?)(?:\.(\d*|\*(\d+\$)?))?([cspdiubBoxXfgeEG])|(%%)/g, function(str, idx_str, flags, width_str, w_idx_str, prec_str, p_idx_str, spec, escaped) { if (escaped) { return '%'; } var width, prec, is_integer_spec = ("diubBoxX".indexOf(spec) != -1), is_float_spec = ("eEfgG".indexOf(spec) != -1), prefix = '', obj; if (width_str === undefined) { width = undefined; } else if (width_str.charAt(0) == '*') { var w_idx = idx++; if (w_idx_str) { w_idx = parseInt(w_idx_str, 10) - 1; } width = #{`args[w_idx]`.to_i}; } else { width = parseInt(width_str, 10); } if (!prec_str) { prec = is_float_spec ? 6 : undefined; } else if (prec_str.charAt(0) == '*') { var p_idx = idx++; if (p_idx_str) { p_idx = parseInt(p_idx_str, 10) - 1; } prec = #{`args[p_idx]`.to_i}; } else { prec = parseInt(prec_str, 10); } if (idx_str) { idx = parseInt(idx_str, 10) - 1; } switch (spec) { case 'c': obj = args[idx]; if (obj.$$is_string) { str = obj.charAt(0); } else { str = String.fromCharCode(#{`obj`.to_i}); } break; case 's': str = #{`args[idx]`.to_s}; if (prec !== undefined) { str = str.substr(0, prec); } break; case 'p': str = #{`args[idx]`.inspect}; if (prec !== undefined) { str = str.substr(0, prec); } break; case 'd': case 'i': case 'u': str = #{`args[idx]`.to_i}.toString(); break; case 'b': case 'B': str = #{`args[idx]`.to_i}.toString(2); break; case 'o': str = #{`args[idx]`.to_i}.toString(8); break; case 'x': case 'X': str = #{`args[idx]`.to_i}.toString(16); break; case 'e': case 'E': str = #{`args[idx]`.to_f}.toExponential(prec); break; case 'f': str = #{`args[idx]`.to_f}.toFixed(prec); break; case 'g': case 'G': str = #{`args[idx]`.to_f}.toPrecision(prec); break; } idx++; if (is_integer_spec || is_float_spec) { if (str.charAt(0) == '-') { prefix = '-'; str = str.substr(1); } else { if (flags.indexOf('+') != -1) { prefix = '+'; } else if (flags.indexOf(' ') != -1) { prefix = ' '; } } } if (is_integer_spec && prec !== undefined) { if (str.length < prec) { str = #{'0' * `prec - str.length`} + str; } } var total_len = prefix.length + str.length; if (width !== undefined && total_len < width) { if (flags.indexOf('-') != -1) { str = str + #{' ' * `width - total_len`}; } else { var pad_char = ' '; if (flags.indexOf('0') != -1) { str = #{'0' * `width - total_len`} + str; } else { prefix = #{' ' * `width - total_len`} + prefix; } } } var result = prefix + str; if ('XEG'.indexOf(spec) != -1) { result = result.toUpperCase(); } return result; }); } end |
#freeze ⇒ Object
296 297 298 299 |
# File 'opal/opal/corelib/kernel.rb', line 296 def freeze @___frozen___ = true self end |
#frozen? ⇒ Boolean
301 302 303 |
# File 'opal/opal/corelib/kernel.rb', line 301 def frozen? @___frozen___ || false end |
#hash ⇒ Object
305 306 307 |
# File 'opal/opal/corelib/kernel.rb', line 305 def hash `[self.$$class.$$name,#{`self.$$class`.__id__},#{__id__}].join(':')` end |
#initialize_clone(other) ⇒ Object
108 109 110 |
# File 'opal/opal/corelib/kernel.rb', line 108 def initialize_clone(other) initialize_copy(other) end |
#initialize_copy(other) ⇒ Object
309 310 |
# File 'opal/opal/corelib/kernel.rb', line 309 def initialize_copy(other) end |
#initialize_dup(other) ⇒ Object
138 139 140 |
# File 'opal/opal/corelib/kernel.rb', line 138 def initialize_dup(other) initialize_copy(other) end |
#inspect ⇒ Object
312 313 314 |
# File 'opal/opal/corelib/kernel.rb', line 312 def inspect to_s end |
#instance_of?(klass) ⇒ Boolean
316 317 318 |
# File 'opal/opal/corelib/kernel.rb', line 316 def instance_of?(klass) `self.$$class === klass` end |
#instance_variable_defined?(name) ⇒ Boolean
320 321 322 |
# File 'opal/opal/corelib/kernel.rb', line 320 def instance_variable_defined?(name) `Opal.hasOwnProperty.call(self, name.substr(1))` end |
#instance_variable_get(name) ⇒ Object
324 325 326 327 328 329 330 |
# File 'opal/opal/corelib/kernel.rb', line 324 def instance_variable_get(name) %x{ var ivar = self[name.substr(1)]; return ivar == null ? nil : ivar; } end |
#instance_variable_set(name, value) ⇒ Object
332 333 334 |
# File 'opal/opal/corelib/kernel.rb', line 332 def instance_variable_set(name, value) `self[name.substr(1)] = value` end |
#instance_variables ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'opal/opal/corelib/kernel.rb', line 336 def instance_variables %x{ var result = []; for (var name in self) { if (name.charAt(0) !== '$') { if (name !== '$$class' && name !== '$$id') { result.push('@' + name); } } } return result; } end |
#Integer(value, base = nil) ⇒ Object
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 |
# File 'opal/opal/corelib/kernel.rb', line 352 def Integer(value, base = nil) if String === value if value.empty? raise ArgumentError, "invalid value for Integer: (empty string)" end return `parseInt(#{value}, #{base || `undefined`})` end if base raise ArgumentError "base is only valid for String values" end case value when Integer value when Float if value.nan? or value.infinite? raise FloatDomainError, "unable to coerce #{value} to Integer" end value.to_int when NilClass raise TypeError, "can't convert nil into Integer" else if value.respond_to? :to_int value.to_int elsif value.respond_to? :to_i value.to_i else raise TypeError, "can't convert #{value.class} into Integer" end end end |
#is_a?(klass) ⇒ Boolean Also known as: kind_of?
400 401 402 |
# File 'opal/opal/corelib/kernel.rb', line 400 def is_a?(klass) `Opal.is_a(self, klass)` end |
#lambda(&block) ⇒ Object
406 407 408 409 410 |
# File 'opal/opal/corelib/kernel.rb', line 406 def lambda(&block) `block.$$is_lambda = true` block end |
#load(file) ⇒ Object
412 413 414 415 |
# File 'opal/opal/corelib/kernel.rb', line 412 def load(file) file = Opal.coerce_to!(file, String, :to_str) `Opal.load(Opal.normalize_loadable_path(#{file}))` end |
#loop(&block) ⇒ Object
417 418 419 420 421 422 423 424 425 426 427 |
# File 'opal/opal/corelib/kernel.rb', line 417 def loop(&block) %x{ while (true) { if (block() === $breaker) { return $breaker.$v; } } } self end |
#method(name) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'opal/opal/corelib/kernel.rb', line 24 def method(name) %x{ var meth = self['$' + name]; if (!meth || meth.$$stub) { #{raise NameError, "undefined method `#{name}' for class `#{self.class}'"}; } return #{Method.new(self, `meth`, name)}; } end |
#methods(all = true) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'opal/opal/corelib/kernel.rb', line 36 def methods(all = true) %x{ var methods = []; for (var key in self) { if (key[0] == "$" && typeof(self[key]) === "function") { if (all == false || all === nil) { if (!Opal.hasOwnProperty.call(self, key)) { continue; } } if (self[key].$$stub === undefined) { methods.push(key.substr(1)); } } } return methods; } end |
#p(*args) ⇒ Object
461 462 463 464 465 |
# File 'opal/opal/corelib/kernel.rb', line 461 def p(*args) args.each { |obj| $stdout.puts obj.inspect } args.length <= 1 ? args[0] : args end |
#print(*strs) ⇒ Object
467 468 469 |
# File 'opal/opal/corelib/kernel.rb', line 467 def print(*strs) $stdout.print(*strs) end |
#printf(*args) ⇒ Object
435 436 437 438 439 440 441 |
# File 'opal/opal/corelib/kernel.rb', line 435 def printf(*args) if args.length > 0 print format(*args) end nil end |
#private_methods ⇒ Object Also known as: private_instance_methods
443 444 445 |
# File 'opal/opal/corelib/kernel.rb', line 443 def private_methods(*) [] end |
#proc(&block) ⇒ Object
448 449 450 451 452 453 454 455 |
# File 'opal/opal/corelib/kernel.rb', line 448 def proc(&block) unless block raise ArgumentError, "tried to create Proc object without a block" end `block.$$is_lambda = false` block end |
#puts(*strs) ⇒ Object
457 458 459 |
# File 'opal/opal/corelib/kernel.rb', line 457 def puts(*strs) $stdout.puts(*strs) end |
#raise(exception = undefined, string = undefined) ⇒ Object Also known as: fail
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# File 'opal/opal/corelib/kernel.rb', line 475 def raise(exception = undefined, string = undefined) %x{ if (exception == null && #$!) { exception = #$!; } else if (exception.$$is_string) { exception = #{RuntimeError.new exception}; } else if (!#{exception.is_a? Exception}) { exception = #{exception.new string}; } #{$! = exception}; throw exception; } end |
#rand(max = undefined) ⇒ Object Also known as: srand
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
# File 'opal/opal/corelib/kernel.rb', line 494 def rand(max = undefined) %x{ if (max === undefined) { return Math.random(); } else if (max.$$is_range) { var arr = #{max.to_a}; return arr[#{rand(`arr.length`)}]; } else { return Math.floor(Math.random() * Math.abs(#{Opal.coerce_to max, Integer, :to_int})); } } end |
#require(file) ⇒ Object
529 530 531 532 |
# File 'opal/opal/corelib/kernel.rb', line 529 def require(file) file = Opal.coerce_to!(file, String, :to_str) `Opal.require(Opal.normalize_loadable_path(#{file}))` end |
#require_relative(file) ⇒ Object
534 535 536 537 538 539 |
# File 'opal/opal/corelib/kernel.rb', line 534 def require_relative(file) Opal.try_convert!(file, String, :to_str) file = File. File.join(`Opal.current_file`, '..', file) `Opal.require(Opal.normalize_loadable_path(#{file}))` end |
#require_tree(path) ⇒ Object
path
should be the full path to be found in registered modules (Opal.modules
)
542 543 544 545 546 547 548 549 550 551 552 553 554 |
# File 'opal/opal/corelib/kernel.rb', line 542 def require_tree(path) path = File.(path) %x{ for (var name in Opal.modules) { if (#{`name`.start_with?(path)}) { Opal.require(name); } } } nil end |
#respond_to?(name, include_all = false) ⇒ Boolean
511 512 513 514 515 516 517 518 519 520 521 522 523 |
# File 'opal/opal/corelib/kernel.rb', line 511 def respond_to?(name, include_all = false) return true if respond_to_missing?(name) %x{ var body = self['$' + name]; if (typeof(body) === "function" && !body.$$stub) { return true; } } false end |
#respond_to_missing?(method_name) ⇒ Boolean
525 526 527 |
# File 'opal/opal/corelib/kernel.rb', line 525 def respond_to_missing?(method_name) false end |
#singleton_class ⇒ Object
559 560 561 |
# File 'opal/opal/corelib/kernel.rb', line 559 def singleton_class %x{Opal.get_singleton_class(self)} end |
#String(str) ⇒ Object
567 568 569 |
# File 'opal/opal/corelib/kernel.rb', line 567 def String(str) `String(str)` end |
#taint ⇒ Object Also known as: untaint
571 572 573 |
# File 'opal/opal/corelib/kernel.rb', line 571 def taint self end |
#tainted? ⇒ Boolean
575 576 577 |
# File 'opal/opal/corelib/kernel.rb', line 575 def tainted? false end |
#tap {|_self| ... } ⇒ Object
579 580 581 582 |
# File 'opal/opal/corelib/kernel.rb', line 579 def tap(&block) yield self self end |
#to_proc ⇒ Object
584 585 586 |
# File 'opal/opal/corelib/kernel.rb', line 584 def to_proc self end |
#to_s ⇒ Object
588 589 590 |
# File 'opal/opal/corelib/kernel.rb', line 588 def to_s "#<#{self.class}:0x#{__id__.to_s(16)}>" end |
#warn(*strs) ⇒ Object
471 472 473 |
# File 'opal/opal/corelib/kernel.rb', line 471 def warn(*strs) $stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty? end |