These are chat archives for opal/opal

2nd
Apr 2018
Kadu Diógenes
@cerdiogenes
Apr 02 2018 01:32
is there any way to run code like this:
`'use strict';`
require "opal"

def entrypoint(event, context, callback)
  callback(nil, event)
end

`module.exports.opal = Opal.Object.$entrypoint`
I get 'TypeError: self.$callback is not a function'
Jamie Gaskins
@jgaskins
Apr 02 2018 02:34
@cerdiogenes Try callback.call(nil, event) instead of callback(nil, event)
Kadu Diógenes
@cerdiogenes
Apr 02 2018 12:45
@jgaskins it's appear to work (altough the request isn't returning what I expect)
from the second request and on I get the error: Cannot read property '$new' of undefined","at Object.Opal.bridge
this is the line that throws the error: throw Opal.ArgumentError.$new("already bridged");
I suspect Opal is being loaded twice and this is causing trouble
Kadu Diógenes
@cerdiogenes
Apr 02 2018 12:50
My intention is to use the resulting code in AWS Lambda
Mitch VanDuyn
@catmando
Apr 02 2018 12:57
@cerdiogenes very interested in this. Perhaps can help. Let me know how it goes
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:00
@catmando this is the simplest code I can think about and I can't make it work: https://gitter.im/opal/opal?at=5ac1883e92f5d62057839bc4
any suggestion?
Mitch VanDuyn
@catmando
Apr 02 2018 13:01
What goes wrong?
oh I see sorry..
sure its not quite ruby :-)
try something like callback.call(nil, event)
sadly calling callbacks is the one place ruby is more verbose than javascript
sorry again... I was reading this on my phone, and I see I am behind the times...
Mitch VanDuyn
@catmando
Apr 02 2018 13:06
okay now on a real computer... one second.
Unless something has changed in Opal 0.11 you will get an explicity "Opal Loaded Twice" error if Opal is loaded twice.
Mitch VanDuyn
@catmando
Apr 02 2018 13:11
the problem above is quite weird (I guess you can see that :-)
ArgumentError is simply defined by Opal so not sure how it can't be there. Is there any way you are loading two different versions of Opal some way? I doubt that.
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:15
No, just one version
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:20
I'm using the serverless framework
I defined the following
functions:
  opal:
    handler: build.opal
    events: # All events associated with this function
      - http:
          path: /opal
          method: get
Mitch VanDuyn
@catmando
Apr 02 2018 13:22
@cerdiogenes I am just guessing but I wonder if you should wrap entrypoint in another class. I have never used an entrypoint directly on the Object class.
`'use strict';`
require "opal"

class MyEntryPoints
  def self.entrypoint(event, context, callback)
    callback.call(nil, event)
  end
end

`module.exports.opal = Opal.MyEntryPoints.$entrypoint`
I must say it does seem like Opal is getting loaded twice...
i.e. works (kindof) on first request, but on second request...
breaks
Mitch VanDuyn
@catmando
Apr 02 2018 13:28
does aws lambda give you some way to separate out some setup code (i.e. so the require 'opal') could be moved elsewhere... also I assume it breaks completely without the require 'opal'
do you have some kind of rake task or script that is pre-compiling the opal code into a JS file?
what does that look like?
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:34
don't know if it's possible to run a setup code
Mitch VanDuyn
@catmando
Apr 02 2018 13:37
What does your build script look like?
Going for Abit BTW
Mitch VanDuyn
@catmando
Apr 02 2018 13:38
Offline
Offline
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:39
I will keep trying, if I had any progress I inform
Mitch VanDuyn
@catmando
Apr 02 2018 13:56
huh... so you don't have a script that runs your Opal code through the compiler resulting in a JS file?
something like this https://github.com/ruby-hyperloop/hyperloop-js/blob/master/Rakefile (although that has more than you need )
Kadu Diógenes
@cerdiogenes
Apr 02 2018 13:58
yes, I have
require 'opal'

desc "Build our app to build.js"
task :build do
  Opal.append_path "app"
  File.binwrite "build.js", Opal::Builder.build("application").to_s
end
Mitch VanDuyn
@catmando
Apr 02 2018 14:02
poking around hyperloop code, I don't think you require 'opal' on the opal side.
so perhaps this is causing the problem
I believe the builder will take care of it.
btw what does application.js / application.rb look like.
?
also... a strategy I use is to build the application.js file, then just load it on a static page on your local box, then just play with it in the console. That gets rid of some of the surrounding potential issues.
Kadu Diógenes
@cerdiogenes
Apr 02 2018 14:19
application.rb is like you suggested: https://gitter.im/opal/opal?at=5ac22e782b9dfdbc3a4981c7
Mitch VanDuyn
@catmando
Apr 02 2018 14:22
leave off the require 'opal'
I think the compiler already takes care of that some how
Kadu Diógenes
@cerdiogenes
Apr 02 2018 14:23
no deal: Opal is not defined
Mitch VanDuyn
@catmando
Apr 02 2018 14:23
hmmm
poking around
yes you are correct...
so I think your original assumption must be right... its loading the code twice...
this is where my knowledge of AWS Lambda stops... you could try adding some puts statements or console.logs right in your application.js file... and try to see what its doing.
Kadu Diógenes
@cerdiogenes
Apr 02 2018 14:29
do you know if there is any particularity of the resulting code be running in nodejs instead of the browser?
Mitch VanDuyn
@catmando
Apr 02 2018 14:31
here is an experment:
re: node-js I don't think so (@adamcreekroad has built oapl electron apps which I think use node-js so he might know)
you could do this experiment:
after that application.js file is generated hand patch it like this:
if (Opal.MyEntryPoints.$entrypoint) {
   //... contents of application.js
}  // in otherwords the definition will only be run once regardless of what AWS tries to do...
Adam
@adamcreekroad
Apr 02 2018 14:50
I have no knowledge on AWS lambda, but I was able to load that build.js file into nodejs no problem and use Opal
Kadu Diógenes
@cerdiogenes
Apr 02 2018 15:04
@catmando the problem is really opal being loaded twice
I changed the generated file using your suggested approach and it worked
the only strange thing is that I need to change callback.$call(nil, event) to callback.$call(null, event)
nil to null
Mitch VanDuyn
@catmando
Apr 02 2018 15:17
Who is defining the call back?
If it's some js code then it wants null
Kadu Diógenes
@cerdiogenes
Apr 02 2018 15:18
the nodejs environment calls the entrypoint
Mitch VanDuyn
@catmando
Apr 02 2018 15:18
But is the call back yours or theirs?
Kadu Diógenes
@cerdiogenes
Apr 02 2018 15:18
is there a way to write null in opal?
theirs
Mitch VanDuyn
@catmando
Apr 02 2018 15:19
Null is a js concept
Kadu Diógenes
@cerdiogenes
Apr 02 2018 15:20
yep, is there a way to express it in opal?
Mitch VanDuyn
@catmando
Apr 02 2018 15:21
Wrap it in back ticks

Can't do o it on my phone!
Back ticks escape s to js
Kadu Diógenes
@cerdiogenes
Apr 02 2018 15:22
was what I though
thanks a lot for the attention @catmando!
callback.call(`null`, event)
worked like a charm :smile:
Mitch VanDuyn
@catmando
Apr 02 2018 15:26
And u can add the if wrapper to your build script!
Mitch VanDuyn
@catmando
Apr 02 2018 18:52
fyi if you look at how opal-rails generates each compiled file there is a similar check around each file to make sure Opal does not get loaded over and over again
Elia Schito
@elia
Apr 02 2018 20:53

@cerdiogenes be aware of the fact that callback.call in opal mimics the ruby method and not the JS one, if you want to call the latter you have to either use backticks

`callback.call(null, #{event})`

or .JS

callback.JS.call(`null`, event)

an alternative could be to wrap the call, so that self isn't changed (you need to try this, but will probably work):

require "opal"

entrypoint = ->(event, context, callback) {
  callback(event)
}

`module`.JS[:exports].JS[:opal] = entrypoint
Kadu Diógenes
@cerdiogenes
Apr 02 2018 22:01
@elia I didn't understand your warning about callback.call, it's appear to be working just fine
I'm in doubt about using a Hash
This
response = {
  statusCode: 200,
  body: 'nothing'.to_json
}
translates to this {"$$smap":{"statusCode":200,"body":"\"nada\""},"$$map":{},"$$keys":["statusCode","body"]}
strange, I put a console.log and get what I expected
only in the callback it's wrong
I think that it have something to do with your warning @elia :smile:
eiko
@eiko
Apr 02 2018 22:15
I just tried the Getting Started tutorials for Opal Sinatra, Opal-Sprockets, Opal-Jquery and Opal-Browser, and I couldn't get any of them to work. I get various errors like "uninitialized constant Opal::Tilt" or "no such file opal/jquery" or "Opal::Server deprecated." Is there a more up-to-date tutorial I can use, or is Getting Started a bumpy road for everyone, or am I just daft?
Elia Schito
@elia
Apr 02 2018 23:21
@eiko I’ll check them tomorrow and let you know, they’re probably broken because of changes in the latest release, maybe I’ll update the with explicit version numbers