Permalink
Browse files

Initial cleanup commit

  • Loading branch information...
Adam Beynon
Adam Beynon committed May 2, 2011
0 parents commit 6a7055424fb6789fbf11fb3af36702c7f5470e4c
Showing with 7,120 additions and 0 deletions.
  1. +75 −0 LICENSE
  2. +197 −0 README.md
  3. +21 −0 examples/browser/README.md
  4. +10 −0 examples/browser/Rakefile
  5. +46 −0 examples/browser/browser.js
  6. +46 −0 examples/browser/browser.rb
  7. +11 −0 examples/browser/index.html
  8. +21 −0 examples/dev_tools/index.html
  9. +10 −0 examples/rquery/Rakefile
  10. +30 −0 examples/rquery/index.html
  11. +37 −0 examples/rquery/rquery_demo.js
  12. +37 −0 examples/rquery/rquery_demo.rb
  13. +109 −0 lib/core.rb
  14. +1,440 −0 lib/core/array.rb
  15. +43 −0 lib/core/basic_object.rb
  16. +49 −0 lib/core/class.rb
  17. +26 −0 lib/core/dir.rb
  18. +33 −0 lib/core/enumerable.rb
  19. +78 −0 lib/core/error.rb
  20. +81 −0 lib/core/false_class.rb
  21. +62 −0 lib/core/file.rb
  22. +765 −0 lib/core/hash.rb
  23. +248 −0 lib/core/kernel.rb
  24. +33 −0 lib/core/match_data.rb
  25. +86 −0 lib/core/module.rb
  26. +54 −0 lib/core/nil_class.rb
  27. +411 −0 lib/core/numeric.rb
  28. +6 −0 lib/core/object.rb
  29. +56 −0 lib/core/proc.rb
  30. +17 −0 lib/core/range.rb
  31. +61 −0 lib/core/regexp.rb
  32. +375 −0 lib/core/string.rb
  33. +38 −0 lib/core/symbol.rb
  34. +8 −0 lib/core/top_self.rb
  35. +41 −0 lib/core/true_class.rb
  36. +31 −0 spec/core/array/append_spec.rb
  37. +29 −0 spec/core/array/assoc_spec.rb
  38. +37 −0 spec/core/array/at_spec.rb
  39. +22 −0 spec/core/array/clear_spec.rb
  40. +27 −0 spec/core/array/collect_bang_spec.rb
  41. +27 −0 spec/core/array/collect_spec.rb
  42. +41 −0 spec/core/array/compact_spec.rb
  43. +15 −0 spec/core/array/concat_spec.rb
  44. +14 −0 spec/core/array/constructor_spec.rb
  45. +9 −0 spec/core/array/each_spec.rb
  46. +4 −0 spec/core/array/element_reference_spec.rb
  47. +35 −0 spec/core/array/first_spec.rb
  48. +9 −0 spec/core/array/include_spec.rb
  49. +6 −0 spec/core/array/join_spec.rb
  50. +51 −0 spec/core/array/last_spec.rb
  51. +6 −0 spec/core/array/length_spec.rb
  52. +33 −0 spec/core/array/map_spec.rb
  53. +6 −0 spec/core/array/reverse_spec.rb
  54. +7 −0 spec/core/array/select_spec.rb
  55. +7 −0 spec/core/builtin_constants/builtin_constants_spec.rb
  56. +10 −0 spec/core/false/and_spec.rb
  57. +6 −0 spec/core/false/inspect_spec.rb
  58. +10 −0 spec/core/false/or_spec.rb
  59. +6 −0 spec/core/false/to_s_spec.rb
  60. +10 −0 spec/core/false/xor_spec.rb
  61. +19 −0 spec/core/file/join_spec.rb
  62. +32 −0 spec/core/hash/assoc_spec.rb
  63. 0 spec/core/kernel/instance_eval_spec.rb
  64. +24 −0 spec/core/kernel/loop_spec.rb
  65. 0 spec/core/kernel/raise_spec.rb
  66. +28 −0 spec/core/module/attr_accessor_spec.rb
  67. +12 −0 spec/core/number/lt_spec.rb
  68. +24 −0 spec/core/string/sub_spec.rb
  69. +10 −0 spec/core/true/and_spec.rb
  70. +6 −0 spec/core/true/inspect_spec.rb
  71. +10 −0 spec/core/true/or_spec.rb
  72. +6 −0 spec/core/true/to_s_spec.rb
  73. +10 −0 spec/core/true/xor_spec.rb
  74. +64 −0 spec/language/and_spec.rb
  75. +70 −0 spec/language/array_spec.rb
  76. +63 −0 spec/language/block_spec.rb
  77. +26 −0 spec/language/break_spec.rb
  78. +103 −0 spec/language/case_spec.rb
  79. +21 −0 spec/language/def_spec.rb
  80. +69 −0 spec/language/eigenclass_spec.rb
  81. +13 −0 spec/language/file_spec.rb
  82. 0 spec/language/fixtures/array.rb
  83. +21 −0 spec/language/fixtures/block.rb
  84. +41 −0 spec/language/fixtures/break.rb
  85. +44 −0 spec/language/fixtures/private.rb
  86. +293 −0 spec/language/fixtures/super.rb
  87. +29 −0 spec/language/hash_spec.rb
  88. +54 −0 spec/language/if_spec.rb
  89. +11 −0 spec/language/loop_spec.rb
  90. +21 −0 spec/language/metaclass_spec.rb
  91. +124 −0 spec/language/method_spec.rb
  92. +25 −0 spec/language/next_spec.rb
  93. +34 −0 spec/language/or_spec.rb
  94. +38 −0 spec/language/private_spec.rb
  95. +24 −0 spec/language/redo_spec.rb
  96. +26 −0 spec/language/regexp_spec.rb
  97. +20 −0 spec/language/rescue_spec.rb
  98. +47 −0 spec/language/return_spec.rb
  99. +25 −0 spec/language/string_spec.rb
  100. +33 −0 spec/language/super_spec.rb
  101. +157 −0 spec/language/until_spec.rb
  102. +155 −0 spec/language/variables_spec.rb
  103. +164 −0 spec/language/while_spec.rb
  104. +5 −0 spec/spec_helper.rb
75 LICENSE
@@ -0,0 +1,75 @@
Copyright (C) 2011 by Adam Beynon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Opalspec based on RSPEC, original license:
==========================================
Copyright (c) 2005-2010 The RSpec Development Team
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Opalite contains Sizzle, original license:
==========================================
MIT License
----
Copyright (c) 2009 John Resig
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
197 README.md
@@ -0,0 +1,197 @@
Opal: Ruby runtime for javascript
=================================
**Homepage**: [http://opalscript.org](http://opalscript.org)
**Github**: [http://github.com/adambeynon/opal](http://github.com/adambeynon/opal)
**Documentation**: [http://adambeynon.github.com/opal/index.html](http://adambeynon.github.com/opal/index.html)
Description
-----------
Opal is a ruby runtime and set of core libraries designed to run
directly on top of javascript. It supports the gem packaging system for
building ruby ready for the browser. It also uses therubyracer to
provide a command line REPL and environment for running ruby server-side
inside a javascript context.
Installation
------------
Opal is distributed as a gem, so install with:
$ gem install opal
Alternativley, you may just clone this git repo. No building is
neccessary.
Usage
-----
The opal build tools can be used in three ways. As a simple repl, the
bundler tasks and the simple build tasks.
**1. opal Command-line tool**
In its current form, the `opal` executable is very simple - it provides
a simple ruby repl that uses therubyracer to provide an embedable
javascript context. If you have installed the gem, to run the REPL,
simply type:
$ opal
or if you have cloned this repository, type:
$ bin/opal
The REPL can be used like any other repl, and internally each command is
being compiled into javascript, and run in the context which opal is
already loaded into.
**2. Builder Rake Task**
To build simple ruby files (without a .gemspec) is to use the
BuilderTask class in a rake file. This can be done by adding the
following to a `Rakefile`:
require 'opal'
Opal::Rake::BuilderTask.new do |t|
t.files = 'ruby/**/*.rb'
t.out = 'js/ruby_code.js'
end
When using the rake task, the `files` and `out` optional are usually
best required. The `files` will take an array, or a single string, and
can be globs. It is important to use relative paths here not absolute
paths. This will be default create a rake task called `opal` in your
rakefile, but you can rename it by passing another name into `new`. To
compile the javascript run:
$ rake opal
The out file will now contain the compiled ruby code. Run this in a
browser by including it into a html document, but make sure to include
the latest `opal.js` file first.
**Main file**
By default the builder task will automatically load the first listed
file in the `files` array when the file is loaded in the browser. The
`main` option allows you to specify another file:
Opal::Rake::BuilderTask.new do |t|
t.files = ['ruby/file_1.rb', 'ruby/file_2.rb', 'ruby/file_3.rb']
t.out = 'out.js'
t.main = 'ruby/file_2.rb'
end
**File watching**
To save manually compiling each time you change a file, the `watch`
option can be set to automatically recompile everytime a listed file is
modified. Observe the command line after using this task:
Opal::Rake::BuilderTask.new do |t|
t.files = 'ruby/*.rb'
t.out = 'my_code.js'
t.watch = true
end
**Examples**
See the `examples/` directory for runnable demos.
Built-in gems
------------
Opal uses gems as the main means of distributing and building code ready
for the browser. Opal uses its own gem system, and cannot access gems
included in the standard ruby installation. To aid this, the actual opal
gem includes several gems that offer the basic features. These gems are:
**core**
The core gem offers the ruby core library. It is mostly written in ruby,
but has a large proportion of inline javascript to make it as performant
as possible. Many of the classes are toll-free bridged to native
javascript objects, including Array, String, Numeric and Proc. Read the
[core library documentation](http://adambeynon.github.com/opal/gems/core/index.html)
for more information.
**dev**
The dev gem contains a ruby parser and compiler written in javascript.
It is a clone of the default opal parser written in ruby. This gem is in
the process of being replaced with the pure ruby version, which will be
compiled into javascript for this gem. This gem can be loaded into
javascript to allow in browser compilation of ruby sources from external
files or through html `script` tags.
**json**
The json gem included methods for parsing json strings and then
generating json objects from a string of json code. This gem aims to be
API compatible with the standard json library. Similarly to that
library, json objects are mapped to Hash, Array, true, false, nil,
String and Numeric as appropriate. See the
[JSON documentation](http://adambeynon.github.com/opal/gems/json/index.html)
for the full api.
**rquery**
RQuery is a port/abstraction of the jQuery javascript library for DOM
manipulation. jQuery is actually included as part of the gem, and top
level classes like Element, Document and Request are used to interact
with the document and elements. The api tries to stay as close to the
jquery interface as possible, but adds appropriate ruby syntax features
on top. See the [rquery documentation](http://adambeynon.github.com/opal/gems/rquery/index.html)
for the full api.
**ospec**
OSpec is a minimal clone of the ruby rspec library. It implements the
core features available in rspec, and works both from the command line
and in browser. The core, json and rquery gems all have tests/specs
written ready for ospec. See the [ospec guide](http://adambeynon.github.com/opal/gems/ospec/index.html)
to get started.
Browser version
---------------
**Latest version**: 0.3.2
**Minified and gzipped**: 42kb
**Download**:
[http://adambeynon.github.com/opal/opal.js](http://adambeynon.github.com/opal/opal.js)
Opal is distributed ready for the browser in a single file `opal.js`.
This file contains the opal bootloader, the core library gem, the json
gem and the rquery gem. In addition to these, jquery is also included as
part of rquery. This self contained file is all that is needed to get
started with opal.
Using the `BuilderTask` as described above, running that file is as
simple as using the given html file:
<!doctype html>
<html>
<head>
<script src="path/to/opal.js"></script>
<script src="ruby_code.js"></script>
</head>
</html>
Changelog
---------
- **31 March 2011**: 0.3.2 Release
- Added BuilderTask for easy building for simple projects.
- Amended build options in Builder to support new rake task.
- **30 March 2011**: 0.3.1 Release
- Fix to make `opal` an executable
- **30 March 2011**: 0.3.0 Release
- Major redesign of build tools to use v8 for server side opal
- Split all opal packages into actual gems
- File and Dir classes for both browser and v8 gem runtimes
@@ -0,0 +1,21 @@
Browser demo
============
This directory contains a very simple browser demo. It is meant to be
run from within this repo, as it relies on the opal.js build being
located in its doc/opal.js location. To run this example manually, alter
the html file to include your local copy of `opal.js`.
Running the demo
----------------
There is a rake task `opal` in the bundled file, which will build the
`browser.rb` file to the needed `browser.js` file. The `Rakefile` is
setup to use the development version of opal, but to use the gem simply
comment out the first line. So, to build:
$ rake opal
From there, open `index.html` in your browser of choice and view the
debug console to view the additional output.
@@ -0,0 +1,10 @@
# Uncomment this line to use the gem version of opal
$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib')
require 'opal'
Opal::Rake::BuilderTask.new do |t|
t.files = 'browser.rb'
t.out = 'browser.js'
end
@@ -0,0 +1,46 @@
opal.register('browser.js', function(VM, self, __FILE__) { var nil = VM.Qnil, $class = VM.dc, $def = VM.dm, $symbol = VM.Y, $hash = VM.H, $block = VM.P, Qtrue = VM.Qtrue, Qfalse = VM.Qfalse;VM.mm(['puts', 'to_s', 'each', 'inspect', 'new', 'try_this', 'do_assign=', '[]=', 'is_this_true?', 'no_its_not!', '+', '[]', '-@', 'raise']);var __a, __b, title, __c, e;if (self['@cls'] == undefined) { self['@cls'] = nil; }
(__a = self).$m.puts(__a, "Hello, world! Running in the browser..");
"All code is generated to keep the same line as the original ruby";
(__a = self).$m.puts(__a, ("This code is generated from line " + (__b = 6).$m.to_s(__b) + ", in file: " + (__b = __FILE__).$m.to_s(__b)));
title = document.title;
(__a = self).$m.puts(__a, ("The document title is '" + (__b = title).$m.to_s(__b) + "'"));
(__a = [1, 2, 3, 4, 5], __b = [__a], ($block.p = function(self, a) { var __a;
return (__a = self).$m.puts(__a, a);
}).$self = self, ($block.f = __a.$m.each).apply(__a, __b));
$class(self, nil, 'ClassA', function() { var self = this;
$def(self, 'method_missing', function(self, method_id, args) { var __a, __b, __c;args = [].slice.call(arguments, 2);
return (__a = self).$m.puts(__a, ("Tried to call '" + (__b = method_id).$m.to_s(__b) + "' with: " + (__b = (__c = args).$m.inspect(__c)).$m.to_s(__b)));
}, 0);
}, 0);
self['@cls'] = (__b = rb_vm_cg(self, 'ClassA')).$m['new'](__b);
(__b = self['@cls']).$m.try_this(__b);
(__b = self['@cls']).$m['do_assign='](__b, "this won't work");
(__b = self['@cls']).$m['[]='](__b, 'neither', $symbol('will_this'));
(__b = self['@cls']).$m['is_this_true?'](__b);
(__b = self['@cls']).$m['no_its_not!'](__b);
(__b = self['@cls']).$m['+'](__b, 'something to add');
(__b = self).$m.puts(__b, (__a = (1)).$m['+'](__a, 2));
(__b = self).$m.puts(__b, (__a = [1, 2, 3, 4]).$m['[]'](__a, 0));
(__b = self).$m.puts(__b, (__a = [1, 2, 3, 4]).$m['[]'](__a, (__c = (2)).$m['-@'](__c)));
$class(self, rb_vm_cg(self, 'Exception'), 'CustomBrowserException', function() { var self = this;nil}, 0);
try {
(__b = self).$m.raise(__b, rb_vm_cg(self, 'CustomBrowserException'), "some error happened");} catch (__err__) {
if (true){e = __err__;
(__b = self).$m.puts(__b, "caught error:");
(__b = self).$m.puts(__b, (__a = e).$m.inspect(__a));}
}; });
opal.require('browser');
Oops, something went wrong.

0 comments on commit 6a70554

Please sign in to comment.