These are chat archives for opal/opal

9th
Mar 2016
Aaron Quamme
@aquamme
Mar 09 2016 15:39

I'm following the instructions from the guides to set up a gem to run in the browser, but I'm encountering an error when I try to build.

rake aborted!
TypeError: can't convert Array into String
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/paths.rb:22:in `initialize'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/paths.rb:22:in `new'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/paths.rb:22:in `normalize_element'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/normalized_array.rb:55:in `block in normalize_elements'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/normalized_array.rb:54:in `map'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/normalized_array.rb:54:in `normalize_elements'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/normalized_array.rb:42:in `push'
/Users/aquamme/.rvm/gems/ruby-1.9.3-p547/gems/hike-1.2.3/lib/hike/trail.rb:68:in `append_paths'

I added my $LOAD_PATH with Opal.append_path so that Opal could find the dependencies of my gem, but it sounds like that might not be the right approach according to the docs, which state this about append_path: "...Load paths added here should only be paths which contain code targeted at being compiled by Opal." There are gems in there that aren't needed by my gem.

Any hints?

I'm pretty inexperienced with Ruby, so I apologize if I'm flubbing up something obvious here.
Elia Schito
@elia
Mar 09 2016 15:45
@aquamme you should avoid adding $LOAD_PATH paths to opal, as it contains a number of paths that are used for Ruby which aren't compatible, usually you just add the dir containing your application code with append path.
or if you're a library developer you add the path to your library, eg: https://github.com/fazibear/opal-virtual-dom/blob/master/lib/opal-virtual-dom.rb#L3
Also the error says TypeError: can't convert Array into String, looks like you tried to add the whole $LOAD_PATHS array with Opal.append_path which instead only takes a single string
here's a rather simple usage example for Rack: https://github.com/opal/opal/blob/master/examples/rack/config.ru#L17
Aaron Quamme
@aquamme
Mar 09 2016 15:59
Thanks! I'll give that a shot
Aaron Quamme
@aquamme
Mar 09 2016 16:06
To be clear, I'm trying to do a static build of the gem so it can be included in a SPA. It looks like the examples you linked assume Ruby code is running on the server?
Elia Schito
@elia
Mar 09 2016 16:09
then you should require 'opal-parser', which will parse <script type="text/ruby">… tags and add Opal::Compiler and Kernel#eval to Opal running in the browser
on the other hand Opal::Builder and the other stuff that is linked to the file system wouldn't work anyway
Aaron Quamme
@aquamme
Mar 09 2016 16:11
I'll dig into those docs, thanks for pointing me in the right direction!
Elia Schito
@elia
Mar 09 2016 16:13
@aquamme :+1:
please be sure to point out any inconsistency or potential improvement in the docs so we can make 'em better! :)
Aaron Quamme
@aquamme
Mar 09 2016 16:13
Sure thing, glad to provide fresh eyes :smile:
Aaron Quamme
@aquamme
Mar 09 2016 16:21
Hmm. So if I follow the approach you mentioned, that will mean that Ruby code will be compiled at run time? Ideally, I could compile my gem and its dependencies into a static JS file that could be treated like a regular JS library.
Ilya Bylich
@iliabylich
Mar 09 2016 16:29
@elia What do you think about adding something similar to MRI's rb_scan_args to Opal's runtime?
Extracting arguments on the compilation stage becomes more and more complex
Stuff like ->(a,(b,(c,d))){} requires a lot of dynamic code in the beginning of the generated function
Elia Schito
@elia
Mar 09 2016 16:31
@iliabylich agree, the only thing we should stay on the look for is performace, although to me would be ok to optimize for the happy path (eg. I'm ok with worse perfs on def foo a = 1, *b, c, d = 3 etc)
Aaron Quamme
@aquamme
Mar 09 2016 16:54
@elia any thoughts on my previous question?
Elia Schito
@elia
Mar 09 2016 16:55
@aquamme sorry, missed it, reading on
Aaron Quamme
@aquamme
Mar 09 2016 16:56
I'm also not working directly with the gem, I'm trying to create a gem that wraps it. So '<the_gem>_js' is what I'm calling it.
Elia Schito
@elia
Mar 09 2016 16:57
@aquamme so you're right, compiling statically is preferable, and using opal-parser should be avoided but for experiments, etc. I misinterpreted what you attempted to do
@aquamme be aware that opal is already capable to automatically add all "lib" paths from a gem to Opal paths, via Opal.use_gem
but of course more complex gems that access the file system, processes etc won't work that way
Aaron Quamme
@aquamme
Mar 09 2016 17:00
Ah, I didn't see that method. And yeah, that makes sense.
I think I'm okay on that front, my dep tree goes 'my_gem' -> 'gem_to_wrap' -> 'georuby'
Hey, it worked! :smile: Time to test it out
Aaron Quamme
@aquamme
Mar 09 2016 17:06
Ah crud. Georuby has a dynamic require
Elia Schito
@elia
Mar 09 2016 17:07
try setting the to ignore mode on Opal::Config
Aaron Quamme
@aquamme
Mar 09 2016 17:14
Opal::Config.config[:dynamic_require_severity] = :ignore ?
Elia Schito
@elia
Mar 09 2016 17:19
Opal::Config.dynamic_require_severity = :ignore
:)
Aaron Quamme
@aquamme
Mar 09 2016 17:19
The issue persists with :ignore and :none, and I can see why
Elia Schito
@elia
Mar 09 2016 17:21
yeah, you can try putting a geo_ruby/simple_features.rb in one of your paths and override it
elia @elia is leaving office, will read replies later
Aaron Quamme
@aquamme
Mar 09 2016 17:22
Oh, clever! I suppose I'll have to figure out how to get at the files that the original file was requiring... hmm
Aaron Quamme
@aquamme
Mar 09 2016 18:01
Thanks @elia , your suggestion worked. The JS engine is choking on one of the lines in the compiled file -- I traced it back to this line
Aaron Quamme
@aquamme
Mar 09 2016 18:06
So that's a line continuation - does Opal not support that?
Elia Schito
@elia
Mar 09 2016 18:11
I think they're supported, but maybe not in that parsing context , please open an issue for that
Aaron Quamme
@aquamme
Mar 09 2016 18:19
Opened opal/opal#1388. Thanks!
Aaron Quamme
@aquamme
Mar 09 2016 18:44
Might have another issue to report - File.dirname(__FILE__) returns a string in irb, but the compiled code returns an array with a string in it.
Aaron Quamme
@aquamme
Mar 09 2016 21:52
Nevermind, looks like it was opal/opal@12b8a49
*fixed on master
Aaron Quamme
@aquamme
Mar 09 2016 22:30
How do you require Opal's stdlib?
Aaron Quamme
@aquamme
Mar 09 2016 22:35
Looks like you just require 'thelibname', no namespacing
Aaron Quamme
@aquamme
Mar 09 2016 23:22
@elia , is it possible to monkey patch Opal code? I'd like to include the fix from the commit I mentioned above in my gem, but this doesn't work:
require 'opal'
...
class File
  def dirname path
   #etc
  end
end