Module: Opal
- Defined in:
- opal/opal/corelib/irb.rb,
opal/opal/corelib/helpers.rb
Overview
helpers: type_error, coerce_to backtick_javascript: true
Defined Under Namespace
Modules: IRB
Class Method Summary collapse
- .bridge(constructor, klass) ⇒ Object
- .class_variable_name!(name) ⇒ Object
- .coerce_to!(object, type, method, *args) ⇒ Object
- .coerce_to?(object, type, method, *args) ⇒ Boolean
- .compare(a, b) ⇒ Object
- .const_name!(const_name) ⇒ Object
- .const_name?(const_name) ⇒ Boolean
- .destructure(args) ⇒ Object
-
.inspect(value = undefined) ⇒ String
Performs a safe call to inspect for any value, whether native or Opal-wrapped.
- .instance_variable_name!(name) ⇒ Object
-
.pristine(owner_class, *method_names) ⇒ nil
Mark some methods as pristine in order to apply optimizations when they are still in their original form.
- .respond_to?(obj, method, include_all = false) ⇒ Boolean
- .try_convert(object, type, method) ⇒ Object
Class Method Details
.bridge(constructor, klass) ⇒ Object
5 6 7 |
# File 'opal/opal/corelib/helpers.rb', line 5 def self.bridge(constructor, klass) `Opal.bridge(constructor, klass)` end |
.class_variable_name!(name) ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'opal/opal/corelib/helpers.rb', line 88 def self.class_variable_name!(name) name = ::Opal.coerce_to!(name, ::String, :to_str) if `name.length < 3 || name.slice(0,2) !== '@@'` ::Kernel.raise ::NameError.new("`#{name}' is not allowed as a class variable name", name) end name end |
.coerce_to!(object, type, method, *args) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'opal/opal/corelib/helpers.rb', line 9 def self.coerce_to!(object, type, method, *args) coerced = `$coerce_to(object, type, method, args)` unless type === coerced ::Kernel.raise `$type_error(object, type, method, coerced)` end coerced end |
.coerce_to?(object, type, method, *args) ⇒ Boolean
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'opal/opal/corelib/helpers.rb', line 19 def self.coerce_to?(object, type, method, *args) return unless object.respond_to? method coerced = `$coerce_to(object, type, method, args)` return if coerced.nil? unless type === coerced ::Kernel.raise `$type_error(object, type, method, coerced)` end coerced end |
.compare(a, b) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'opal/opal/corelib/helpers.rb', line 41 def self.compare(a, b) compare = a <=> b if `compare === nil` ::Kernel.raise ::ArgumentError, "comparison of #{a.class} with #{b.class} failed" end compare end |
.const_name!(const_name) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 |
# File 'opal/opal/corelib/helpers.rb', line 108 def self.const_name!(const_name) const_name = ::Opal.coerce_to!(const_name, ::String, :to_str) if defined? ::String %x{ if (!const_name || const_name[0] != const_name[0].toUpperCase()) { #{raise ::NameError, "wrong constant name #{const_name}"} } } const_name end |
.const_name?(const_name) ⇒ Boolean
98 99 100 101 102 103 104 105 106 |
# File 'opal/opal/corelib/helpers.rb', line 98 def self.const_name?(const_name) %x{ if (typeof const_name !== 'string') { #{const_name = ::Opal.coerce_to!(const_name, ::String, :to_str)} } return #{const_name}[0] === #{const_name}[0].toUpperCase() } end |
.destructure(args) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'opal/opal/corelib/helpers.rb', line 51 def self.destructure(args) %x{ if (args.length == 1) { return args[0]; } else if (args.$$is_array) { return args; } else { var args_ary = new Array(args.length); for(var i = 0, l = args_ary.length; i < l; i++) { args_ary[i] = args[i]; } return args_ary; } } end |
.inspect(value = undefined) ⇒ String
Performs a safe call to inspect for any value, whether native or Opal-wrapped.
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 |
# File 'opal/opal/corelib/helpers.rb', line 168 def self.inspect(value = undefined) `var pushed = false` begin %x{ if (value === null) { // JS null value return 'null'; } else if (value === undefined) { // JS undefined value return 'undefined'; } else if (typeof value.$$class === 'undefined') { // JS object / other value that is not bridged return Object.prototype.toString.apply(value); } else if (typeof value.$inspect !== 'function' || value.$inspect.$$stub) { // BasicObject and friends return #{"#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>"} } else if (inspect_stack.indexOf(#{value.__id__}) !== -1) { // inspect recursing inside inspect to find out about the // same object return #{"#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>"} } else { // anything supporting Opal inspect_stack.push(#{value.__id__}); pushed = true; return value.$inspect(); } } nil rescue ::Exception => e # rubocop:disable Lint/RescueException "#<#{`value.$$class`}:0x#{value.__id__.to_s(16)}>" ensure `if (pushed) inspect_stack.pop()` end end |
.instance_variable_name!(name) ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'opal/opal/corelib/helpers.rb', line 78 def self.instance_variable_name!(name) name = ::Opal.coerce_to!(name, ::String, :to_str) unless `/^@[a-zA-Z_][a-zA-Z0-9_]*?$/.test(name)` ::Kernel.raise ::NameError.new("'#{name}' is not allowed as an instance variable name", name) end name end |
.pristine(owner_class, *method_names) ⇒ nil
Mark some methods as pristine in order to apply optimizations when they
are still in their original form. This could probably be moved to
the Opal.def()
JS API, but for now it will stay manual.
146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'opal/opal/corelib/helpers.rb', line 146 def self.pristine(owner_class, *method_names) %x{ var method_name, method; for (var i = method_names.length - 1; i >= 0; i--) { method_name = method_names[i]; method = owner_class.$$prototype[Opal.jsid(method_name)]; if (method && !method.$$stub) { method.$$pristine = true; } } } nil end |
.respond_to?(obj, method, include_all = false) ⇒ Boolean
68 69 70 71 72 73 74 75 76 |
# File 'opal/opal/corelib/helpers.rb', line 68 def self.respond_to?(obj, method, include_all = false) %x{ if (obj == null || !obj.$$class) { return false; } } obj.respond_to?(method, include_all) end |
.try_convert(object, type, method) ⇒ Object
33 34 35 36 37 38 39 |
# File 'opal/opal/corelib/helpers.rb', line 33 def self.try_convert(object, type, method) return object if type === object if object.respond_to? method object.__send__ method end end |