Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: opal/opal
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1c0d981d5415^
Choose a base ref
...
head repository: opal/opal
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 631d5c454bf2
Choose a head ref
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Sep 26, 2015

  1. Copy tweaks

    elia committed Sep 26, 2015
    Copy the full SHA
    1c0d981 View commit details
  2. Copy the full SHA
    6bb13f2 View commit details
  3. Add Kernel#eval to unsupported

    Will fail with proper instructions on how to enable the feature.
    elia committed Sep 26, 2015
    Copy the full SHA
    631d5c4 View commit details
Showing with 63 additions and 3 deletions.
  1. +1 −1 README.md
  2. +2 −2 docs/configuring_gems.md
  3. +53 −0 docs/opal_parser.md
  4. +7 −0 opal/corelib/unsupported.rb
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ Then from the terminal

```bash
$ opal --compile app.rb > app.js # The Opal runtime is included by default but
# but can be skipped with the --no-opal CLI flag.
# but can be skipped with the --no-opal flag
```

The resulting JavaScript file can be used normally from an HTML page:
4 changes: 2 additions & 2 deletions docs/configuring_gems.md
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@

To configure a gem to run in Opal the gem will need a couple of things:

1. The opal gem running on a server (so the ruby code can get compiled to .js.)
2. The opal search path has to know to look for your gem when it is required.
1. The opal gem running on a server (so the ruby code can get compiled to JavaScript).
2. The Opal search path has to know to look for your gem when it is required.

This is done by having the following 2 lines in your outermost .rb file:

53 changes: 53 additions & 0 deletions docs/opal_parser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Parsing Ruby from JavaScript with `opal-parser`

Generally the is best to percompile Ruby source files to JavaScript serverside but sometimes may become useful to be able to compile Ruby to JavaScript directly from JS.

Opal is able to compile its – pure Ruby – compiler to JavaScript (how cool is that!). The whole compiler chain is available in the `stdlib` as `opal-parser`.

```ruby
require 'opal-parser'
```


## Features

### `Kernel#eval`

`opal-parser` provides a partial implementation of `Kernel#eval` that only support the first parameter (String) and refuse any passed `binding`, `lineno` or `filename`.

Example:

```ruby
require 'opal-parser'
eval "puts 'hello world!'"
```


### `Kernel#require_remote`

Will fetch a remote URL (by means of a sync `XMLHttpRequest`) and evaluate its contents as Ruby code.

Example:

```ruby
require 'opal-parser'
require_remote 'http://pastie.org/pastes/10444960/text'
HelloWorld.new.say_hello!
```


### `Opal.compile()` and `Opal.eval()` (JavaScript)

After requiring `opal-parser` both `Opal.compile()` and `Opal.eval()` functions are added to the JavaScript API.

`Opal.compile(string, options)` (JavaScript) will forward the call to `Opal.compile` (Ruby) converting options from a plain JS object to a Ruby Hash.
`Opal.eval(string)` will compile the given code to JavaScript and then pass it to the [native `eval()` function][eval].


### Support for `<script type="text/ruby">`

When `opal-parser` is required it will search the page for any `<script>` tag with type `text/ruby`.
If an `src` attribute is present will fetch and eval the file with `Kernel#require_remote` otherwise it will get the script tags contents and eval them with `Kernel#eval`.


[eval]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
7 changes: 7 additions & 0 deletions opal/corelib/unsupported.rb
Original file line number Diff line number Diff line change
@@ -207,3 +207,10 @@ def private_methods(*)

alias private_instance_methods private_methods
end

module Kernel
def eval(*)
raise NotImplementedError, "To use Kernel#eval, you must first require 'opal-parser'. "\
"See https://github.com/opal/opal/blob/#{RUBY_ENGINE_VERSION}/docs/opal_parser.md for details."
end
end