|
| 1 | +module Nashorn |
| 2 | + module JS |
| 3 | + |
| 4 | + JSObject.module_eval do |
| 5 | + |
| 6 | + def [](key) |
| 7 | + Nashorn.to_rb key.is_a?(Fixnum) ? getSlot(key) : getMember(key.to_s) |
| 8 | + end |
| 9 | + |
| 10 | + def []=(key, value) |
| 11 | + name.is_a?(Fixnum) ? setSlot(key, value) : setMember(key.to_s, value) |
| 12 | + end |
| 13 | + |
| 14 | + # enumerate the key value pairs contained in this javascript object. e.g. |
| 15 | + # |
| 16 | + # eval_js("{foo: 'bar', baz: 'bang'}").each do |key,value| |
| 17 | + # puts "#{key} -> #{value} " |
| 18 | + # end |
| 19 | + # |
| 20 | + # outputs foo -> bar baz -> bang |
| 21 | + # |
| 22 | + def each |
| 23 | + each_raw { |key, val| yield key, Nashorn.to_rb(val) } |
| 24 | + end |
| 25 | + |
| 26 | + def each_key |
| 27 | + each_raw { |key, val| yield key } |
| 28 | + end |
| 29 | + |
| 30 | + def each_value |
| 31 | + each_raw { |key, val| yield Nashorn.to_rb(val) } |
| 32 | + end |
| 33 | + |
| 34 | + def each_raw |
| 35 | + for id in keySet do |
| 36 | + yield id, getMember(id) |
| 37 | + end |
| 38 | + end |
| 39 | + |
| 40 | + def keys |
| 41 | + keySet.to_a |
| 42 | + end |
| 43 | + |
| 44 | + def values |
| 45 | + values.to_a |
| 46 | + end |
| 47 | + |
| 48 | + # Converts the native object to a hash. This isn't really a stretch since it's |
| 49 | + # pretty much a hash in the first place. |
| 50 | + def to_h |
| 51 | + hash = {} |
| 52 | + each do |key, val| |
| 53 | + hash[key] = val.is_a?(JSObject) && ! val.equal?(self) ? val.to_h : val |
| 54 | + end |
| 55 | + hash |
| 56 | + end |
| 57 | + |
| 58 | + # def ==(other) |
| 59 | + # equivalentValues(other) == true # JS == |
| 60 | + # end |
| 61 | + # |
| 62 | + # def eql?(other) |
| 63 | + # self.class == other.class && self.==(other) |
| 64 | + # end |
| 65 | + |
| 66 | + # Convert this javascript object into a json string. |
| 67 | + def to_json(*args) |
| 68 | + to_h.to_json(*args) |
| 69 | + end |
| 70 | + |
| 71 | + # Delegate methods to JS object if possible when called from Ruby. |
| 72 | + def method_missing(name, *args) |
| 73 | + name_str = name.to_s |
| 74 | + if name_str.end_with?('=') && args.size == 1 # writer -> JS put |
| 75 | + self[ name_str[0...-1] ] = args[0] |
| 76 | + else |
| 77 | + if hasMember(name_str) && property = getMember(name_str) |
| 78 | + if property.is_a?(JSObject) && property.isFunction |
| 79 | + js_args = Nashorn.args_to_js(args) |
| 80 | + Nashorn.to_rb property.__call__(self, js_args) |
| 81 | + else |
| 82 | + if args.size > 0 |
| 83 | + raise ArgumentError, "can't call '#{name_str}' with args: #{args.inspect} as it's a property" |
| 84 | + end |
| 85 | + Nashorn.to_rb property |
| 86 | + end |
| 87 | + else |
| 88 | + super |
| 89 | + end |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + # function : |
| 94 | + |
| 95 | + alias_method :__call__, :call |
| 96 | + |
| 97 | + # make JavaScript functions callable Ruby style e.g. `fn.call('42')` |
| 98 | + # |
| 99 | + # NOTE: That invoking #call does not have the same semantics as |
| 100 | + # JavaScript's Function#call but rather as Ruby's Method#call ! |
| 101 | + # Use #apply or #bind before calling to achieve the same effect. |
| 102 | + def call(*args) |
| 103 | + this = nil |
| 104 | + Nashorn.to_rb __call__ this, Nashorn.args_to_js(args) |
| 105 | + rescue JS::NashornException => e |
| 106 | + raise Nashorn::JSError.new(e) |
| 107 | + end |
| 108 | + |
| 109 | + # bind a JavaScript function into the given (this) context |
| 110 | + #def bind(this, *args) |
| 111 | + # args = Nashorn.args_to_js(args) |
| 112 | + # Rhino::JS::BoundFunction.new(self, Nashorn.to_js(this), args) |
| 113 | + #end |
| 114 | + |
| 115 | + # use JavaScript functions constructors from Ruby as `fn.new` |
| 116 | + def new(*args) |
| 117 | + construct Nashorn.args_to_js(args) |
| 118 | + rescue JS::NashornException => e |
| 119 | + raise Nashorn::JSError.new(e) |
| 120 | + end |
| 121 | + |
| 122 | + # apply a function with the given context and (optional) arguments |
| 123 | + # e.g. `fn.apply(obj, 1, 2)` |
| 124 | + # |
| 125 | + # NOTE: That #call from Ruby does not have the same semantics as |
| 126 | + # JavaScript's Function#call but rather as Ruby's Method#call ! |
| 127 | + def apply(this, *args) |
| 128 | + __call__ Nashorn.to_js(this), Nashorn.args_to_js(args) |
| 129 | + rescue JS::NashornException => e |
| 130 | + raise Nashorn::JSError.new(e) |
| 131 | + end |
| 132 | + alias_method :methodcall, :apply # V8::Function compatibility |
| 133 | + |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments