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
- #exit(status = true) ⇒ Object
- #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 |
#exit(status = true) ⇒ Object
152 153 154 155 156 157 |
# File 'opal/opal/corelib/kernel.rb', line 152 def exit(status = true) $__at_exit__.reverse.each(&:call) if $__at_exit__ status = 0 if `status === true` # it's in JS because it can be null/undef `Opal.exit(status);` nil end |
#extend(*mods) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'opal/opal/corelib/kernel.rb', line 159 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
397 398 399 400 401 402 403 404 405 |
# File 'opal/opal/corelib/kernel.rb', line 397 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
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 |
# File 'opal/opal/corelib/kernel.rb', line 174 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
303 304 305 306 |
# File 'opal/opal/corelib/kernel.rb', line 303 def freeze @___frozen___ = true self end |
#frozen? ⇒ Boolean
308 309 310 |
# File 'opal/opal/corelib/kernel.rb', line 308 def frozen? @___frozen___ || false end |
#hash ⇒ Object
312 313 314 |
# File 'opal/opal/corelib/kernel.rb', line 312 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
316 317 |
# File 'opal/opal/corelib/kernel.rb', line 316 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
319 320 321 |
# File 'opal/opal/corelib/kernel.rb', line 319 def inspect to_s end |
#instance_of?(klass) ⇒ Boolean
323 324 325 |
# File 'opal/opal/corelib/kernel.rb', line 323 def instance_of?(klass) `self.$$class === klass` end |
#instance_variable_defined?(name) ⇒ Boolean
327 328 329 |
# File 'opal/opal/corelib/kernel.rb', line 327 def instance_variable_defined?(name) `Opal.hasOwnProperty.call(self, name.substr(1))` end |
#instance_variable_get(name) ⇒ Object
331 332 333 334 335 336 337 |
# File 'opal/opal/corelib/kernel.rb', line 331 def instance_variable_get(name) %x{ var ivar = self[name.substr(1)]; return ivar == null ? nil : ivar; } end |
#instance_variable_set(name, value) ⇒ Object
339 340 341 |
# File 'opal/opal/corelib/kernel.rb', line 339 def instance_variable_set(name, value) `self[name.substr(1)] = value` end |
#instance_variables ⇒ Object
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 |
# File 'opal/opal/corelib/kernel.rb', line 343 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
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 |
# File 'opal/opal/corelib/kernel.rb', line 359 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?
407 408 409 |
# File 'opal/opal/corelib/kernel.rb', line 407 def is_a?(klass) `Opal.is_a(self, klass)` end |
#lambda(&block) ⇒ Object
413 414 415 416 417 |
# File 'opal/opal/corelib/kernel.rb', line 413 def lambda(&block) `block.$$is_lambda = true` block end |
#load(file) ⇒ Object
419 420 421 422 |
# File 'opal/opal/corelib/kernel.rb', line 419 def load(file) file = Opal.coerce_to!(file, String, :to_str) `Opal.load(Opal.normalize_loadable_path(#{file}))` end |
#loop(&block) ⇒ Object
424 425 426 427 428 429 430 431 432 433 434 |
# File 'opal/opal/corelib/kernel.rb', line 424 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
468 469 470 471 472 |
# File 'opal/opal/corelib/kernel.rb', line 468 def p(*args) args.each { |obj| $stdout.puts obj.inspect } args.length <= 1 ? args[0] : args end |
#print(*strs) ⇒ Object
474 475 476 |
# File 'opal/opal/corelib/kernel.rb', line 474 def print(*strs) $stdout.print(*strs) end |
#printf(*args) ⇒ Object
442 443 444 445 446 447 448 |
# File 'opal/opal/corelib/kernel.rb', line 442 def printf(*args) if args.length > 0 print format(*args) end nil end |
#private_methods ⇒ Object Also known as: private_instance_methods
450 451 452 |
# File 'opal/opal/corelib/kernel.rb', line 450 def private_methods(*) [] end |
#proc(&block) ⇒ Object
455 456 457 458 459 460 461 462 |
# File 'opal/opal/corelib/kernel.rb', line 455 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
464 465 466 |
# File 'opal/opal/corelib/kernel.rb', line 464 def puts(*strs) $stdout.puts(*strs) end |
#raise(exception = undefined, string = undefined) ⇒ Object Also known as: fail
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'opal/opal/corelib/kernel.rb', line 482 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
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'opal/opal/corelib/kernel.rb', line 501 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
536 537 538 539 |
# File 'opal/opal/corelib/kernel.rb', line 536 def require(file) file = Opal.coerce_to!(file, String, :to_str) `Opal.require(Opal.normalize_loadable_path(#{file}))` end |
#require_relative(file) ⇒ Object
541 542 543 544 545 546 |
# File 'opal/opal/corelib/kernel.rb', line 541 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
)
549 550 551 552 553 554 555 556 557 558 559 560 561 |
# File 'opal/opal/corelib/kernel.rb', line 549 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
518 519 520 521 522 523 524 525 526 527 528 529 530 |
# File 'opal/opal/corelib/kernel.rb', line 518 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
532 533 534 |
# File 'opal/opal/corelib/kernel.rb', line 532 def respond_to_missing?(method_name) false end |
#singleton_class ⇒ Object
566 567 568 |
# File 'opal/opal/corelib/kernel.rb', line 566 def singleton_class %x{Opal.get_singleton_class(self)} end |
#String(str) ⇒ Object
574 575 576 |
# File 'opal/opal/corelib/kernel.rb', line 574 def String(str) `String(str)` end |
#taint ⇒ Object Also known as: untaint
578 579 580 |
# File 'opal/opal/corelib/kernel.rb', line 578 def taint self end |
#tainted? ⇒ Boolean
582 583 584 |
# File 'opal/opal/corelib/kernel.rb', line 582 def tainted? false end |
#tap {|_self| ... } ⇒ Object
586 587 588 589 |
# File 'opal/opal/corelib/kernel.rb', line 586 def tap(&block) yield self self end |
#to_proc ⇒ Object
591 592 593 |
# File 'opal/opal/corelib/kernel.rb', line 591 def to_proc self end |
#to_s ⇒ Object
595 596 597 |
# File 'opal/opal/corelib/kernel.rb', line 595 def to_s "#<#{self.class}:0x#{__id__.to_s(16)}>" end |
#warn(*strs) ⇒ Object
478 479 480 |
# File 'opal/opal/corelib/kernel.rb', line 478 def warn(*strs) $stderr.puts(*strs) unless $VERBOSE.nil? || strs.empty? end |