opal 0.6.3 → 0.7.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (221) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.spectator +2 -0
  4. data/.spectator-mspec +3 -0
  5. data/.travis.yml +8 -11
  6. data/CHANGELOG.md +33 -0
  7. data/CONTRIBUTING.md +8 -43
  8. data/Gemfile +15 -4
  9. data/Guardfile +77 -0
  10. data/README.md +15 -9
  11. data/Rakefile +36 -12
  12. data/benchmarks/operators.rb +11 -0
  13. data/bin/opal +10 -13
  14. data/bin/opal-build +4 -4
  15. data/bin/opal-mspec +10 -0
  16. data/bin/opal-repl +4 -3
  17. data/examples/sinatra/Gemfile +1 -1
  18. data/examples/sinatra/config.ru +3 -3
  19. data/lib/mspec/opal/main.rb.erb +2 -2
  20. data/lib/mspec/opal/rake_task.rb +31 -24
  21. data/lib/mspec/opal/runner.rb +18 -1
  22. data/lib/mspec/opal/sprockets.js +17 -0
  23. data/lib/opal.rb +1 -34
  24. data/lib/opal/builder.rb +92 -58
  25. data/lib/opal/builder_processors.rb +165 -0
  26. data/lib/opal/cli.rb +85 -144
  27. data/lib/opal/cli_options.rb +136 -90
  28. data/lib/opal/cli_runners.rb +10 -0
  29. data/lib/opal/cli_runners/nodejs.rb +56 -0
  30. data/lib/opal/cli_runners/phantom.js +35 -0
  31. data/lib/opal/cli_runners/phantomjs.rb +28 -0
  32. data/lib/opal/cli_runners/server.rb +54 -0
  33. data/lib/opal/compiler.rb +35 -16
  34. data/lib/opal/erb.rb +29 -15
  35. data/lib/opal/hike_path_finder.rb +18 -0
  36. data/lib/opal/nodes.rb +1 -0
  37. data/lib/opal/nodes/call.rb +107 -26
  38. data/lib/opal/nodes/call_special.rb +31 -6
  39. data/lib/opal/nodes/class.rb +2 -2
  40. data/lib/opal/nodes/constants.rb +5 -20
  41. data/lib/opal/nodes/def.rb +4 -4
  42. data/lib/opal/nodes/defined.rb +3 -3
  43. data/lib/opal/nodes/definitions.rb +1 -1
  44. data/lib/opal/nodes/for.rb +35 -0
  45. data/lib/opal/nodes/helpers.rb +2 -2
  46. data/lib/opal/nodes/iter.rb +3 -3
  47. data/lib/opal/nodes/literal.rb +10 -2
  48. data/lib/opal/nodes/masgn.rb +2 -2
  49. data/lib/opal/nodes/module.rb +2 -2
  50. data/lib/opal/nodes/scope.rb +1 -0
  51. data/lib/opal/nodes/singleton_class.rb +2 -2
  52. data/lib/opal/nodes/super.rb +2 -2
  53. data/lib/opal/nodes/top.rb +30 -3
  54. data/lib/opal/parser.rb +15 -1
  55. data/lib/opal/parser/grammar.rb +2571 -2452
  56. data/lib/opal/parser/grammar.y +37 -5
  57. data/lib/opal/parser/keywords.rb +2 -0
  58. data/lib/opal/parser/lexer.rb +21 -11
  59. data/lib/opal/path_reader.rb +28 -0
  60. data/lib/opal/paths.rb +38 -0
  61. data/lib/opal/source_map.rb +32 -15
  62. data/lib/opal/sprockets/environment.rb +9 -2
  63. data/lib/opal/sprockets/erb.rb +1 -2
  64. data/lib/opal/sprockets/path_reader.rb +34 -0
  65. data/lib/opal/sprockets/processor.rb +40 -39
  66. data/lib/opal/sprockets/server.rb +47 -33
  67. data/lib/opal/version.rb +1 -1
  68. data/opal.gemspec +10 -5
  69. data/opal/README.md +6 -0
  70. data/opal/corelib/array.rb +36 -4
  71. data/opal/corelib/array/inheritance.rb +6 -6
  72. data/opal/corelib/basic_object.rb +9 -9
  73. data/opal/corelib/boolean.rb +1 -1
  74. data/opal/corelib/class.rb +12 -12
  75. data/opal/corelib/dir.rb +20 -0
  76. data/opal/corelib/enumerable.rb +42 -42
  77. data/opal/corelib/enumerator.rb +1 -1
  78. data/opal/corelib/error.rb +2 -2
  79. data/opal/corelib/file.rb +56 -0
  80. data/opal/corelib/hash.rb +5 -5
  81. data/opal/corelib/helpers.rb +3 -3
  82. data/opal/corelib/io.rb +13 -10
  83. data/opal/corelib/kernel.rb +44 -68
  84. data/opal/corelib/method.rb +1 -1
  85. data/opal/corelib/module.rb +89 -114
  86. data/opal/corelib/nil_class.rb +1 -1
  87. data/opal/corelib/numeric.rb +27 -23
  88. data/opal/corelib/proc.rb +5 -5
  89. data/opal/corelib/range.rb +8 -4
  90. data/opal/corelib/regexp.rb +5 -5
  91. data/opal/corelib/runtime.js +589 -272
  92. data/opal/corelib/string.rb +52 -37
  93. data/opal/corelib/string/inheritance.rb +5 -5
  94. data/opal/corelib/time.rb +102 -52
  95. data/opal/corelib/variables.rb +3 -3
  96. data/opal/opal.rb +2 -0
  97. data/package.json +9 -0
  98. data/spec/filters/bugs/array.rb +0 -6
  99. data/spec/filters/bugs/language.rb +4 -0
  100. data/spec/filters/bugs/numeric.rb +7 -6
  101. data/spec/filters/bugs/opal.rb +2 -0
  102. data/spec/filters/bugs/regexp.rb +4 -0
  103. data/spec/filters/bugs/string.rb +0 -7
  104. data/spec/filters/bugs/stringscanner.rb +4 -1
  105. data/spec/filters/unsupported/private_methods.rb +2 -0
  106. data/spec/lib/builder_processors_spec.rb +27 -0
  107. data/spec/lib/builder_spec.rb +66 -0
  108. data/spec/{cli → lib}/cli_spec.rb +60 -5
  109. data/spec/{cli → lib}/compiler_spec.rb +66 -5
  110. data/spec/{cli → lib}/dependency_resolver_spec.rb +1 -1
  111. data/spec/lib/fixtures/no_requires.rb +1 -0
  112. data/spec/{cli → lib}/fixtures/opal_file.rb +0 -0
  113. data/spec/lib/fixtures/require_tree_test.rb +3 -0
  114. data/spec/lib/fixtures/required_tree_test/required_file1.rb +1 -0
  115. data/spec/lib/fixtures/required_tree_test/required_file2.rb +1 -0
  116. data/spec/lib/fixtures/requires.rb +7 -0
  117. data/spec/{cli → lib}/fixtures/sprockets_file.js.rb +0 -0
  118. data/spec/lib/fixtures/sprockets_require_tree_test.rb +3 -0
  119. data/spec/lib/hike_path_finder_spec.rb +23 -0
  120. data/spec/{cli → lib}/lexer_spec.rb +1 -1
  121. data/spec/{cli → lib}/parser/alias_spec.rb +1 -1
  122. data/spec/{cli → lib}/parser/and_spec.rb +1 -1
  123. data/spec/{cli → lib}/parser/attrasgn_spec.rb +1 -1
  124. data/spec/{cli → lib}/parser/begin_spec.rb +1 -1
  125. data/spec/{cli → lib}/parser/block_spec.rb +1 -1
  126. data/spec/{cli → lib}/parser/break_spec.rb +1 -1
  127. data/spec/{cli → lib}/parser/call_spec.rb +1 -1
  128. data/spec/{cli → lib}/parser/class_spec.rb +1 -1
  129. data/spec/{cli → lib}/parser/comments_spec.rb +1 -1
  130. data/spec/{cli → lib}/parser/def_spec.rb +1 -1
  131. data/spec/{cli → lib}/parser/if_spec.rb +1 -1
  132. data/spec/{cli → lib}/parser/iter_spec.rb +1 -1
  133. data/spec/{cli → lib}/parser/lambda_spec.rb +1 -1
  134. data/spec/{cli → lib}/parser/literal_spec.rb +1 -1
  135. data/spec/{cli → lib}/parser/masgn_spec.rb +1 -1
  136. data/spec/{cli → lib}/parser/module_spec.rb +1 -1
  137. data/spec/{cli → lib}/parser/not_spec.rb +1 -1
  138. data/spec/{cli → lib}/parser/op_asgn1_spec.rb +1 -1
  139. data/spec/{cli → lib}/parser/op_asgn2_spec.rb +1 -1
  140. data/spec/{cli → lib}/parser/or_spec.rb +1 -1
  141. data/spec/{cli → lib}/parser/return_spec.rb +1 -1
  142. data/spec/{cli → lib}/parser/sclass_spec.rb +1 -1
  143. data/spec/{cli → lib}/parser/string_spec.rb +8 -1
  144. data/spec/{cli → lib}/parser/super_spec.rb +1 -1
  145. data/spec/lib/parser/unary_spec.rb +48 -0
  146. data/spec/{cli → lib}/parser/undef_spec.rb +1 -1
  147. data/spec/{cli → lib}/parser/unless_spec.rb +1 -1
  148. data/spec/{cli → lib}/parser/variables_spec.rb +1 -1
  149. data/spec/{cli → lib}/parser/while_spec.rb +1 -1
  150. data/spec/{cli → lib}/parser/yield_spec.rb +1 -1
  151. data/spec/lib/path_reader_spec.rb +24 -0
  152. data/spec/lib/shared/path_finder_shared.rb +19 -0
  153. data/spec/lib/shared/path_reader_shared.rb +31 -0
  154. data/spec/lib/spec_helper.rb +9 -0
  155. data/spec/lib/sprockets/environment_spec.rb +30 -0
  156. data/spec/{cli → lib}/sprockets/erb_spec.rb +1 -1
  157. data/spec/lib/sprockets/path_reader_spec.rb +25 -0
  158. data/spec/{cli → lib}/sprockets/processor_spec.rb +9 -2
  159. data/spec/lib/sprockets/server_spec.rb +20 -0
  160. data/spec/opal/compiler/irb_spec.rb +11 -11
  161. data/spec/opal/core/fixtures/require_tree_files/file 1.rb +1 -0
  162. data/spec/opal/core/fixtures/require_tree_files/file 2.rb +1 -0
  163. data/spec/opal/core/fixtures/require_tree_files/file 3.rb +1 -0
  164. data/spec/opal/core/fixtures/require_tree_files/file 4.rb +1 -0
  165. data/spec/opal/core/fixtures/require_tree_files/file 5.rb +1 -0
  166. data/spec/opal/core/kernel/require_tree_spec.rb +7 -0
  167. data/spec/opal/core/kernel/respond_to_spec.rb +2 -2
  168. data/spec/opal/core/runtime/method_missing_spec.rb +19 -0
  169. data/spec/opal/core/source_map_spec.rb +2 -2
  170. data/spec/opal/core/string_spec.rb +11 -0
  171. data/spec/opal/stdlib/erb/erb_spec.rb +0 -1
  172. data/spec/opal/stdlib/thread/mutex_spec.rb +40 -0
  173. data/spec/opal/stdlib/thread/thread_queue_spec.rb +32 -0
  174. data/spec/opal/stdlib/thread/thread_spec.rb +60 -0
  175. data/spec/rubyspecs +54 -11
  176. data/spec/spec_helper.rb +18 -3
  177. data/spec/support/mspec_rspec_adapter.rb +33 -0
  178. data/spec/{cli/spec_helper.rb → support/parser_helpers.rb} +10 -10
  179. data/stdlib/README.md +3 -0
  180. data/stdlib/benchmark.rb +10 -0
  181. data/stdlib/date.rb +2 -2
  182. data/stdlib/dir.rb +1 -5
  183. data/stdlib/file.rb +1 -7
  184. data/stdlib/json.rb +10 -1
  185. data/stdlib/native.rb +5 -5
  186. data/stdlib/nodejs.rb +5 -0
  187. data/stdlib/nodejs/dir.rb +13 -0
  188. data/stdlib/nodejs/file.rb +98 -0
  189. data/stdlib/nodejs/fileutils.rb +26 -0
  190. data/stdlib/nodejs/io.rb +2 -0
  191. data/stdlib/nodejs/irb.rb +45 -0
  192. data/stdlib/nodejs/process.rb +16 -0
  193. data/stdlib/nodejs/require.rb +32 -0
  194. data/stdlib/nodejs/rubygems.rb +68 -0
  195. data/stdlib/nodejs/runtime.rb +25 -0
  196. data/stdlib/nodejs/yaml.rb +11 -0
  197. data/stdlib/opal-parser.rb +1 -2
  198. data/stdlib/opal-source-maps.rb +2 -0
  199. data/stdlib/phantomjs.rb +8 -0
  200. data/stdlib/process.rb +10 -0
  201. data/stdlib/promise.rb +12 -4
  202. data/stdlib/set.rb +27 -0
  203. data/stdlib/source_map.rb +5 -63
  204. data/stdlib/source_map/map.rb +220 -0
  205. data/stdlib/source_map/mapping.rb +26 -0
  206. data/stdlib/source_map/offset.rb +88 -0
  207. data/stdlib/source_map/version.rb +3 -0
  208. data/stdlib/source_map/vlq.rb +77 -101
  209. data/stdlib/sourcemap.rb +1 -0
  210. data/stdlib/strscan.rb +7 -1
  211. data/stdlib/template.rb +1 -1
  212. data/stdlib/thread.rb +147 -7
  213. metadata +238 -104
  214. data/lib/mspec/opal/mspec_fixes.rb +0 -87
  215. data/spec/cli/sprockets/environment_spec.rb +0 -14
  216. data/spec/filters/bugs/symbol.rb +0 -5
  217. data/spec/opal/core/kernel/warn_spec.rb +0 -83
  218. data/spec/opal/core/language/numbers_spec.rb +0 -60
  219. data/stdlib/opal-source-maps.js.erb +0 -2
  220. data/stdlib/source_map/generator.rb +0 -251
  221. data/stdlib/source_map/parser.rb +0 -102
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
1
  require 'opal'
2
- require 'file'
3
2
  require 'set'
4
3
  require 'opal-parser'
5
4
  require 'mspec'
6
- require 'mspec/opal/mspec_fixes'
5
+ require 'mspec/version'
6
+ require 'support/mspec_rspec_adapter'
7
7
  require 'mspec/opal/runner'
8
8
 
9
9
  require 'math'
@@ -19,9 +19,24 @@ module Kernel
19
19
  def eval_js(javascript)
20
20
  `eval(javascript)`
21
21
  end
22
+
23
+ def at_exit(&block)
24
+ $AT_EXIT_CALLBACKS ||= []
25
+ $AT_EXIT_CALLBACKS << block
26
+ end
27
+ end
28
+
29
+ if `!!window.OPAL_SPEC_PHANTOM`
30
+ require 'phantomjs'
31
+ formatter_class = PhantomFormatter
32
+ else
33
+ formatter_class = BrowserFormatter
22
34
  end
23
35
 
24
- formatter_class = `!!window.OPAL_SPEC_PHANTOM` ? PhantomFormatter : BrowserFormatter
36
+ # Uncomment the following to see example titles when they're executed.
37
+ # (useful to relate debug output to the example that generated it)
38
+ #
39
+ #formatter_class = PhantomDebugFormatter
25
40
 
26
41
  # As soon as this file loads, tell the runner the specs are starting
27
42
  OSpecRunner.main(formatter_class).will_start
@@ -0,0 +1,33 @@
1
+ module MSpecRSpecAdapter
2
+ def expect(object)
3
+ MSpecRSpecAdapterShould.new(object)
4
+ end
5
+
6
+ def eq(expected)
7
+ MSpecRSpecAdapterEq.new(expected)
8
+ end
9
+
10
+ class MSpecRSpecAdapterEq < Struct.new(:object)
11
+ end
12
+
13
+ class MSpecRSpecAdapterShould < Struct.new(:object)
14
+ def to(expectation)
15
+ apply_expectation(:should, expectation)
16
+ end
17
+
18
+ def apply_expectation(type, expectation)
19
+ if MSpecRSpecAdapterEq === expectation
20
+ object.send(type) == expectation.object
21
+ else
22
+ object.send(type, expectation)
23
+ end
24
+ end
25
+
26
+ def not_to
27
+ apply_expectation(:should_not, expectation)
28
+ end
29
+ alias to_not not_to
30
+ end
31
+ end
32
+
33
+ include MSpecRSpecAdapter unless defined? RSpec
@@ -1,7 +1,7 @@
1
- require 'opal'
1
+ require 'opal/parser'
2
2
 
3
- module OpalSpecHelpers
4
- def parsed(source, file='(string)')
3
+ module ParserHelpers
4
+ def parsed(source, file='(ParserHelpers:string)')
5
5
  Opal::Parser.new.parse(source, file)
6
6
  end
7
7
 
@@ -27,11 +27,11 @@ module OpalSpecHelpers
27
27
  end
28
28
  end
29
29
 
30
- RSpec.configure do |config|
31
- config.treat_symbols_as_metadata_keys_with_true_values = true
32
- config.run_all_when_everything_filtered = true
33
- config.filter_run :focus
34
- config.order = 'random'
35
-
36
- config.include OpalSpecHelpers
30
+ if defined? RSpec
31
+ RSpec.configure do |config|
32
+ config.include ParserHelpers
33
+ end
34
+ else
35
+ include ParserHelpers
37
36
  end
37
+
data/stdlib/README.md ADDED
@@ -0,0 +1,3 @@
1
+ # Opal stdlib
2
+
3
+ This is the Opal stdlib implementation API documentation.
@@ -0,0 +1,10 @@
1
+ module Benchmark
2
+
3
+ def measure(&block)
4
+ start = Time.now
5
+ yield
6
+ Time.now - start
7
+ end
8
+
9
+ module_function :measure
10
+ end
data/stdlib/date.rb CHANGED
@@ -24,7 +24,7 @@ class Date
24
24
 
25
25
  def -(date)
26
26
  %x{
27
- if (date._isNumber) {
27
+ if (date.$$is_number) {
28
28
  var result = #{clone};
29
29
  result.date.setDate(#@date.getDate() - date);
30
30
  return result;
@@ -40,7 +40,7 @@ class Date
40
40
 
41
41
  def +(date)
42
42
  %x{
43
- if (date._isNumber) {
43
+ if (date.$$is_number) {
44
44
  var result = #{clone};
45
45
  result.date.setDate(#@date.getDate() + date);
46
46
  return result;
data/stdlib/dir.rb CHANGED
@@ -1,5 +1 @@
1
- class Dir
2
- def self.getwd
3
- '.'
4
- end
5
- end
1
+ warn "Dir is already part of corelib now, you don't need to require it anymore."
data/stdlib/file.rb CHANGED
@@ -1,7 +1 @@
1
- class File
2
- SEPARATOR = '/'
3
-
4
- def self.expand_path(path, *)
5
- path
6
- end
7
- end
1
+ warn "File is already part of corelib now, you don't need to require it anymore."
data/stdlib/json.rb CHANGED
@@ -20,7 +20,7 @@ module JSON
20
20
  case 'object':
21
21
  if (!value) return nil;
22
22
 
23
- if (value._isArray) {
23
+ if (value.$$is_array) {
24
24
  var arr = #{`options.array_class`.new};
25
25
 
26
26
  for (var i = 0, ii = value.length; i < ii; i++) {
@@ -105,6 +105,14 @@ class Object
105
105
  end
106
106
  end
107
107
 
108
+ # BUG: Enumerable must come before Array, otherwise it overrides #to_json
109
+ # this is due to how modules are implemented.
110
+ module Enumerable
111
+ def to_json
112
+ to_a.to_json
113
+ end
114
+ end
115
+
108
116
  class Array
109
117
  def to_json
110
118
  %x{
@@ -171,3 +179,4 @@ class Date
171
179
  to_s
172
180
  end
173
181
  end
182
+
data/stdlib/native.rb CHANGED
@@ -128,7 +128,7 @@ end
128
128
 
129
129
  module Kernel
130
130
  def native?(value)
131
- `value == null || !value._klass`
131
+ `value == null || !value.$$class`
132
132
  end
133
133
 
134
134
  def Native(obj)
@@ -163,7 +163,7 @@ module Kernel
163
163
  end
164
164
 
165
165
  class Native::Object < BasicObject
166
- include Native
166
+ include ::Native
167
167
 
168
168
  def ==(other)
169
169
  `#@native === #{Native.try_convert(other)}`
@@ -256,11 +256,11 @@ class Native::Object < BasicObject
256
256
  alias kind_of? is_a?
257
257
 
258
258
  def instance_of?(klass)
259
- `self._klass === klass`
259
+ `self.$$class === klass`
260
260
  end
261
261
 
262
262
  def class
263
- `self._klass`
263
+ `self.$$class`
264
264
  end
265
265
 
266
266
  def to_a(options = {}, &block)
@@ -520,7 +520,7 @@ end
520
520
 
521
521
  class Class
522
522
  def native_alias(jsid, mid)
523
- `#{self}._proto[#{jsid}] = #{self}._proto['$' + #{mid}]`
523
+ `#{self}.$$proto[#{jsid}] = #{self}.$$proto['$' + #{mid}]`
524
524
  end
525
525
 
526
526
  alias native_class native_module
data/stdlib/nodejs.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'nodejs/runtime'
2
+ require 'nodejs/file'
3
+ require 'nodejs/dir'
4
+ require 'nodejs/io'
5
+ require 'nodejs/process'
@@ -0,0 +1,13 @@
1
+ class Dir
2
+ @__glob__ = NodeJS.require :glob
3
+ `var __glob__ = #{@__glob__}`
4
+
5
+ def self.[] glob
6
+ `__glob__.sync(#{glob})`
7
+ end
8
+
9
+ def pwd
10
+ `process.cwd()`
11
+ end
12
+ alias getwd pwd
13
+ end
@@ -0,0 +1,98 @@
1
+ class File < IO
2
+ include ::IO::Writable
3
+ include ::IO::Readable
4
+
5
+ @__fs__ = NodeJS.require :fs
6
+ @__path__ = NodeJS.require :path
7
+ `var __fs__ = #{@__fs__}`
8
+ `var __path__ = #{@__path__}`
9
+
10
+ def self.read path
11
+ `__fs__.readFileSync(#{path}).toString()`
12
+ end
13
+
14
+ def self.exist? path
15
+ `__fs__.existsSync(#{path})`
16
+ end
17
+
18
+ def self.realpath(pathname, dir_string = nil, cache = nil, &block)
19
+ pathname = join(dir_string, pathname) if dir_string
20
+ if block_given?
21
+ `
22
+ __fs__.realpath(#{pathname}, #{cache}, function(error, realpath){
23
+ if (error) #{raise error.message}
24
+ else #{block.call(`realpath`)}
25
+ })
26
+ `
27
+ else
28
+ `__fs__.realpathSync(#{pathname}, #{cache})`
29
+ end
30
+ end
31
+
32
+ def self.basename(path, ext = undefined)
33
+ `__path__.basename(#{path}, #{ext})`
34
+ end
35
+
36
+ def self.dirname(path)
37
+ `__path__.dirname(#{path})`
38
+ end
39
+
40
+ def self.join(*paths)
41
+ `__path__.join.apply(__path__, #{paths})`
42
+ end
43
+
44
+ def self.directory? path
45
+ return nil unless exist? path
46
+ `!!__fs__.lstatSync(path).isDirectory()`
47
+ end
48
+
49
+ def self.file? path
50
+ return nil unless exist? path
51
+ `!!__fs__.lstatSync(path).isFile()`
52
+ end
53
+
54
+ def self.size path
55
+ return nil unless exist? path
56
+ `__fs__.lstatSync(path).size`
57
+ end
58
+
59
+ def self.open path, flags
60
+ file = new(path, flags)
61
+ if block_given?
62
+ begin
63
+ yield(file)
64
+ ensure
65
+ file.close
66
+ end
67
+ else
68
+ file
69
+ end
70
+ end
71
+
72
+
73
+
74
+
75
+ # Instance Methods
76
+
77
+ def initialize(path, flags)
78
+ flags = flags.gsub(/b/, '')
79
+ @path = path
80
+ @flags = flags
81
+ @fd = `__fs__.openSync(path, flags)`
82
+ end
83
+
84
+ attr_reader :path
85
+
86
+ def write string
87
+ `__fs__.writeSync(#{@fd}, #{string}, null, #{string}.length)`
88
+ end
89
+
90
+ def flush
91
+ `__fs__.fsyncSync(#@fd)`
92
+ end
93
+
94
+ def close
95
+ `__fs__.closeSync(#{@fd})`
96
+ end
97
+ end
98
+
@@ -0,0 +1,26 @@
1
+ module FileUtils
2
+ extend self
3
+ `var __fs__ = #{File}.__fs__`
4
+
5
+ def mkdir_p path
6
+ return true if File.directory? path
7
+ `__fs__.mkdirSync(#{path})`
8
+ end
9
+
10
+ def cp source, target
11
+ target = File.join(target, File.basename(source)) if File.directory? target
12
+ `__fs__.writeFileSync(target, __fs__.readFileSync(source));`
13
+ end
14
+
15
+ def rm path
16
+ `__fs__.unlinkSync(path)`
17
+ end
18
+
19
+ def mv source, target
20
+ target = File.join(target, File.basename(source)) if File.directory? target
21
+ `__fs__.renameSync(source, target)`
22
+ end
23
+
24
+ alias mkpath mkdir_p
25
+ alias makedirs mkdir_p
26
+ end
@@ -0,0 +1,2 @@
1
+ $stdout.write_proc = -> (string) {`process.stdout.write(#{string})`}
2
+ $stderr.write_proc = -> (string) {`process.stderr.write(string)`}
@@ -0,0 +1,45 @@
1
+ require 'native'
2
+
3
+ NodeRepl = Native(`OpalNode.node_require('repl')`)
4
+
5
+ def NodeRepl.start opations = {}
6
+ Native::Object.new(`#@native.start(#{opations.to_n})`)
7
+ end
8
+
9
+ line = 1
10
+ prompt_interrupted = false
11
+
12
+ prompt = ->(_self) {
13
+ tip = prompt_interrupted ? '*' : '>'
14
+ "irb(#{_self.to_s}):#{line.to_s.rjust(3, '0')}#{tip} "
15
+ }
16
+
17
+ $repl = NodeRepl.start prompt: prompt.call(self),
18
+ useGlobal: true, ignoreUndefined: true,
19
+ eval: -> (cmd, context, filename, callback) {
20
+ line += 1
21
+
22
+ cmd = cmd[1...-1].chomp
23
+
24
+ if cmd.empty?
25
+ prompt_interrupted = true
26
+ $repl.prompt = prompt.call(self)
27
+
28
+ callback.call('')
29
+ next
30
+ end
31
+
32
+ prompt_interrupted = false
33
+ $repl.prompt = prompt.call(self)
34
+
35
+ begin
36
+ result = `OpalNode.run(cmd, filename)`
37
+ result = nil if `#{result} == nil`
38
+ callback.call('=> '+result.inspect)
39
+ rescue => e
40
+ callback.call(e.backtrace.join("\n"))
41
+ end
42
+ }
43
+
44
+ # Add a newline before exiting
45
+ $repl.on :exit, -> { puts }
@@ -0,0 +1,16 @@
1
+ module Kernel
2
+ def exit
3
+ `process.exit()`
4
+ end
5
+ end
6
+
7
+ ARGV = `process.argv.slice(2)`
8
+
9
+ ENV = Object.new
10
+ def ENV.[]= name, value
11
+ `process.env[#{name.to_s}] = #{value.to_s}`
12
+ end
13
+
14
+ def ENV.[] name
15
+ `process.env[#{name}] || nil`
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'opal-parser'
2
+
3
+ module Kernel
4
+ def __prepare_require__(path)
5
+ name = `$opal.normalize_loadable_path(#{path})`
6
+ full_path = name.end_with?('.rb') ? name : name+'.rb'
7
+
8
+ if `!$opal.modules[#{name}]`
9
+ ruby = File.read(full_path)
10
+ compiler = Opal::Compiler.new(ruby, requirable: true, file: name)
11
+ js = compiler.compile
12
+ compiler.requires.each do |sub_path|
13
+ __prepare_require__(sub_path)
14
+ end
15
+ `eval(#{js})`
16
+ end
17
+
18
+ name
19
+ rescue => e
20
+ raise [path, name, full_path].inspect+e.message
21
+ end
22
+
23
+ def require path
24
+ name = __prepare_require__(path)
25
+ `$opal.require(#{name})`
26
+ end
27
+
28
+ def load path
29
+ name = __prepare_require__(path)
30
+ `$opal.load(#{name})`
31
+ end
32
+ end