opal 0.3.6 → 0.3.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. data/README.md +69 -97
  2. data/bin/opal +2 -2
  3. data/{lib/core → corelib}/array.rb +85 -56
  4. data/corelib/boolean.rb +20 -0
  5. data/corelib/class.rb +58 -0
  6. data/{lib → corelib}/core.rb +2 -50
  7. data/corelib/dir.rb +22 -0
  8. data/{lib/core → corelib}/enumerable.rb +0 -0
  9. data/corelib/error.rb +19 -0
  10. data/{lib/core → corelib}/file.rb +7 -9
  11. data/{lib/core → corelib}/hash.rb +104 -144
  12. data/{lib/core → corelib}/kernel.rb +38 -44
  13. data/corelib/load_order +21 -0
  14. data/{lib/core → corelib}/match_data.rb +0 -0
  15. data/{lib/core → corelib}/module.rb +12 -8
  16. data/{lib/core → corelib}/nil_class.rb +2 -2
  17. data/{lib/core → corelib}/numeric.rb +37 -100
  18. data/corelib/object.rb +37 -0
  19. data/{lib/core → corelib}/proc.rb +3 -3
  20. data/corelib/range.rb +27 -0
  21. data/{lib/core → corelib}/regexp.rb +1 -1
  22. data/{lib/core → corelib}/string.rb +16 -107
  23. data/{lib/core → corelib}/top_self.rb +0 -0
  24. data/lib/opal.rb +7 -0
  25. data/lib/opal/browserify.rb +34 -0
  26. data/{opal_lib → lib}/opal/builder.rb +70 -24
  27. data/lib/opal/command.rb +52 -0
  28. data/lib/opal/context.rb +197 -0
  29. data/{opal_lib/opal/ruby/parser.rb → lib/opal/lexer.rb} +20 -4
  30. data/{opal_lib/opal/ruby → lib/opal}/nodes.rb +238 -127
  31. data/lib/opal/parser.rb +4894 -0
  32. data/{opal_lib/opal/ruby/ruby_parser.y → lib/opal/parser.y} +38 -18
  33. data/lib/rbp.rb +2 -0
  34. data/lib/rbp/package.rb +49 -0
  35. data/runtime/class.js +216 -189
  36. data/runtime/fs.js +2 -2
  37. data/runtime/init.js +242 -244
  38. data/runtime/loader.js +78 -99
  39. data/runtime/module.js +34 -40
  40. data/runtime/post.js +2 -2
  41. data/runtime/pre.js +1 -1
  42. data/runtime/runtime.js +129 -135
  43. data/{lib → stdlib}/dev.rb +10 -10
  44. data/{lib → stdlib}/racc/parser.rb +0 -6
  45. data/{lib → stdlib}/strscan.rb +4 -4
  46. metadata +57 -105
  47. data/lib/core/basic_object.rb +0 -51
  48. data/lib/core/class.rb +0 -38
  49. data/lib/core/dir.rb +0 -26
  50. data/lib/core/error.rb +0 -75
  51. data/lib/core/false_class.rb +0 -81
  52. data/lib/core/object.rb +0 -6
  53. data/lib/core/range.rb +0 -27
  54. data/lib/core/symbol.rb +0 -42
  55. data/lib/core/true_class.rb +0 -41
  56. data/lib/ospec.rb +0 -7
  57. data/lib/ospec/autorun.rb +0 -8
  58. data/lib/ospec/dsl.rb +0 -15
  59. data/lib/ospec/example.rb +0 -11
  60. data/lib/ospec/example/before_and_after_hooks.rb +0 -56
  61. data/lib/ospec/example/errors.rb +0 -17
  62. data/lib/ospec/example/example_group.rb +0 -12
  63. data/lib/ospec/example/example_group_factory.rb +0 -18
  64. data/lib/ospec/example/example_group_hierarchy.rb +0 -21
  65. data/lib/ospec/example/example_group_methods.rb +0 -100
  66. data/lib/ospec/example/example_group_proxy.rb +0 -15
  67. data/lib/ospec/example/example_methods.rb +0 -46
  68. data/lib/ospec/example/example_proxy.rb +0 -18
  69. data/lib/ospec/expectations.rb +0 -19
  70. data/lib/ospec/expectations/errors.rb +0 -8
  71. data/lib/ospec/expectations/fail_with.rb +0 -9
  72. data/lib/ospec/expectations/handler.rb +0 -33
  73. data/lib/ospec/helpers/scratch.rb +0 -18
  74. data/lib/ospec/matchers.rb +0 -24
  75. data/lib/ospec/matchers/be.rb +0 -1
  76. data/lib/ospec/matchers/generated_descriptions.rb +0 -20
  77. data/lib/ospec/matchers/operator_matcher.rb +0 -54
  78. data/lib/ospec/matchers/raise_error.rb +0 -38
  79. data/lib/ospec/runner.rb +0 -90
  80. data/lib/ospec/runner/example_group_runner.rb +0 -41
  81. data/lib/ospec/runner/formatter/html_formatter.rb +0 -139
  82. data/lib/ospec/runner/formatter/terminal_formatter.rb +0 -48
  83. data/lib/ospec/runner/options.rb +0 -34
  84. data/lib/ospec/runner/reporter.rb +0 -82
  85. data/opal_lib/opal.rb +0 -16
  86. data/opal_lib/opal/build_methods.rb +0 -51
  87. data/opal_lib/opal/bundle.rb +0 -70
  88. data/opal_lib/opal/command.rb +0 -68
  89. data/opal_lib/opal/context.rb +0 -81
  90. data/opal_lib/opal/context/console.rb +0 -10
  91. data/opal_lib/opal/context/file_system.rb +0 -34
  92. data/opal_lib/opal/context/loader.rb +0 -135
  93. data/opal_lib/opal/gem.rb +0 -84
  94. data/opal_lib/opal/rake/builder_task.rb +0 -44
  95. data/opal_lib/opal/rake/spec_task.rb +0 -32
  96. data/opal_lib/opal/ruby/ruby_parser.rb +0 -4862
  97. data/opal_lib/opal/version.rb +0 -4
  98. data/runtime/debug.js +0 -84
@@ -1,68 +0,0 @@
1
- module Opal
2
- class Command < Thor
3
-
4
- desc "irb", "Opens interactive opal/ruby repl"
5
- def irb
6
- require 'opal/context'
7
- ctx = Opal::Context.new
8
- ctx.start_repl
9
- end
10
-
11
- desc "spec [FILENAME]", "ospec runner"
12
- long_desc %[
13
- This will run the ospec gem in the current working directory
14
- to load the given specs. If no input files are given then
15
- ospec will run all specs it can find in the spec/ dir.
16
- ]
17
- def spec(*specs)
18
- require 'opal/context'
19
-
20
- argv = specs
21
-
22
- ctx = Opal::Context.new
23
- ctx.argv = argv
24
- ctx.require_file 'ospec/autorun'
25
- end
26
-
27
- desc "compile [FILE]", "Compile and puts compiled code to stdout"
28
- long_desc %[
29
- Basically compile the given file, and output it to the
30
- stdout. This is useful for testing the compiler to see
31
- what the generated code will be. If the str name is a
32
- file that exists, it will be read, otherwise the
33
- content will be run as a string.
34
- ]
35
- method_options :out => :string, :watch => :boolean, :main => :string
36
- def compile(*path)
37
- opts = options
38
- builder = Builder.new
39
- builder.build :files => path,
40
- :out => opts["out"], :watch => opts["watch"], :main => opts["main"]
41
- end
42
-
43
- desc "bundle", "Bundle the gem in the given directory ready for browser"
44
- method_options :out => :string
45
- def bundle
46
- opts = options
47
- bundle = Opal::Bundle.new(Opal::Gem.new(Dir.getwd))
48
- bundle.build opts
49
- end
50
-
51
- desc "exec [FILENAME]", "Run the given ruby file"
52
- def exec(file)
53
- puts "need to run #{file.inspect}"
54
- require 'opal/context'
55
- ctx = Opal::Context.new
56
- ctx.require_file file
57
- end
58
-
59
- def method_missing(task, *)
60
- if File.exist? task.to_s
61
- exec task.to_s
62
- else
63
- super
64
- end
65
- end
66
- end
67
- end
68
-
@@ -1,81 +0,0 @@
1
- # This file should only be required as needed. This file relies on v8,
2
- # therubyracer. As this is not an essential feature of opal, this file
3
- # will be loaded as needed, and when it is, an error is just thrown if
4
- # the required gems are not installed.
5
- begin
6
- require 'v8'
7
- rescue LoadError => e
8
- abort "therubyracer is required for running javascript. Install it with `gem install therubyracer`"
9
- end
10
-
11
- require 'opal/context/loader'
12
- require 'opal/context/console'
13
- require 'opal/context/file_system'
14
-
15
- module Opal
16
- class Context < V8::Context
17
-
18
- RUNTIME_PATH = File.expand_path File.join('..', '..', '..', 'runtime.js'), __FILE__
19
-
20
- def initialize(opts = {})
21
- super opts
22
- @builder = Opal::Builder.new
23
- setup_context
24
- end
25
-
26
- # Setup the context. This basically loads opal.js into our context, and
27
- # replace the loader etc with our custom loader for a Ruby environment. The
28
- # default "browser" loader cannot access files from disk.
29
- def setup_context
30
- self['console'] = Console.new
31
- eval "OPAL_DEBUG = true;", "(opal)"
32
- eval @builder.build_runtime, "(opal)"
33
-
34
- opal = self['opal']
35
- opal['loader'] = Loader.new opal, self
36
- opal['fs'] = FileSystem.new opal, self
37
- opal['platform']['engine'] = 'opal-gem'
38
-
39
- # eval "opal.require('core');", "(opal)"
40
- require_file 'core'
41
- end
42
-
43
- # Require the given id as if it was required in the context. This simply
44
- # passes the require through to the underlying context.
45
- def require_file(path)
46
- eval "opal.require('#{path}');", "(opal)"
47
- end
48
-
49
- # Set ARGV for the context
50
- def argv=(args)
51
- puts "setting argv to #{args.inspect}"
52
- eval "opal.runtime.cs(opal.runtime.Object, 'ARGV', #{args.inspect});"
53
- end
54
-
55
- # Start normal js repl
56
- def start_repl
57
- require 'readline'
58
-
59
- loop do
60
- line = Readline.readline '>> ', true
61
- puts "=> #{eval_ruby line, '(opal)'}"
62
- end
63
- end
64
-
65
- def eval_ruby(content, line = "")
66
- begin
67
- code = Opal::RubyParser.new(content).parse!.generate_top
68
- code = "opal.run(function() {var $rb = opal.runtime, self = $rb.top, __FILE__ = '(opal)';" + code + "});"
69
- # puts code
70
- self['$opal_irb_result'] = eval code, line
71
- # self['$code'].to_s + "ww"
72
- eval "!$opal_irb_result.$m.inspect.$rbMM ? $opal_irb_result.$m.inspect($opal_irb_result) : '(Object doesnt support #inspect)'"
73
- rescue => e
74
- puts e
75
- puts("\t" + e.backtrace.join("\n\t"))
76
- end
77
- end
78
- end
79
-
80
- end
81
-
@@ -1,10 +0,0 @@
1
- module Opal
2
- class Console
3
-
4
- def log(*str)
5
- puts str.join("\n")
6
- nil
7
- end
8
- end
9
- end
10
-
@@ -1,34 +0,0 @@
1
- module Opal
2
- class FileSystem
3
-
4
- def initialize(opal, context)
5
- @opal = opal
6
- @ctx = context
7
- end
8
-
9
- def cwd
10
- Dir.getwd
11
- end
12
-
13
- def glob(*arr)
14
- Dir.glob(arr)
15
- end
16
-
17
- def exist_p(path)
18
- File.exist? path
19
- end
20
-
21
- def expand_path(filename, dir_string = nil)
22
- File.expand_path filename, dir_string
23
- end
24
-
25
- def dirname(file)
26
- File.dirname file
27
- end
28
-
29
- def join(*parts)
30
- File.join *parts
31
- end
32
- end
33
- end
34
-
@@ -1,135 +0,0 @@
1
-
2
- module Opal
3
- # Custom loader class, which an instance is set on the opal.loader property
4
- # to allow for disk based loading and access to modules/packages/gems.
5
- #
6
- # For now, this simply adds hard-coded paths to the load path. In future,
7
- # dynamic package lookup will take place, but for now we just manually loop
8
- # through gems/packages in this gems' dir, and the cwd dir (in ./vendor/) and
9
- # add each of their lib/ dirs). Unil we add a custom package manager, this
10
- # will make it the easiest way of managing packages for vienna.
11
- class Loader
12
-
13
- # Load paths. This is an array of all load paths
14
- attr_reader :paths
15
-
16
- # The vienna instance for the object
17
- attr_reader :vienna
18
-
19
- # @param vienna The js vienna object itself so we can talk to it
20
- # @param {Vienna::Context} context The v8 context
21
- def initialize(vienna, context)
22
- @vienna = vienna
23
- @ctx = context
24
- @paths = []
25
-
26
- # core lib/std lib
27
- @paths << File.expand_path(File.join('..', '..', '..', '..', 'lib'), __FILE__)
28
-
29
- hardcode_gems
30
- end
31
-
32
- # Register all gems - this is currently hardcoded while we don't have a
33
- # proper package manager. This needs to be replaced to be more dynamic in
34
- # lookup.
35
- def hardcode_gems
36
- return nil
37
- core = File.expand_path(File.join(__FILE__, '..', '..', '..', '..', 'gems'))
38
-
39
- Dir.open(core).each do |entry|
40
- try_dir = File.join core, entry
41
- if File.directory?(try_dir) && !['.', '..'].include?(entry)
42
- add_hardcoded_gem try_dir
43
- end
44
- end
45
- end
46
-
47
- # Add an individual hardcoded gem by its path. We need to check it has a
48
- # .gemspec in its dir, then add its lib directory to our path.
49
- def add_hardcoded_gem(path)
50
- # make sure gem dir has a gemspec
51
- gemspec = File.join path, File.basename(path) + '.gemspec'
52
- return unless File.exists? gemspec
53
- # make sure gem dir has a lib dir
54
- libpath = File.join path, 'lib'
55
- return unless File.exists? libpath
56
- # all is ok, so add to loadpath
57
- @paths << libpath
58
- end
59
-
60
- # Valid extensions. This should be more dynamic for custom extensions
61
- def valid_extensions
62
- %w[.rb .js]
63
- end
64
-
65
- # Exposed as replacement method.
66
- def resolve_lib(id)
67
- resolved = find_lib id, @paths
68
- raise "Cannot find lib '#{id}'" unless resolved
69
- resolved
70
- end
71
-
72
- # Resolve the id requested with the given valid paths
73
- def find_lib(id, paths)
74
- extensions = valid_extensions
75
-
76
- @paths.each do |path|
77
-
78
- extensions.each do |ext|
79
- candidate = File.join(path, id + ext)
80
-
81
- # if file exists, return it!
82
- return candidate if File.exists? candidate
83
- end
84
-
85
- # if has extension already
86
- candidate = File.join(path, id)
87
- return candidate if File.exists? candidate
88
- end
89
-
90
- # alternatively, if id is a full path, just load it
91
- if File.exist? id
92
- return File.expand_path id
93
- end
94
-
95
- if File.exist? id + '.rb'
96
- return File.expand_path(id + '.rb')
97
- end
98
-
99
- # if we cannot find it, just return nil
100
- nil
101
- end
102
-
103
- # Returns the contents of the module. This, in ruby, reads from the disk,
104
- # but the browser uses either XHR or a cached module reference. The
105
- # method name is kept generic to reflect this.
106
- #
107
- # @param {String} filename The filename to load. This is a full filename,
108
- # not just a module id.
109
- def file_contents(filename)
110
- File.read filename
111
- end
112
-
113
- # Special version of module contents that will compile the given ruby code
114
- # at the file into javascript first, before returning it. This is exposed
115
- # to vienna so that it makes it easier to compile and run ruby from the
116
- # v8 context. The default implementation of this throws an error to say
117
- # that in browser ruby code cannot be run.
118
- def ruby_file_contents(filename)
119
- parser = Opal::RubyParser.new File.read(filename)
120
- result = parser.parse!.generate_top
121
- result
122
- end
123
-
124
- # Wraps the content with the given filename. Basically, here we eval
125
- # the code inside a function which takes our exports, module,
126
- # require etc, and returns the function ready for calling. If an
127
- # error occures (likely a parse error) it is just thrown as normal
128
- def wrap(content, filename)
129
- code = "(function($rb, self, __FILE__) { #{content} });"
130
- # puts code
131
- @ctx.eval code, filename
132
- end
133
- end
134
- end
135
-
data/opal_lib/opal/gem.rb DELETED
@@ -1,84 +0,0 @@
1
- module Opal
2
-
3
- # The gem class wraps around RubyGem's Gem::Specification class to basically add
4
- # support for building a gem ready for the browser. The actual gem object can be
5
- # accessed through {#spec}.
6
- class Gem
7
-
8
- # The real Gem::Specification object, which is used for accessing gem info
9
- attr_reader :spec
10
-
11
- # Root directory for the package
12
- attr_reader :root_dir
13
-
14
- def initialize(rootdir = Dir.getwd)
15
- require 'rubygems'
16
- @root_dir = rootdir
17
- path = File.expand_path File.join(@root_dir, File.basename(@root_dir) + '.gemspec')
18
-
19
- # we MUST chdir here to make sure files globs etc in the gemspec use the basedir
20
- # as the working directory, otherwise opal, or the main gem working dir will
21
- # accidentally be the cwd.
22
- Dir.chdir(File.dirname(path)) do
23
- @spec = ::Gem::Specification.load path
24
- end
25
-
26
- raise "Bad/missing gemspec in #{rootdir}" unless @spec
27
- end
28
-
29
- def bundle(opts = {})
30
- Opal::Bundle.new(self).build opts
31
- end
32
-
33
- def name
34
- @spec.name
35
- end
36
-
37
- def version
38
- @spec.version.to_s
39
- end
40
-
41
- def require_paths
42
- @spec.require_paths
43
- end
44
-
45
- def files
46
- @spec.files
47
- end
48
-
49
- def test_files
50
- @spec.test_files
51
- end
52
-
53
- def to_s
54
- "#<Gem '#{name}'>"
55
- end
56
-
57
- # Returns an array of dependencies for this package. Version requirements
58
- # are ignored for now. This simply returns the array of package names that
59
- # are needed. If the package.json does not declare any, then an empty
60
- # array is returned.
61
- #
62
- # @return {Array<String>}
63
- def dependencies
64
- dependencies = @spec.dependencies
65
-
66
- dependencies.map { |d| d.name }
67
- end
68
-
69
- # Returns the files from the .files property of the gem that are only
70
- # available from the require_paths array. Basically, these are the files
71
- # that are our 'lib' files. They are returned as relative to the root.
72
- #
73
- # @return [Array<String>]
74
- def lib_files
75
- paths = require_paths
76
- ext = %w[.rb .js]
77
-
78
- files.select do |f|
79
- f.start_with?(*paths) && ext.include?(File.extname(f))
80
- end
81
- end
82
- end
83
- end
84
-
@@ -1,44 +0,0 @@
1
- require 'rake'
2
- require 'rake/tasklib'
3
-
4
- module Opal
5
- module Rake
6
-
7
- class BuilderTask < ::Rake::TaskLib
8
-
9
- attr_accessor :files
10
-
11
- attr_accessor :out
12
-
13
- attr_accessor :main
14
-
15
- attr_accessor :watch
16
-
17
- def initialize(name = :opal)
18
- @name = name
19
- @files = []
20
- @out = nil
21
- @main = nil
22
- @watch = false
23
-
24
- yield self if block_given?
25
- define_rake_task
26
- end
27
-
28
- def define_rake_task
29
- desc "Build opal files"
30
- task(@name) do
31
- options = {}
32
- options[:files] = @files
33
- options[:out] = @out if @out
34
- options[:main] = @main if @main
35
- options[:watch] = @watch
36
-
37
- builder = Opal::Builder.new
38
- builder.build options
39
- end
40
- end
41
- end
42
- end
43
- end
44
-