These are chat archives for opal/opal

16th
Jan 2018
G. Gibson
@mistergibson
Jan 16 2018 00:10
@elia I'm having trouble adding methods to class String - is this unsupported?
Elia Schito
@elia
Jan 16 2018 00:11
should work fine, unless you're trying to mutate the string, inheriting from String it's more of a hackery but still supported
G. Gibson
@mistergibson
Jan 16 2018 00:12
no, it mainly is for some methods that do regex matching
Elia Schito
@elia
Jan 16 2018 00:12
@mistergibson can you paste your attempt here?
G. Gibson
@mistergibson
Jan 16 2018 00:12
yeah, I'll whip up a gist for you
Elia Schito
@elia
Jan 16 2018 00:14
Looks good, what kind of error are you getting?
G. Gibson
@mistergibson
Jan 16 2018 00:15
This: uncaught exception: TypeError: can't convert String into Kernel::String (String#to_str gives String
I was surprised
Elia Schito
@elia
Jan 16 2018 00:17
Seems to be working here, p "AsdfAasdf".split_camelcase resulted in ["Asdf", "Aasdf"]
G. Gibson
@mistergibson
Jan 16 2018 00:17
I wonder what the deal is?
it won't even accept my method defs
it must be in the way I'm building the bootstrap
Elia Schito
@elia
Jan 16 2018 00:19
maybe it's some weird interaction due to all those requires, try removing most of them and see if it works, if it does then you can start looking for the one to blame
G. Gibson
@mistergibson
Jan 16 2018 00:19
sure
G. Gibson
@mistergibson
Jan 16 2018 00:25
Hrm ... loaded it right after 'opal/full' and got the same error - never makes it to the other requires.
Elia Schito
@elia
Jan 16 2018 00:25
have you tried just with 'opal'?
G. Gibson
@mistergibson
Jan 16 2018 00:26
That's my next stop :)
Same error
so .... require 'opal' ... then the .rb file on require_remote gives the error.
Elia Schito
@elia
Jan 16 2018 00:28
gotta go to sleep :crescent_moon: , I'll read tomorrow :wave:
G. Gibson
@mistergibson
Jan 16 2018 00:28
ok ... sleep tight :)
G. Gibson
@mistergibson
Jan 16 2018 01:13
@elia -- when you get up: When I exclude 'opal-parser' I don't get the error.
Jan Biedermann
@janbiedermann
Jan 16 2018 02:43

@elia i think there is a bug in js inlining, sometimes not generating a necessary return
example opal-active support String.camelize
non-working, generated by 0.11:

return (Opal.defn(self, '$camelize', TMP_String_camelize_2 = function $$camelize(first_letter) {
      var self = this;

      if (first_letter == null) {
        first_letter = "upper";
      }
      self.$underscore().replace(/(^|_)([^_]+)/g, function(match, pre, word, index) {
      var capitalize = first_letter === "upper" || index > 0;
      return capitalize ? word.substr(0,1).toUpperCase()+word.substr(1) : word;
    })

manual workaround:

    return (Opal.defn(self, '$camelize', TMP_String_camelize_2 = function $$camelize(first_letter) {
      var self = this;

      if (first_letter == null) {
        first_letter = "upper";
      }
      return self.$underscore().replace(/(^|_)([^_]+)/g, function(match, pre, word, index) {
      var capitalize = first_letter === "upper" || index > 0;
      return capitalize ? word.substr(0,1).toUpperCase()+word.substr(1) : word;
    })
note the added return in the middle
original code:
  def camelize(first_letter = :upper)
    `#{underscore}.replace(/(^|_)([^_]+)/g, function(match, pre, word, index) {
      var capitalize = #{first_letter} === #{:upper} || index > 0;
      return capitalize ? word.substr(0,1).toUpperCase()+word.substr(1) : word;
    })`
  end
workaround:
  def camelize(first_letter = :upper)
    `return #{underscore}.replace(/(^|_)([^_]+)/g, function(match, pre, word, index) {
      var capitalize = #{first_letter} === #{:upper} || index > 0;
      return capitalize ? word.substr(0,1).toUpperCase()+word.substr(1) : word;
    })`
  end
Jan Biedermann
@janbiedermann
Jan 16 2018 02:57
now, where would i have to look for fixing this in the compiler?
Jan Biedermann
@janbiedermann
Jan 16 2018 03:22
or how can i debug such things?
Simon George
@sfcgeorge
Jan 16 2018 09:30
That native inlining issue @janbiedermann mentions is reported here opal/opal-activesupport#16 - Another workaround is rather than adding a return you can just call .to_s on the backtick string. I don't know why that would change what is happening. Does it force a native value to be cast into an Opal value or something?
Elia Schito
@elia
Jan 16 2018 09:33

@janbiedermann try adding a semicolon before closing the backticks, that said to debug this kind of stuff I often do

opal -cEO -e "def foo; `return 123, function(){}` end"

and

opal --sexp  -e "def foo; `return 123, function(){}` end"
Jan Biedermann
@janbiedermann
Jan 16 2018 14:19
@sfcgeorge when you add the the .to_s the compiler adds the returnabove, thats why it works with .to_s
Simon George
@sfcgeorge
Jan 16 2018 14:21
@janbiedermann Interesting. That almost makes it seem like expected behaviour. If you show you intend to use the value then it returns it. Seems unhelpful though, especially if you don't know what the return type will be.
Jan Biedermann
@janbiedermann
Jan 16 2018 14:30
@elia adding a ;doesnt help
Jan Biedermann
@janbiedermann
Jan 16 2018 19:49

@iliabylich changing https://github.com/opal/opal/blob/ee50768d368959b33a77b58028a90ec9bded64a1/lib/opal/compiler.rb#L436 to

          if multiline
            # xstr starts with interpolation
            # then it must contain js_return inside
            if first_child.type == :begin
              s(:js_return, sexp)
            else
              sexp
            end
          else
            if first_child.type == :str

helps the above troubled case, also tests pass, though i am not sure, if that is the right thing to do there, looks too specific, or would you be ok with that? then i would provide a PR

G. Gibson
@mistergibson
Jan 16 2018 21:26
Question: is there any plan to support REXML in the StandardLib? I notice it is missing - compared to MRI.