These are chat archives for opal/opal

29th
May 2017
Bernhard Weichel
@bwl21
May 29 2017 19:04
HI, I observe, that a String is sometimes not represented as a JS primitive but a ruby class. What could be the reason?
screenshot_867.jpg
Does that mean, that whenver I pass a string to a native library, I should do it with .to_n ?
Bernhard Weichel
@bwl21
May 29 2017 19:22
I have copied parts of activesupport/lib/active_support/core_ext/object/deep_dup.rb , there is the root of the problem. I do a deep_dup on a hash, which makes the strings in that hash to bei String instead of JS primitives.
The same happens with Numerical etc.
Bernhard Weichel
@bwl21
May 29 2017 19:33
deep_dup is defined in
class Object
  # Returns a deep copy of object if it's duplicable. If it's
  # not duplicable, returns +self+.
  #
  #   object = Object.new
  #   dup    = object.deep_dup
  #   dup.instance_variable_set(:@a, 1)
  #
  #   object.instance_variable_defined?(:@a) # => false
  #   dup.instance_variable_defined?(:@a)    # => true
  def deep_dup
    duplicable? ? dup : self
  end
end

where dupliactable? is something like

class Numeric
  # Numbers are not duplicable:
  #
  #  3.duplicable? # => false
  #  3.dup         # => TypeError: can't dup Integer
  def duplicable?
    false
  end
end

The problem went away after I added deep_dup to String and Numeric directly. But i don't not what I am doing.

## this is necessary for opal ...
class String
  def deep_dup
    self.to_n
  end
end

class Numeric
  def deep_dup
    self.to_n
  end
end

Any advice is welcome.

Jamie Gaskins
@jgaskins
May 29 2017 20:09
Screen Shot 2017-05-29 at 4.06.27 PM.png
@bwl21 ^^ It's the difference between a string literal and new String(string_literal). It's one of the quirks of JS — they even have a different typeof.
The same thing happens with numbers: 123 vs new Number(123). I think it's some legacy stuff from when JS was originally intended to "work like Java".
If you call .valueOf() on it, it'll cast itself down to the primitive type, which might be why .to_n fixes it.
Jamie Gaskins
@jgaskins
May 29 2017 20:16
I ran into this same problem with Clearwater because it checks typeof before adding stuff to a DOM node. Some of the math stuff wasn't working because I was getting back objects instead of numbers — but only sometimes. I assume it's something in the bridging stuff between Ruby classes and JS primitives, but I didn't have time to look into it so I just ran everything through .valueOf().
Bernhard Weichel
@bwl21
May 29 2017 20:36

@jgaskins do I get it right that I should for example change

      `#{@native_jspdf}.text(#{nx}, #{ny}, #{text}, #{flags})`

to

        `#{@native_jspdf}.text(#{nx}.valuelOf(), #{ny}.valueOf(), #{text}.valueOf(), #{flags}).valueOf()`
Bernhard Weichel
@bwl21
May 29 2017 20:43
I basically have reimplemented one of my classes based on deep_merge, deep_dup etc. So I think I should find an implementation which does not require a change in the clients of that class.. I guess I stick with the to_n in the code I took from active_support untl I am facing another problem :-)
Thanks